waiting thread until a condition has been occurred

前端 未结 4 1125
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 10:22

I want to wait one thread of 2 thread that executed in a Simultaneous simulator until a condition has been occurred, may be the condition occurred after 1000 or more cycles

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 10:39

    Using Semaphore for signalling. Example (application clean exit) as below:

    Declare in header

    static sem_t semPrepareExit;            //declaration
    

    In source (main thread);

    sem_init(&semPrepareExit, 0, 0);        ///semaphore initialized
    ...
    ///now wait for the signal on the semaphore, to proceed hereforth
    sem_post(&semPrepareExit);
    /// cleanup ahead
    ...
    

    In source, (spawned-thread);

    ...
    sem_post(&semPrepareExit);
    

    Now, as soon as you signal on the semaphore using "sem_post". The main-thread will receive the signal at the wait-node/point, and will proceed, there-forth.

提交回复
热议问题