Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Considering your additional requirements posted as comment to Checkers' reply (which is the most straightforward way to do that): </p> <p>I agree that join in DTor is problematic for various reasons. But from that the lifetime of your thread object is unrelated to the lifetime of the OS thread object. </p> <hr> <p>First, you need to <strong>separate the data the thread uses from the thread object itself.</strong> They are distinct entities with distinct lifetime requirements.</p> <p>One approach is to make the data refcounted, and have any thread that wants to access it hold a strong reference to the data. This way, no thread will suddenly grab into the void, but the data will be destroyed as soon as noone touches it anymore.</p> <hr> <p>Second, about the thread object being destroyed when the thread joins:<br> I am not sure if this is a good idea. The thread object is normally a way to query the state of a thread - but with a thread object that dies as soon as the thread finishes, noone can tell you wether the thread finished. </p> <p>Generally, I'd completely decouple the lifetime of the thread object from the lifetime of the OS thread: Destroying your thread object should not affect the thread itself. I see two basic approaches to this:</p> <ol> <li>Thread Handle Object - reference counted again, returned by thread creator, can be released as early as one likes without affecting the OS thread. It would expose methods such as <code>Join</code>, <code>IsFinished</code>, and can give access to the thread shared data. </li> </ol> <p>(If the thread object holds relevant execution state, the threafFunc itself could hold a reference to it, thereby ensuring the instance won't be released before the thread ends)</p> <ol start="2"> <li>Thin Wrapper - You simply create a temporary around an OS thread handle. You could not hold additional state for the thread easily, but it might be just enough to make it work: At any place, you can turn an OS thread handle into an thread object. The majority of communication - e.g. telling the thread to terminate - would be via the shared data. </li> </ol> <hr> <p>For your code example, this means: separate the <code>start()</code> from the <code>svc()</code> </p> <p>You'd roughly work with this API (XxxxPtr could be e.g. boost::shared_ptr):</p> <pre><code>class Thread { public: bool IsFinished(); void Join(); bool TryJoin(long timeout); WorkerPtr GetWorker(); static ThreadPtr Start(WorkerPtr worker); // creates the thread }; class Worker { private: virtual void Svc() = 0; friend class Thread; // so thread can run Svc() } </code></pre> <p>Worker could contain a ThreadPtr itself, giving you a guarantee that the thread object exists during execution of <code>Svc()</code>. If multiple threads are allowed to work on the same data, this would have to be a thread list. Otherwise, <code>Thread::Start</code> would have to reject Workers that are already associated with a thread.</p> <hr> <p><strong>Motivation:</strong> What to do with rogue threads that block?<br> Assuming a thread fails to terminate within time for one reason or another, even though you told it to. You simply have three choices:</p> <ul> <li>Deadlock, your applicaiton hangs. That usually happens if you join in the destructor.</li> <li>Violently terminate the thread. That's potentially a violent termination of the app.</li> <li>Let the thread run to completion on it's own data - you can notify the user, who can <em>safely</em> save &amp; exit. Or you simply let the rogue thread dance on it's own copy of the data (not reference by the main thread anymore) until it completes. </li> </ul>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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