Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You ask...</p> <blockquote> <p>Is the fact that Manager spawns a new thread in its constructor considered a bad practice?</p> </blockquote> <p>In most cases, RAII is good enough to approach the object creation and resource acquisition. In some cases you may want to achieve the deferred resource initialization: when you first construct an object and later you proceed with the initialization. This can be achieved, for example, via a ctor (either default or parameterized) and open/start routines. Though you may also do it in the ctor and achieve the deffered object creation by allocating the object in the process heap (via operator new). It depends on your requirements, software design considerations and corporate software development standards. So, you may create a thread in ctor, or may want or need to spawn it in the later stage of the application/object lifecycle.</p> <blockquote> <p>What about the pthread_exit</p> </blockquote> <p>It is not required. It terminates the calling thread, making its exit status available to any waiting threads (i.e. via pthread_join()). An implicit call to pthread_exit() occurs when any thread returns from its start routine. Basically, the pthread_exit() function provides an interface similar to exit() but on a per-thread basis (including cancelation cleanup handlers). But beware of calling pthread_exit() from cancelation cleanup handlers or from destructors of objects allocated in the TSD (thread-specific data area) - it can lead to undesirable side effects.</p> <blockquote> <p>About the constructor - can i destroy the thread attr object (workerSettings) immediately after spawning the thread or does it have to stay valid for the entire lifetime of the thread?</p> </blockquote> <p>Yes, you can destroy it right away: it will not affect already created threads.</p> <blockquote> <p>About the destructor: Is this the right way to do this?</p> </blockquote> <p>Same thing as for ctor: you may use dtor and close/stop routine or can do it all in the dtor: depends on your specific needs (e.g. object reusability etc). Just make the dtor not throw.</p> <blockquote> <p>Do your experienced eyes see any synchronization issues there?</p> </blockquote> <p>I may suggest using pthread_testcancel(), to introduce the explicit cancelation point in a thread, and issue pthread_cancel() + pthread_join() (should return PTHREAD_CANCELED) in the control thread to stop the child thread, instead of synch variable workerRunning. Of course, if it is applicable in your case.</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