This topic is 2169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

The one thing that confuses me is how could I catch the event of when a win32/pthread exits? I need to update the status of the thread object that represents the running thread.

Here's what I've sketched so far;

 class Thread { public: #if defined _WIN32 || _WIN64 typedef void* ThreadHandle; #else #include <pthread.h> typedef pthread_t ThreadHandle; #endif enum ThreadState { DETACHED = 0, RUNNING, DONE }; Thread(); Thread(void* (*start) (void*), void* arg); ~Thread(); void Sleep(uint32_t milliseconds); void Join(); void SetThreadPriority(int32_t priority); void Detach(); ThreadHandle GetNativeHandle(); ThreadState GetThreadState(); private: ThreadHandle mHandle; ThreadState mState; }; 

Share on other sites
You could let the thread itself set an event or call a callback right before the thread returns. Edited by eppo

Share on other sites
Have the thread maintain a reference to thread state variable (mState), and when the thread finished, via return or exception, modify that reference. You need a smart reference to ensure that the thread will live on even if the thread that created it dies. You'll also need mutual exclusion on the thread state parameter, which could be done by making it atomic.

Share on other sites
What are you hoping to gain from your own library vs. what c++11 or boost.threads give you?

Share on other sites
AFAIK the Thread Handle in win32 can be used in WaitForSingleObject(). If the Thread Terminates the Event returns.
Same works with Processes.
In POSIX-Threads you can use joinable threads but you must then call join() to free the resources.

Share on other sites
Either by calling WaitForSingleObject() with a zero timeout or by setting a flag when the method you use to call the user-supplied functor exits (make sure to use the Interlocked...() functions and be aware that the functor may exit with an exception).

If you're developing with MSVC or GCC, you could just use C++11 threads. GCC 4.7 supports them just fine and you can download Visual Studio 2012 RC which also provides full support for std::thread, std::atomic, std::mutex and std::future. A free Express edition of Visual Studio 2012 has been confirmed and they even decided to still support Windows XP. There's also just::thread, which adds C++11 threads to older compilers for a reasonable price.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631424
• Total Posts
3000008
×