Killing a pthread waiting on a condition variable

前端 未结 3 2304
刺人心
刺人心 2021-02-15 16:58

I have a pthread waiting on a condition variable using pthread_cond_wait(). It\'s waiting for data from a queue structure that is filled by another thread. I want

3条回答
  •  萌比男神i
    2021-02-15 17:19

    Do you have access to the queue, and control of the object schema for enqueued items? If so, define a queue object type that when de-queued, instructs the thread that is processing the item to exit gracefully.

    Now, to shut down these threads, simply post a number of these "quit" objects to the HEAD of the queue that corresponds to the number of threads that are servicing the queue, and join on the threads.

    This seems much cleaner than the "nuclear option" of pthread_cancel/kill.

提交回复
热议问题