Home > Uncategorized > Class Wrapper #1: Thread (and Mutex)

Class Wrapper #1: Thread (and Mutex)

In keeping with my promise, here is the first bit of my C/C++ library I felt like sharing.  This object oriented wrapper abstracts the POSIX Thread C API.  The library is mighty useful, exceptionally powerful, and devastatingly troublesome to debug if you’re not careful about it.  This post is not intended as a full reference or tutorial on the pthreads library, nor does my wrapper attempt to encompass all functionality possible with the library.  I’ve merely packaged the basic threading functionality into a couple of classes, and slapped my name on it.

If you have absolutely zero clue what threading is, I recommend you follow the above link.  Maybe with some basic Google or Wikipedia searches to supplement it, and bootstrap your knowledge.  From 30,000 feet, threading effectively allows your program to run in two (or more) different places at once.

That having been said, the rest of the post assumes some working knowledge of threading.  The concept I’ve designed with my Thread class is twofold.  First, it eases passing arguments into, and retrieving return values from, threads.  Secondly, I’ve built the class such that each Thread can be pseudo self-aware.  By that I mean, you can inspect a Thread object to see whether or not there is an underlying pthread running for it, and test whether that underlying code has executed to completion and would be ready to join, without actually join()‘ing the thread (which could cause the parent thread to block, if the child thread wasn’t done yet).

Thread awareness is easily acheived, at the cost of one layer of indirection.  The programmer must specify a function pointer (which is where the newly created thread will begin execution) and an argument (as a void*).  The void* that gets passed into the starting thread function though, is NOT the value you specified, but rather a pointer back to the Thread object.  With a pointer to the Thread object, the argument you actually want can be accessed via the method Thread::argument() as a void*.  This abstraction is useful, because with a pointer to the Thread object, you can relay to the parent thread status information, send and receive messages and other data, and so on.  In particular, just before the “return” of the child thread, you can make a call similar to

thread->complete( true );

Which is of course a variable visible to the parent thread.  This allows you to check the status of child threads, like so,

if ( processing_thread->complete() ) {
...
}

without any potentially blocking operations.  I’ve found this technique to be highly useful in applications where it is desirable to maintain a continuous pool of threads, and constant blocking on pthread_join() can really be a lag.

Header file: thread.h

Implementation file: thread.cpp

Test driver / example program: thread_main.cpp

Note: Be sure to add the “-lpthread” linker directive when compiling.  At least, that’s what it takes on my particular system (Fedora 10 on an i686).

Update: Added a Read/Write Mutex in addition to the standard Mutex object.

  1. No comments yet.