From 9a602d38035972e6aabfb3bb4da2b1ee53f9f7be Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 6 Apr 2012 18:57:05 +0000 Subject: Threads can tell you their own ids now, and they can also report that they failed to start. --- src/stable/thread.cpp | 7 ++++++- src/stable/thread.h | 9 ++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/stable/thread.cpp b/src/stable/thread.cpp index 0d0b768..9cf223e 100644 --- a/src/stable/thread.cpp +++ b/src/stable/thread.cpp @@ -9,6 +9,8 @@ #include "bu/config.h" +namespace Bu { subExceptionDef( ThreadException ); } + Bu::ThreadId::ThreadId( pthread_t tId ) : tId( tId ) { @@ -43,7 +45,10 @@ Bu::ThreadId Bu::Thread::currentThread() bool Bu::Thread::start() { - nHandle = pthread_create( &ptHandle, NULL, threadRunner, this ); + if( pthread_create( &ptHandle, NULL, threadRunner, this ) ) + { + throw Bu::ThreadException("Could not start thread."); + } return true; } diff --git a/src/stable/thread.h b/src/stable/thread.h index f4b961f..0a0d8bb 100644 --- a/src/stable/thread.h +++ b/src/stable/thread.h @@ -10,8 +10,11 @@ #include +#include "bu/exceptionbase.h" + namespace Bu { + subExceptionDecl( ThreadException ); class ThreadId { friend class Thread; @@ -49,6 +52,7 @@ namespace Bu virtual ~Thread(); static ThreadId currentThread(); + ThreadId getId() { return ThreadId( ptHandle ); } /** * Begin thread execution. This will call the overridden run function, @@ -93,12 +97,11 @@ namespace Bu private: pthread_t ptHandle; /**< Internal handle to the posix thread. */ - int nHandle; /**< Numeric handle to the posix thread. */ protected: /** - * The workhorse of the Thread class. This is the function that will run - * in the thread, when this function exits the thread dies and is + * The workhorse of the Thread class. This is the function that will + * run in the thread, when this function exits the thread dies and is * cleaned up by the system. Make sure to read up on ThreadMutex, * ThreadCondition, and cancel to see how to control and protect * everything you do in a safe way within this function. -- cgit v1.2.3