Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It sounds like you have two questions here:</p> <h2>How can I wait until my thread completes?</h2> <p>Answer: This is directly supported by pthreads -- make your thread-to-be-stopped JOINABLE (when it is first started), and use pthread_join() to block your current thread until the thread-to-be-stopped is no longer running.</p> <hr> <h2>How can I tell if my thread is still running?</h2> <p>Answer: You can add a "thread_complete" flag to do the trick:</p> <p>Scenario: Thread A wants to know if Thread B is still alive.</p> <p>When Thread B is created, it is given a pointer to the "thread_complete" flag address. The "thread_complete" flag should be initialized to NOT_COMPLETED before the thread is created. Thread B's entry point function should immediately call pthread_cleanup_push() to push a "cleanup handler" which sets the "thread_complete" flag to COMPLETED.</p> <p>See details about cleanup handlers here: <a href="http://www.opengroup.org/onlinepubs/000095399/functions/pthread_cleanup_pop.html" rel="noreferrer">pthread cleanup handlers</a></p> <p>You'll want to include a corresponding pthread_cleanup_pop(1) call to ensure that the cleanup handler gets called no matter what (i.e. if the thread exits normally OR due to cancellation, etc.).</p> <p>Then, Thread A can simply check the "thread_complete" flag to see if Thread B has exited yet.</p> <p>NOTE: Your "thread_complete" flag should be declared "volatile" and should be an atomic type -- the GNU compilers provide the sig_atomic_t for this purpose. This allows the two threads consistent access the same data without the need for synchronization constructs (mutexes/semaphores).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload