1:orphan: 2 3.. _samples_kernel_simple_condition_variables: 4 5Condition Variables 6################### 7 8Overview 9******** 10 11This sample demonstrates the usage of condition variables in a 12multithreaded application. Condition variables are used with a mutex 13to signal changing states (conditions) from worker thread to the main 14thread. Main thread uses a condition variable to wait for a condition to 15become true. Main thread and the worker thread alternate between their 16execution based on when the worker thread signals the main thread that is 17pending on the condition variable. The sample can be used with any 18:ref:`supported board <boards>` and prints the sample output shown to 19the console. 20 21Building and Running 22******************** 23 24This application can be built and executed on :ref:`native_sim <native_sim>` as follows: 25 26.. zephyr-app-commands:: 27 :zephyr-app: samples/kernel/condition_variables/simple 28 :host-os: unix 29 :board: native_sim 30 :goals: run 31 :compact: 32 33To build for another board, change ``native_sim`` above to that board's name. 34 35Sample Output 36============= 37 38.. code-block:: console 39 40 [thread 0] working (0/5) 41 [thread 1] working (0/5) 42 [thread 2] working (0/5) 43 [thread 3] working (0/5) 44 [thread 4] working (0/5) 45 [thread 5] working (0/5) 46 [thread 6] working (0/5) 47 [thread 7] working (0/5) 48 [thread 8] working (0/5) 49 [thread 9] working (0/5) 50 [thread 10] working (0/5) 51 [thread 11] working (0/5) 52 [thread 12] working (0/5) 53 [thread 13] working (0/5) 54 [thread 14] working (0/5) 55 [thread 15] working (0/5) 56 [thread 16] working (0/5) 57 [thread 17] working (0/5) 58 [thread 18] working (0/5) 59 [thread 19] working (0/5) 60 [thread 0] working (1/5) 61 [thread 1] working (1/5) 62 [thread 2] working (1/5) 63 [thread 3] working (1/5) 64 [thread 4] working (1/5) 65 [thread 5] working (1/5) 66 [thread 6] working (1/5) 67 [thread 7] working (1/5) 68 [thread 8] working (1/5) 69 [thread 9] working (1/5) 70 [thread 10] working (1/5) 71 [thread 11] working (1/5) 72 [thread 12] working (1/5) 73 [thread 13] working (1/5) 74 [thread 14] working (1/5) 75 [thread 15] working (1/5) 76 [thread 16] working (1/5) 77 [thread 17] working (1/5) 78 [thread 18] working (1/5) 79 [thread 19] working (1/5) 80 [thread zephyr_app_main] done is 0 which is < 20 so waiting on cond 81 [thread 0] working (2/5) 82 [thread 1] working (2/5) 83 [thread 2] working (2/5) 84 [thread 3] working (2/5) 85 [thread 4] working (2/5) 86 [thread 5] working (2/5) 87 [thread 6] working (2/5) 88 [thread 7] working (2/5) 89 [thread 8] working (2/5) 90 [thread 9] working (2/5) 91 [thread 10] working (2/5) 92 [thread 11] working (2/5) 93 [thread 12] working (2/5) 94 [thread 13] working (2/5) 95 [thread 14] working (2/5) 96 [thread 15] working (2/5) 97 [thread 16] working (2/5) 98 [thread 17] working (2/5) 99 [thread 18] working (2/5) 100 [thread 19] working (2/5) 101 [thread 0] working (3/5) 102 [thread 1] working (3/5) 103 [thread 2] working (3/5) 104 [thread 3] working (3/5) 105 [thread 4] working (3/5) 106 [thread 5] working (3/5) 107 [thread 6] working (3/5) 108 [thread 7] working (3/5) 109 [thread 8] working (3/5) 110 [thread 9] working (3/5) 111 [thread 10] working (3/5) 112 [thread 11] working (3/5) 113 [thread 12] working (3/5) 114 [thread 13] working (3/5) 115 [thread 14] working (3/5) 116 [thread 15] working (3/5) 117 [thread 16] working (3/5) 118 [thread 17] working (3/5) 119 [thread 18] working (3/5) 120 [thread 19] working (3/5) 121 [thread 0] working (4/5) 122 [thread 1] working (4/5) 123 [thread 2] working (4/5) 124 [thread 3] working (4/5) 125 [thread 4] working (4/5) 126 [thread 5] working (4/5) 127 [thread 6] working (4/5) 128 [thread 7] working (4/5) 129 [thread 8] working (4/5) 130 [thread 9] working (4/5) 131 [thread 10] working (4/5) 132 [thread 11] working (4/5) 133 [thread 12] working (4/5) 134 [thread 13] working (4/5) 135 [thread 14] working (4/5) 136 [thread 15] working (4/5) 137 [thread 16] working (4/5) 138 [thread 17] working (4/5) 139 [thread 18] working (4/5) 140 [thread 19] working (4/5) 141 [thread 0] done is now 1. Signalling cond. 142 [thread zephyr_app_main] wake - cond was signalled. 143 [thread zephyr_app_main] done is 1 which is < 20 so waiting on cond 144 [thread 1] done is now 2. Signalling cond. 145 [thread zephyr_app_main] wake - cond was signalled. 146 [thread zephyr_app_main] done is 2 which is < 20 so waiting on cond 147 [thread 2] done is now 3. Signalling cond. 148 [thread zephyr_app_main] wake - cond was signalled. 149 [thread zephyr_app_main] done is 3 which is < 20 so waiting on cond 150 [thread 3] done is now 4. Signalling cond. 151 [thread zephyr_app_main] wake - cond was signalled. 152 [thread zephyr_app_main] done is 4 which is < 20 so waiting on cond 153 [thread 4] done is now 5. Signalling cond. 154 [thread zephyr_app_main] wake - cond was signalled. 155 [thread zephyr_app_main] done is 5 which is < 20 so waiting on cond 156 [thread 5] done is now 6. Signalling cond. 157 [thread zephyr_app_main] wake - cond was signalled. 158 [thread zephyr_app_main] done is 6 which is < 20 so waiting on cond 159 [thread 6] done is now 7. Signalling cond. 160 [thread zephyr_app_main] wake - cond was signalled. 161 [thread zephyr_app_main] done is 7 which is < 20 so waiting on cond 162 [thread 7] done is now 8. Signalling cond. 163 [thread zephyr_app_main] wake - cond was signalled. 164 [thread zephyr_app_main] done is 8 which is < 20 so waiting on cond 165 [thread 8] done is now 9. Signalling cond. 166 [thread zephyr_app_main] wake - cond was signalled. 167 [thread zephyr_app_main] done is 9 which is < 20 so waiting on cond 168 [thread 9] done is now 10. Signalling cond. 169 [thread zephyr_app_main] wake - cond was signalled. 170 [thread zephyr_app_main] done is 10 which is < 20 so waiting on cond 171 [thread 10] done is now 11. Signalling cond. 172 [thread zephyr_app_main] wake - cond was signalled. 173 [thread zephyr_app_main] done is 11 which is < 20 so waiting on cond 174 [thread 11] done is now 12. Signalling cond. 175 [thread zephyr_app_main] wake - cond was signalled. 176 [thread zephyr_app_main] done is 12 which is < 20 so waiting on cond 177 [thread 12] done is now 13. Signalling cond. 178 [thread zephyr_app_main] wake - cond was signalled. 179 [thread zephyr_app_main] done is 13 which is < 20 so waiting on cond 180 [thread 13] done is now 14. Signalling cond. 181 [thread zephyr_app_main] wake - cond was signalled. 182 [thread zephyr_app_main] done is 14 which is < 20 so waiting on cond 183 [thread 14] done is now 15. Signalling cond. 184 [thread zephyr_app_main] wake - cond was signalled. 185 [thread zephyr_app_main] done is 15 which is < 20 so waiting on cond 186 [thread 15] done is now 16. Signalling cond. 187 [thread zephyr_app_main] wake - cond was signalled. 188 [thread zephyr_app_main] done is 16 which is < 20 so waiting on cond 189 [thread 16] done is now 17. Signalling cond. 190 [thread zephyr_app_main] wake - cond was signalled. 191 [thread zephyr_app_main] done is 17 which is < 20 so waiting on cond 192 [thread 17] done is now 18. Signalling cond. 193 [thread zephyr_app_main] wake - cond was signalled. 194 [thread zephyr_app_main] done is 18 which is < 20 so waiting on cond 195 [thread 18] done is now 19. Signalling cond. 196 [thread zephyr_app_main] wake - cond was signalled. 197 [thread zephyr_app_main] done is 19 which is < 20 so waiting on cond 198 [thread 19] done is now 20. Signalling cond. 199 [thread zephyr_app_main] wake - cond was signalled. 200 [thread zephyr_app_main] done == 20 so everyone is done 201