Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From <a href="http://opengroup.org/onlinepubs/007908799/xsh/pthread_join.html" rel="nofollow noreferrer">opengroup.org on pthread_join</a>:</p> <blockquote> <p>The results of multiple simultaneous calls to pthread_join() specifying the same target thread are undefined.</p> </blockquote> <p>So, you really should not have several threads joining your previous insertThread.</p> <p>First, as you use C++, I recommend <a href="http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html" rel="nofollow noreferrer">boost.thread</a>. They resemble the POSIX model of threads, and also work on Windows. And it helps you with C++, i.e. by making function-objects usable more easily.</p> <p>Second, why do you want to start a new thread for inserting an element, when you always have to wait for the previous one to finish before you start the next one? Seems not to be classical use of multiple-threads.</p> <p>Although... One classical solution to this would be to have one worker-thread getting jobs from an event-queue, and other threads posting the operation onto the event-queue. </p> <p>If you really just want to keep it more or less the way you have it now, you'd have to do this:</p> <ul> <li>Create a condition variable, like <code>insert_finished</code>.</li> <li>All the threads which want to do an insert, wait on the condition variable.</li> <li>As soon as one thread is done with its insertion, it fires the condition variable.</li> <li>As the condition variable requires a mutex, you can just notify <em>all</em> waiting threads, they all want start inserting, but as only one thread can acquire the mutex at a time, all threads will do the insert sequentially.</li> </ul> <p>But you should take care that your synchronization is not implemented in a too ad-hoc way. As this is called <code>insert</code>, I suspect you want to manipulate a data-structure, so you probably want to implement a thread-safe data-structure first, instead of sharing the synchronization between data-structure-accesses and all clients. I also suspect that there will be more operations then just <code>insert</code>, which will need proper synchronization...</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