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