Can someone please provide an example how one can test if a started thread is still running or is done? With Kind Regards, Ovanes Markarian
I don't think you can do that with the thread lib. If you need to wait until a thread is finished you can use the join(). You can of course keep the state somewhere else. Like: enum teThreadState { BLOCKED = 0, RUNNING, ... } teThreadState eState; void run() throw() { scoped_lock lock(mutex); eState = RUNNING; doSomething(); eState = BLOCKED; while( condition ) cond.wait( lock ); eState = RUNNING; } Christian
Ok, this is probably the easiest way, but I am afraid not the safest one. What if I will access the eState member from the function (running in another thread) while this thread will write it? Is it possible to try to lock an already locked mutex receive an error or exception and know that if the mutex is not lockable another thread is still running, otherwise another thread is done... Would be that an option? If yes, I can't get it to work. On Tue, August 30, 2005 23:52, Christian Henning said:
I don't think you can do that with the thread lib. If you need to wait until a thread is finished you can use the join().
You can of course keep the state somewhere else. Like:
enum teThreadState { BLOCKED = 0, RUNNING, ... }
teThreadState eState;
void run() throw() { scoped_lock lock(mutex);
eState = RUNNING; doSomething();
eState = BLOCKED;
while( condition ) cond.wait( lock );
eState = RUNNING; }
Christian
With Kind Regards, Ovanes Markarian
What if I will access the eState member from the function (running in another thread) while this thread will write it?
Make the access to eState threadsafe, probably with an read_write_mutex an example: class ThreadState { enum teThreadState { BLOCKED=0, RUNNING }; teThreadState getState() throw() { scoped_read_lock oLock(oMutex); return _eState; } void setState( teThreadState eState ) { scoped_write_lock oLock( oMutex ); _eState = eState; } private: teThreadState _eState; boost::read_write_mutex oMutex; };
Is it possible to try to lock an already locked mutex receive an error or exception and know that if the mutex is not lockable another thread is still running, otherwise another thread is done... Would be that an option? If yes, I can't get it to work.
Just encapsulate a Mutex in a separate class and everytime you wont to lock it, check if the state is locked or not. But be careful and make it as threadsafe as possible. The boost thread lib is a very low level library, to ensure portability. That's the reason why there are so many basic functionality is missing. But it's working and does what it's supposed to. Greets, Christian
participants (2)
-
Christian Henning
-
Ovanes Markarian