samples/kernel/condition_variables/condvar/README.html · samples/kernel/condition_variables/condvar/README.rst 공식 원문 ↗

Zephyr 3.7.0 · Samples · Kernel

Condition Variables

Count threshold에서 signal을 보내 대기 thread를 깨우고 세 thread를 join합니다.

Official pathsamples/kernel/condition_variables/condvar/README.html
Source filesamples/kernel/condition_variables/condvar/README.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Thread 1·2·3의 wait, mutex unlock, threshold signal과 최종 count 145를 보존합니다.

Threads

3

Signal

Count = 12

Final

Count = 145

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 DB57E1636884A9AE2752F619A6515BFB301EB039329F035622AC0F86C49E12F9
.. zephyr:code-sample:: kernel-condvar
   :name: Condition Variables
   :relevant-api: condvar_apis

   Manipulate condition variables in a multithreaded application.

Overview
********

This sample demonstrates the usage of :ref:`condition variables <condvar>` in a
multithreaded application. Condition variables are used with a mutex
to signal changing states (conditions) from one thread to another
thread. A thread uses a condition variable to wait for a condition to
become true. Different threads alternate between their entry point
function execution based on when they signal the other 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/condvar
   :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

    Starting watch_count: thread 1
    watch_count: thread 1 Count= 0. Going into wait...
    inc_count: thread 2, count = 1, unlocking mutex
    inc_count: thread 3, count = 2, unlocking mutex
    inc_count: thread 2, count = 3, unlocking mutex
    inc_count: thread 3, count = 4, unlocking mutex
    inc_count: thread 2, count = 5, unlocking mutex
    inc_count: thread 3, count = 6, unlocking mutex
    inc_count: thread 2, count = 7, unlocking mutex
    inc_count: thread 3, count = 8, unlocking mutex
    inc_count: thread 2, count = 9, unlocking mutex
    inc_count: thread 3, count = 10, unlocking mutex
    inc_count: thread 2, count = 11, unlocking mutex
    inc_count: thread 3, count = 12  Threshold reached.Just sent signal.
    inc_count: thread 3, count = 12, unlocking mutex
    watch_count: thread 1 Condition signal received. Count= 12
    watch_count: thread 1 Updating the value of count...
    watch_count: thread 1 count now = 137.
    watch_count: thread 1 Unlocking mutex.
    inc_count: thread 2, count = 138, unlocking mutex
    inc_count: thread 3, count = 139, unlocking mutex
    inc_count: thread 2, count = 140, unlocking mutex
    inc_count: thread 3, count = 141, unlocking mutex
    inc_count: thread 2, count = 142, unlocking mutex
    inc_count: thread 3, count = 143, unlocking mutex
    inc_count: thread 2, count = 144, unlocking mutex
    inc_count: thread 3, count = 145, unlocking mutex
    Main(): Waited and joined with 3 threads. Final value of count = 145. Done.

Part 3

한국어 전문 번역

개요

이 sample은 multithread application에서 condition variable을 사용하는 방법을 보여줍니다. Condition variable은 mutex와 함께 사용하여 한 thread에서 다른 thread로 상태, 즉 condition의 변화를 알립니다.

Thread 하나는 condition이 true가 될 때까지 condition variable에서 기다립니다. 다른 thread들은 기다리는 thread에 signal을 보내는 시점에 따라 entry point function 실행을 번갈아 진행합니다.

지원되는 모든 board에서 sample을 사용할 수 있으며, 아래에 표시한 실행 과정을 console에 출력합니다. 출력에서는 thread 1이 count를 감시하고, thread 2와 3이 mutex를 잠근 상태에서 값을 증가시킵니다. 값이 12에 도달하면 signal을 보내 대기 중인 thread 1을 깨웁니다.

Build와 실행

다음과 같이 native_sim에서 application을 build하고 실행할 수 있습니다.

west build -b native_sim samples/kernel/condition_variables/condvar
west build -t run

다른 board용으로 build하려면 위 명령의 native_sim을 해당 board 이름으로 바꾸십시오.

Sample 출력

Starting watch_count: thread 1
watch_count: thread 1 Count= 0. Going into wait...
inc_count: thread 2, count = 1, unlocking mutex
inc_count: thread 3, count = 2, unlocking mutex
inc_count: thread 2, count = 3, unlocking mutex
inc_count: thread 3, count = 4, unlocking mutex
inc_count: thread 2, count = 5, unlocking mutex
inc_count: thread 3, count = 6, unlocking mutex
inc_count: thread 2, count = 7, unlocking mutex
inc_count: thread 3, count = 8, unlocking mutex
inc_count: thread 2, count = 9, unlocking mutex
inc_count: thread 3, count = 10, unlocking mutex
inc_count: thread 2, count = 11, unlocking mutex
inc_count: thread 3, count = 12  Threshold reached.Just sent signal.
inc_count: thread 3, count = 12, unlocking mutex
watch_count: thread 1 Condition signal received. Count= 12
watch_count: thread 1 Updating the value of count...
watch_count: thread 1 count now = 137.
watch_count: thread 1 Unlocking mutex.
inc_count: thread 2, count = 138, unlocking mutex
inc_count: thread 3, count = 139, unlocking mutex
inc_count: thread 2, count = 140, unlocking mutex
inc_count: thread 3, count = 141, unlocking mutex
inc_count: thread 2, count = 142, unlocking mutex
inc_count: thread 3, count = 143, unlocking mutex
inc_count: thread 2, count = 144, unlocking mutex
inc_count: thread 3, count = 145, unlocking mutex
Main(): Waited and joined with 3 threads. Final value of count = 145. Done.

Signal을 받은 thread 1은 count를 137로 갱신하고 mutex를 풉니다. 증가 thread들이 값을 145까지 올린 뒤 main thread가 세 thread의 종료를 기다려 join하면 sample이 끝납니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.