Skip to content

Commit

Permalink
Fix race on thread creation
Browse files Browse the repository at this point in the history
This often broke the threading tests on OSX.
  • Loading branch information
ShadowNinja committed Apr 28, 2016
1 parent e416738 commit 46fd114
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
12 changes: 1 addition & 11 deletions src/threading/thread.cpp
Expand Up @@ -116,9 +116,7 @@ bool Thread::start()
#if USE_CPP11_THREADS

try {
m_thread_obj = new std::thread(threadProc, this);
m_thread_id = m_thread_obj->get_id();
m_thread_handle = m_thread_obj->native_handle();
m_thread_obj = new std::thread(threadProc, this);
} catch (const std::system_error &e) {
return false;
}
Expand All @@ -135,8 +133,6 @@ bool Thread::start()
if (status)
return false;

m_thread_id = m_thread_handle;

#endif

while (!m_running)
Expand Down Expand Up @@ -234,12 +230,6 @@ bool Thread::getReturnValue(void **ret)
}


bool Thread::isCurrentThread()
{
return thr_is_current_thread(m_thread_id);
}


#if USE_CPP11_THREADS || USE_POSIX_THREADS
void *Thread::threadProc(void *param)
#elif defined(_WIN32_WCE)
Expand Down
18 changes: 16 additions & 2 deletions src/threading/thread.h
Expand Up @@ -90,12 +90,22 @@ class Thread {
/*
* Returns true if the calling thread is this Thread object.
*/
bool isCurrentThread();
bool isCurrentThread() { return thr_is_current_thread(getThreadId()); }

inline bool isRunning() { return m_running; }
inline bool stopRequested() { return m_request_stop; }

#if USE_CPP11_THREADS
inline threadid_t getThreadId() { return m_thread_obj->get_id(); }
inline threadhandle_t getThreadHandle() { return m_thread_obj->native_handle(); }
#else
# if USE_WIN_THREADS
inline threadid_t getThreadId() { return m_thread_id; }
# else
inline threadid_t getThreadId() { return m_thread_handle; }
# endif
inline threadhandle_t getThreadHandle() { return m_thread_handle; }
#endif

/*
* Gets the thread return value.
Expand Down Expand Up @@ -147,8 +157,12 @@ class Thread {
Atomic<bool> m_running;
Mutex m_mutex;

threadid_t m_thread_id;
#if !USE_CPP11_THREADS
threadhandle_t m_thread_handle;
#if _WIN32

This comment has been minimized.

Copy link
@kwolekr

kwolekr Apr 28, 2016

Contributor

I wish this was indented...

threadid_t m_thread_id;
#endif
#endif

static ThreadStartFunc threadProc;

Expand Down

0 comments on commit 46fd114

Please sign in to comment.