Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to make a few assumptions because, as more knowledgable people than me have pointed out, this kind of behavior immediately raises the question of <em>why</em> you (think you) need to destroy/recreate these threads, and why this is as obfuscated as it is:</p> <ul> <li>You have a valid reason for needing to destroy/recreate the threads</li> <li>You have a valid reason for adding this many possibly useless layers to the thread creation process</li> <li>Your code which will "receive a message from the other user agent" has access to the thread ids</li> <li>Your <code>send</code> and <code>recv</code> functions can have access to some sort of flagging mechanism</li> </ul> <p>This is such a shoe-horn approach that I'm almost afraid to approach it. Without knowing more about the constraints of your design, it's difficult to express more than some of the options you <em>can</em> start exploring.</p> <p>First, let's set up the <code>send</code> and <code>recv</code> functions so that they can be notified that it's time to go bye-bye:</p> <pre><code>void* send_thread(void *arg) { pthread_mutex_lock(&amp;wrapUpFlagMutex); bool timeToQuit = wrapUpFlag; pthread_mutex_unlock(&amp;wrapUpFlagMutex); while( timeToQuit == false ) { ... // You're doing something here ... pthread_mutex_lock(&amp;wrapUpFlagMutex); timeToQuit = wrapUpFlag; pthread_mutex_unlock(&amp;wrapUpFlagMutex); } // We've been flagged! Get out...the join will catch us. pthread_exit(); } </code></pre> <p>Now change the code which somehow knows when the magic needs to end and be restarted:</p> <pre><code>dontShootTheMessenger() { ... // We've just determined that those threads need to be restarted // Flag those functions to wrap it up pthread_mutex_lock(&amp;wrapUpFlagMutex); wrapUpFlag = true; pthread_mutex_unlock(&amp;wrapUpFlagMutex); // Join the threads, note that this will block pthread_join(thread3, NULL); pthread_join(thread2, NULL); pthread_join(thread1, NULL); // Flag those functions to...not...wrap it up pthread_mutex_lock(&amp;wrapUpFlagMutex); wrapUpFlag = false; pthread_mutex_unlock(&amp;wrapUpFlagMutex); // Launch those puppies again pthread_create(thread1, NULL, first_thread, NULL); ... } </code></pre> <p>Again, this is a mimimalist approach. A more robust method will probably involve conditional variables, a redesign of your calling structure, actually using the passed arguments to the thread functions and return values for <code>pthread_exit()</code>, and much more.</p> <p>Also, depending on your contraints, you may be interested in functionality like <a href="http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_kill.3.html" rel="nofollow">pthread_kill</a>. Note that whatever road you go down, you're not saving yourself any trouble by just hoping that the act of killing off threads somehow cleans things up for you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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