README.rst
1.. zephyr:code-sample:: kernel-condvar
2 :name: Condition Variables
3 :relevant-api: condvar_apis
4
5 Manipulate condition variables in a multithreaded application.
6
7Overview
8********
9
10This sample demonstrates the usage of :ref:`condition variables <condvar>` in a
11multithreaded application. Condition variables are used with a mutex
12to signal changing states (conditions) from one thread to another
13thread. A thread uses a condition variable to wait for a condition to
14become true. Different threads alternate between their entry point
15function execution based on when they signal the other thread that is
16pending on the condition variable. The sample can be used with any
17:ref:`supported board <boards>` and prints the sample output shown to
18the console.
19
20Building and Running
21********************
22
23This application can be built and executed on :ref:`native_sim <native_sim>` as follows:
24
25.. zephyr-app-commands::
26 :zephyr-app: samples/kernel/condition_variables/condvar
27 :host-os: unix
28 :board: native_sim
29 :goals: run
30 :compact:
31
32To build for another board, change ``native_sim`` above to that board's name.
33
34Sample Output
35=============
36
37.. code-block:: console
38
39 Starting watch_count: thread 1
40 watch_count: thread 1 Count= 0. Going into wait...
41 inc_count: thread 2, count = 1, unlocking mutex
42 inc_count: thread 3, count = 2, unlocking mutex
43 inc_count: thread 2, count = 3, unlocking mutex
44 inc_count: thread 3, count = 4, unlocking mutex
45 inc_count: thread 2, count = 5, unlocking mutex
46 inc_count: thread 3, count = 6, unlocking mutex
47 inc_count: thread 2, count = 7, unlocking mutex
48 inc_count: thread 3, count = 8, unlocking mutex
49 inc_count: thread 2, count = 9, unlocking mutex
50 inc_count: thread 3, count = 10, unlocking mutex
51 inc_count: thread 2, count = 11, unlocking mutex
52 inc_count: thread 3, count = 12 Threshold reached.Just sent signal.
53 inc_count: thread 3, count = 12, unlocking mutex
54 watch_count: thread 1 Condition signal received. Count= 12
55 watch_count: thread 1 Updating the value of count...
56 watch_count: thread 1 count now = 137.
57 watch_count: thread 1 Unlocking mutex.
58 inc_count: thread 2, count = 138, unlocking mutex
59 inc_count: thread 3, count = 139, unlocking mutex
60 inc_count: thread 2, count = 140, unlocking mutex
61 inc_count: thread 3, count = 141, unlocking mutex
62 inc_count: thread 2, count = 142, unlocking mutex
63 inc_count: thread 3, count = 143, unlocking mutex
64 inc_count: thread 2, count = 144, unlocking mutex
65 inc_count: thread 3, count = 145, unlocking mutex
66 Main(): Waited and joined with 3 threads. Final value of count = 145. Done.
67