Jump to content
  • Advertisement
Sign in to follow this  
KaiserJohan

A thread class question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm doing a wrapper around win32/pthread threads. I've had a look at the C++11 threads (http://en.cppreference.com/w/cpp/thread) and I'm aiming for something similiar.

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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!