Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions inst/include/tthread/tinythread.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ freely, subject to the following restrictions:
#include <signal.h>
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdlib.h>
#endif

// Generic includes
Expand Down Expand Up @@ -866,12 +866,17 @@ inline void * thread::wrapper_function(void * aArg)
{
// Uncaught exceptions will terminate the application (default behavior
// according to C++11)

std::terminate();
}

// The thread is no longer executing
// Originally it executes on all platforms, but I don't think it's right to
// call with pthreads, so only make it work on Windows
#if defined(_TTHREAD_WIN32_)
lock_guard<mutex> guard(ti->mThread->mDataMutex);
ti->mThread->mNotAThread = true;
#endif

// The thread is responsible for freeing the startup information
delete ti;
Expand Down Expand Up @@ -912,14 +917,18 @@ inline thread::thread(void (*aFunction)(void *), void * aArg)

inline thread::~thread()
{
#if defined(_TTHREAD_WIN32_)
if(joinable())
std::terminate();
#elif defined(_TTHREAD_POSIX_)
join();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The C++ standard says (https://en.cppreference.com/w/cpp/thread/thread/~thread):

If *this has an associated thread (joinable() == true), std::terminate() is called.

so I think the behavior here in tinythread was intended to conform with that. In other words, I don't think the destructor should try to join / attach here.

detach();
#endif
}

inline void thread::join()
{
if(joinable())
{
if( joinable() ) {
#if defined(_TTHREAD_WIN32_)
WaitForSingleObject(mHandle, INFINITE);
CloseHandle(mHandle);
Expand All @@ -939,17 +948,23 @@ inline bool thread::joinable() const

inline void thread::detach()
{
bool isDetachable = false;
mDataMutex.lock();
if(!mNotAThread)
{
mNotAThread = true;
isDetachable = true;
}
mDataMutex.unlock();

if(isDetachable)
{
#if defined(_TTHREAD_WIN32_)
CloseHandle(mHandle);
#elif defined(_TTHREAD_POSIX_)
pthread_detach(mHandle);
#endif
mNotAThread = true;
}
mDataMutex.unlock();
}

inline thread::id thread::get_id() const
Expand Down