1.. zephyr:code-sample:: cmsis-rtos-v2
2   :name: Dining Philosophers (CMSIS RTOS V2 APIs)
3
4   Implement a solution to the Dining Philosophers problem using CMSIS RTOS V2.
5
6Overview
7********
8This sample implements a solution to the `Dining Philosophers problem
9<https://en.wikipedia.org/wiki/Dining_philosophers_problem>`_ (a classic
10multi-thread synchronization problem) using CMSIS RTOS V2 APIs.  This particular
11implementation demonstrates the usage of multiple preemptible and cooperative
12threads of differing priorities, as well as mutex/semaphore and thread sleeping
13which uses CMSIS RTOS V2 API implementation in Zephyr.
14
15The philosopher always tries to get the lowest fork first (f1 then f2).  When
16done, he will give back the forks in the reverse order (f2 then f1).  If he
17gets two forks, he is EATING.  Otherwise, he is THINKING. Transitional states
18are shown as well, such as STARVING when the philosopher is hungry but the
19forks are not available, and HOLDING ONE FORK when a philosopher is waiting
20for the second fork to be available.
21
22Each Philosopher will randomly alternate between the EATING and THINKING state.
23
24
25Building and Running
26********************
27
28This project outputs to the console.  It can be built and executed
29on QEMU as follows:
30
31.. zephyr-app-commands::
32   :zephyr-app: samples/philosophers
33   :host-os: unix
34   :board: qemu_x86
35   :goals: run
36   :compact:
37
38Sample Output
39=============
40
41.. code-block:: console
42
43    Philosopher 0 [P: 8]    HOLDING ONE FORK
44    Philosopher 1 [P: 9]    HOLDING ONE FORK
45    Philosopher 2 [P: 10]   EATING  [  575 ms ]
46    Philosopher 3 [P: 11]  THINKING [  225 ms ]
47    Philosopher 4 [P: 12]  THINKING [  425 ms ]
48    Philosopher 5 [P: 13]  THINKING [  325 ms ]
49
50    Demo Description
51    ----------------
52    An implementation of a solution to the Dining Philosophers
53    problem (a classic multi-thread synchronization problem) using
54    CMSIS RTOS V2 APIs. This particular implementation demonstrates the
55    usage of multiple preemptible threads of differing
56    priorities, as well as semaphores and thread sleeping.
57
58Exit QEMU by pressing :kbd:`CTRL+A` :kbd:`x`.
59