Part 1
요약·해설
20개 worker, 각 5회 작업, 20회 signal과 161줄 실행 출력을 보존합니다.
20
5 rounds
done == 20
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
26432D960A38854BCC67DE549858BCEFF3DADC95D878F423888C5F5D4E5D327F
.. _samples_kernel_simple_condition_variables:
Condition Variables
###################
Overview
********
This sample demonstrates the usage of condition variables in a
multithreaded application. Condition variables are used with a mutex
to signal changing states (conditions) from worker thread to the main
thread. Main thread uses a condition variable to wait for a condition to
become true. Main thread and the worker thread alternate between their
execution based on when the worker thread signals the main thread that is
pending on the condition variable. The sample can be used with any
:ref:`supported board <boards>` and prints the sample output shown to
the console.
Building and Running
********************
This application can be built and executed on :ref:`native_sim <native_sim>` as follows:
.. zephyr-app-commands::
:zephyr-app: samples/kernel/condition_variables/simple
:host-os: unix
:board: native_sim
:goals: run
:compact:
To build for another board, change ``native_sim`` above to that board's name.
Sample Output
=============
.. code-block:: console
[thread 0] working (0/5)
[thread 1] working (0/5)
[thread 2] working (0/5)
[thread 3] working (0/5)
[thread 4] working (0/5)
[thread 5] working (0/5)
[thread 6] working (0/5)
[thread 7] working (0/5)
[thread 8] working (0/5)
[thread 9] working (0/5)
[thread 10] working (0/5)
[thread 11] working (0/5)
[thread 12] working (0/5)
[thread 13] working (0/5)
[thread 14] working (0/5)
[thread 15] working (0/5)
[thread 16] working (0/5)
[thread 17] working (0/5)
[thread 18] working (0/5)
[thread 19] working (0/5)
[thread 0] working (1/5)
[thread 1] working (1/5)
[thread 2] working (1/5)
[thread 3] working (1/5)
[thread 4] working (1/5)
[thread 5] working (1/5)
[thread 6] working (1/5)
[thread 7] working (1/5)
[thread 8] working (1/5)
[thread 9] working (1/5)
[thread 10] working (1/5)
[thread 11] working (1/5)
[thread 12] working (1/5)
[thread 13] working (1/5)
[thread 14] working (1/5)
[thread 15] working (1/5)
[thread 16] working (1/5)
[thread 17] working (1/5)
[thread 18] working (1/5)
[thread 19] working (1/5)
[thread zephyr_app_main] done is 0 which is < 20 so waiting on cond
[thread 0] working (2/5)
[thread 1] working (2/5)
[thread 2] working (2/5)
[thread 3] working (2/5)
[thread 4] working (2/5)
[thread 5] working (2/5)
[thread 6] working (2/5)
[thread 7] working (2/5)
[thread 8] working (2/5)
[thread 9] working (2/5)
[thread 10] working (2/5)
[thread 11] working (2/5)
[thread 12] working (2/5)
[thread 13] working (2/5)
[thread 14] working (2/5)
[thread 15] working (2/5)
[thread 16] working (2/5)
[thread 17] working (2/5)
[thread 18] working (2/5)
[thread 19] working (2/5)
[thread 0] working (3/5)
[thread 1] working (3/5)
[thread 2] working (3/5)
[thread 3] working (3/5)
[thread 4] working (3/5)
[thread 5] working (3/5)
[thread 6] working (3/5)
[thread 7] working (3/5)
[thread 8] working (3/5)
[thread 9] working (3/5)
[thread 10] working (3/5)
[thread 11] working (3/5)
[thread 12] working (3/5)
[thread 13] working (3/5)
[thread 14] working (3/5)
[thread 15] working (3/5)
[thread 16] working (3/5)
[thread 17] working (3/5)
[thread 18] working (3/5)
[thread 19] working (3/5)
[thread 0] working (4/5)
[thread 1] working (4/5)
[thread 2] working (4/5)
[thread 3] working (4/5)
[thread 4] working (4/5)
[thread 5] working (4/5)
[thread 6] working (4/5)
[thread 7] working (4/5)
[thread 8] working (4/5)
[thread 9] working (4/5)
[thread 10] working (4/5)
[thread 11] working (4/5)
[thread 12] working (4/5)
[thread 13] working (4/5)
[thread 14] working (4/5)
[thread 15] working (4/5)
[thread 16] working (4/5)
[thread 17] working (4/5)
[thread 18] working (4/5)
[thread 19] working (4/5)
[thread 0] done is now 1. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 1 which is < 20 so waiting on cond
[thread 1] done is now 2. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 2 which is < 20 so waiting on cond
[thread 2] done is now 3. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 3 which is < 20 so waiting on cond
[thread 3] done is now 4. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 4 which is < 20 so waiting on cond
[thread 4] done is now 5. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 5 which is < 20 so waiting on cond
[thread 5] done is now 6. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 6 which is < 20 so waiting on cond
[thread 6] done is now 7. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 7 which is < 20 so waiting on cond
[thread 7] done is now 8. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 8 which is < 20 so waiting on cond
[thread 8] done is now 9. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 9 which is < 20 so waiting on cond
[thread 9] done is now 10. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 10 which is < 20 so waiting on cond
[thread 10] done is now 11. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 11 which is < 20 so waiting on cond
[thread 11] done is now 12. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 12 which is < 20 so waiting on cond
[thread 12] done is now 13. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 13 which is < 20 so waiting on cond
[thread 13] done is now 14. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 14 which is < 20 so waiting on cond
[thread 14] done is now 15. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 15 which is < 20 so waiting on cond
[thread 15] done is now 16. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 16 which is < 20 so waiting on cond
[thread 16] done is now 17. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 17 which is < 20 so waiting on cond
[thread 17] done is now 18. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 18 which is < 20 so waiting on cond
[thread 18] done is now 19. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 19 which is < 20 so waiting on cond
[thread 19] done is now 20. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done == 20 so everyone is done
Part 3
한국어 전문 번역
개요
이 sample은 multithread application에서 condition variable을 사용하는 방법을 보여줍니다. Worker thread가 main thread에 상태, 즉 condition의 변화를 알릴 때 condition variable을 mutex와 함께 사용합니다.
Main thread는 condition이 true가 될 때까지 condition variable에서 기다립니다. Condition variable에서 대기 중인 main thread에 worker thread가 signal을 보내는 시점을 기준으로 main thread와 worker thread의 실행이 번갈아 진행됩니다.
지원되는 모든 board에서 sample을 사용할 수 있으며 아래의 실행 과정을 console에 출력합니다. Worker thread 20개가 각각 다섯 차례 작업하고, 완료할 때마다 shared done count를 증가시킨 뒤 condition variable에 signal을 보냅니다.
Main thread는 mutex가 보호하는 done 값을 검사합니다. 값이 20보다 작으면 condition variable에서 다시 기다리고, signal로 깨어날 때마다 조건을 재검사합니다. 마지막 worker가 done == 20을 만들면 모든 작업이 끝난 것으로 판단합니다.
Build와 실행
다음과 같이 native_sim에서 application을 build하고 실행할 수 있습니다.
west build -b native_sim samples/kernel/condition_variables/simple
west build -t run
다른 board용으로 build하려면 위 명령의 native_sim을 해당 board 이름으로 바꾸십시오.
Sample 출력
[thread 0] working (0/5)
[thread 1] working (0/5)
[thread 2] working (0/5)
[thread 3] working (0/5)
[thread 4] working (0/5)
[thread 5] working (0/5)
[thread 6] working (0/5)
[thread 7] working (0/5)
[thread 8] working (0/5)
[thread 9] working (0/5)
[thread 10] working (0/5)
[thread 11] working (0/5)
[thread 12] working (0/5)
[thread 13] working (0/5)
[thread 14] working (0/5)
[thread 15] working (0/5)
[thread 16] working (0/5)
[thread 17] working (0/5)
[thread 18] working (0/5)
[thread 19] working (0/5)
[thread 0] working (1/5)
[thread 1] working (1/5)
[thread 2] working (1/5)
[thread 3] working (1/5)
[thread 4] working (1/5)
[thread 5] working (1/5)
[thread 6] working (1/5)
[thread 7] working (1/5)
[thread 8] working (1/5)
[thread 9] working (1/5)
[thread 10] working (1/5)
[thread 11] working (1/5)
[thread 12] working (1/5)
[thread 13] working (1/5)
[thread 14] working (1/5)
[thread 15] working (1/5)
[thread 16] working (1/5)
[thread 17] working (1/5)
[thread 18] working (1/5)
[thread 19] working (1/5)
[thread zephyr_app_main] done is 0 which is < 20 so waiting on cond
[thread 0] working (2/5)
[thread 1] working (2/5)
[thread 2] working (2/5)
[thread 3] working (2/5)
[thread 4] working (2/5)
[thread 5] working (2/5)
[thread 6] working (2/5)
[thread 7] working (2/5)
[thread 8] working (2/5)
[thread 9] working (2/5)
[thread 10] working (2/5)
[thread 11] working (2/5)
[thread 12] working (2/5)
[thread 13] working (2/5)
[thread 14] working (2/5)
[thread 15] working (2/5)
[thread 16] working (2/5)
[thread 17] working (2/5)
[thread 18] working (2/5)
[thread 19] working (2/5)
[thread 0] working (3/5)
[thread 1] working (3/5)
[thread 2] working (3/5)
[thread 3] working (3/5)
[thread 4] working (3/5)
[thread 5] working (3/5)
[thread 6] working (3/5)
[thread 7] working (3/5)
[thread 8] working (3/5)
[thread 9] working (3/5)
[thread 10] working (3/5)
[thread 11] working (3/5)
[thread 12] working (3/5)
[thread 13] working (3/5)
[thread 14] working (3/5)
[thread 15] working (3/5)
[thread 16] working (3/5)
[thread 17] working (3/5)
[thread 18] working (3/5)
[thread 19] working (3/5)
[thread 0] working (4/5)
[thread 1] working (4/5)
[thread 2] working (4/5)
[thread 3] working (4/5)
[thread 4] working (4/5)
[thread 5] working (4/5)
[thread 6] working (4/5)
[thread 7] working (4/5)
[thread 8] working (4/5)
[thread 9] working (4/5)
[thread 10] working (4/5)
[thread 11] working (4/5)
[thread 12] working (4/5)
[thread 13] working (4/5)
[thread 14] working (4/5)
[thread 15] working (4/5)
[thread 16] working (4/5)
[thread 17] working (4/5)
[thread 18] working (4/5)
[thread 19] working (4/5)
[thread 0] done is now 1. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 1 which is < 20 so waiting on cond
[thread 1] done is now 2. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 2 which is < 20 so waiting on cond
[thread 2] done is now 3. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 3 which is < 20 so waiting on cond
[thread 3] done is now 4. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 4 which is < 20 so waiting on cond
[thread 4] done is now 5. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 5 which is < 20 so waiting on cond
[thread 5] done is now 6. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 6 which is < 20 so waiting on cond
[thread 6] done is now 7. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 7 which is < 20 so waiting on cond
[thread 7] done is now 8. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 8 which is < 20 so waiting on cond
[thread 8] done is now 9. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 9 which is < 20 so waiting on cond
[thread 9] done is now 10. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 10 which is < 20 so waiting on cond
[thread 10] done is now 11. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 11 which is < 20 so waiting on cond
[thread 11] done is now 12. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 12 which is < 20 so waiting on cond
[thread 12] done is now 13. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 13 which is < 20 so waiting on cond
[thread 13] done is now 14. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 14 which is < 20 so waiting on cond
[thread 14] done is now 15. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 15 which is < 20 so waiting on cond
[thread 15] done is now 16. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 16 which is < 20 so waiting on cond
[thread 16] done is now 17. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 17 which is < 20 so waiting on cond
[thread 17] done is now 18. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 18 which is < 20 so waiting on cond
[thread 18] done is now 19. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done is 19 which is < 20 so waiting on cond
[thread 19] done is now 20. Signalling cond.
[thread zephyr_app_main] wake - cond was signalled.
[thread zephyr_app_main] done == 20 so everyone is done
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.