I'm working on some programs which do some massively parallel work, normally though a threadpool (1 thread per processor) which I've implemented on top of CC++. However the main thread of execution really just creates the pool and waits for the collective pool to terminate (either successfully or not), and noticed the Thread class has no methods to wait for termination (blocking or with timeout).
On windows this is fairly easy to deal with, by adding some methods to the CC++ Thread class waitForCompletion(), waitForCompletion(unsigned timeout) which simply call WaitForSingleObject on the thread handle, and are only callable from a different thread context. (I.E. its a programmer error for a thread to call these methods on itself)
When looking at implementing this on the posix side, I'm kind of stumped (mostly by the relative crudeness of pthreads as compared to Win32 threads). Having multiple threads call pthread_join is problematic (and not really allowed), so it seems that some kind of mutex would need to be added to handle this case there. However a mutex would seem to be overkill (plus the added overhead), especially since the call to check it is either blocking or non-blocking, and there is no timeout-based version, so you are stuck polling periodically with pthread_trylock. One might as well use an atomic counter spinlock that sleeps periodically.
Any thoughts?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Let me see if I understand this scenario correctly. You have a pool of "n" threads created at startup. As soon as ALL the threads die, you wish to perform some action. You might want to consider using a conditional or the Event class, where the main thread waits for death termination events to be signaled from the other threads, and then counts down until all the threads have terminated.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think I got it, its just a matter of stepping back from the problem a bit.
The Event is the way to go, I'll just stick an AtomicCounter in the Pool manager class, and in each thread's Final method decrement the counter and signal the Event (also in the pool manager).
Its not a real thread pool per-se, but more of a container of threads to handle the massively parallel run-once work until its 'solved' or 'cancelled due to error'.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Or alternately a rwrite lock can be used. Each pool thread creates a "read" lock which is a shared lock, and the main thread blocks until it can assert a write lock which only occurs after all the pool threads have died....
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm working on some programs which do some massively parallel work, normally though a threadpool (1 thread per processor) which I've implemented on top of CC++. However the main thread of execution really just creates the pool and waits for the collective pool to terminate (either successfully or not), and noticed the Thread class has no methods to wait for termination (blocking or with timeout).
On windows this is fairly easy to deal with, by adding some methods to the CC++ Thread class waitForCompletion(), waitForCompletion(unsigned timeout) which simply call WaitForSingleObject on the thread handle, and are only callable from a different thread context. (I.E. its a programmer error for a thread to call these methods on itself)
When looking at implementing this on the posix side, I'm kind of stumped (mostly by the relative crudeness of pthreads as compared to Win32 threads). Having multiple threads call pthread_join is problematic (and not really allowed), so it seems that some kind of mutex would need to be added to handle this case there. However a mutex would seem to be overkill (plus the added overhead), especially since the call to check it is either blocking or non-blocking, and there is no timeout-based version, so you are stuck polling periodically with pthread_trylock. One might as well use an atomic counter spinlock that sleeps periodically.
Any thoughts?
Let me see if I understand this scenario correctly. You have a pool of "n" threads created at startup. As soon as ALL the threads die, you wish to perform some action. You might want to consider using a conditional or the Event class, where the main thread waits for death termination events to be signaled from the other threads, and then counts down until all the threads have terminated.
I think I got it, its just a matter of stepping back from the problem a bit.
The Event is the way to go, I'll just stick an AtomicCounter in the Pool manager class, and in each thread's Final method decrement the counter and signal the Event (also in the pool manager).
Its not a real thread pool per-se, but more of a container of threads to handle the massively parallel run-once work until its 'solved' or 'cancelled due to error'.
Or alternately a rwrite lock can be used. Each pool thread creates a "read" lock which is a shared lock, and the main thread blocks until it can assert a write lock which only occurs after all the pool threads have died....