Pthread_join() : resources collector : whaiting multiple threads to exit?

As alexey previously said, the thread function can allocate and free its resources. You can also use pthread_cleanup_push and pop if you want a method that can handle cancelation.

As alexey previously said, the thread function can allocate and free its resources. You can also use pthread_cleanup_push and pop if you want a method that can handle cancelation. You can use a global flag that tells you if a thread is still active or not.

Set it to 1 before creating the thread and back to 0 when the thread function exits. If you have lots of threads, use an array. Simply test the variable to see if the thread is done.

Hopefully your collector thread doesn't continuously spin, which would consume resources. Creating threads is slow. You probably want to keep a handful of threads around permanently and let them pick work to do from a queue.

When there is no work, they should wait for a signal. When work is added to the queue you could signal as many threads as you wanted to wake up and start working again. This is 1000x faster than creating a thread for each piece of work and destroying the thread when the work is done.

Message edited to be more specific. – DevDz Apr 13 at 22:45 So why can't you detach the threads and periodically check a global as I describe above? You don't use pthread_join at all.

– johnnycrash Apr 14 at 0:03.

I would recommend to do the necessary cleanup in exiting threads, not in a special collector thread. Specifically, thread-local data created with pthread_key_create() may have an associated destructor call invoked when a thread exits. This destructor could release the application resource(s) associated with the thread; the easiest way is to store a pointer to the resource(s) in the thread-specific slot (see pthread_set_specific()) in which case the pointer will be automatically sent to the destructor.

The collector thread can still call pthread_join() if threads are created and destroyed all the time; though alternatively you can create the thread in detached state, or call pthread_detach() after creation. Also look at a similar question asked earlier.

The number of -client- threads must be more then the available servers (resources)?.... – DevDz Apr 13 at 22:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions