Thread Management Class (complex)
More...
#include <iostream>
#include <stack>
#include <map>
#include <algorithm>
#include <cstring>
#include <pthread.h>
Go to the source code of this file.
Thread Management Class (complex)
- Purpose:
- Example class that manages threads and the detection of their exits (death) through the use of condition variables. Please see the example code and ThreadMgr class documentation for more details.
For more fundamental examples see documentation for files threadDeath1.cc and threadDeath2.cc.
The class ThreadMgr uses a map and a stack from the C++ STL to track thread IDs and their runtime status. While ThreadMgr does add some overhead to basic pthread management (in the form of some memory usage and slightly slower thread creation and destruction) the class offers a relatively easy to use interface to pthreads in general and may be enhanced for better type recognition (i.e. making it a template) and the like.
Definition in file threadDeath3.cc.
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
the main function
- Example 1:
- A slightly overcomplicated example that attempts to cancel a thread. The thread attributes have not been set to be cancelable so milage may vary from system to system. The cancelation routine is not very robust here -this is just for demonstratio purposes to show the kinds of things we might want to do with a class implimentation like that of ThreadMgr.
- Example 2:
- A less complicated implimentation of Example 1. Here we don't cancel any threads.
- Example 3:
- Demonstrates creating a thread that both passes arguments to the user defined function as well as gets something back from the call to condWait (ultimately from pthread_join)
- Note
- we use a different pointer in the third example to provide a cleaner example of returning an object
\warning an attempt to declare void **ret after example 3
yielded a consistant segmentation fault using "g++ (GCC) 3.3.3
20040125" this is considered to be a compler or linker error -I didn't bother trying to figure it out (just be aware that these things happen sometimes).
- Example 4:
- Demonstrates the use of two instances of the ThreadMgr class. While some of the functionality of the class uses static functions, the thread management of each instance stays with the instance. In other words, each instance of ThreadMgr manages it's own private list of threads (the threads are managed seperately).
Definition at line 448 of file threadDeath3.cc.
References ThreadMgr::cancel_thread(), ThreadMgr::condWait(), ThreadMgr::createThread(), myfunc0(), myfunc1(), myfunc2(), myStringFunc(), ThreadMgr::no_threads_terminated(), ThreadMgr::threadsActive(), TwaitStringThreads(), and waitStringThreads().
454 const char *pc =
"987654321";
457 std::string *str =
new std::string(
"a string from main");
460 pthread_t pret, pret2;
470 std::cout <<
"Example 1:" << std::endl;
502 std::cout <<
"CANCELING THREAD:" << pret2 << std::endl;
512 if(*return_val != NULL)
514 std::cout <<
"#######################main:" << ((
char *)(
char *)*return_val) << std::endl;
519 delete (
char *)*return_val;
526 std::cout <<
"\n" <<
"Example 2:" << std::endl;
555 if(*return_val != NULL)
559 std::cout <<
"#######################main:" << ((
char *)(
char *)*return_val) << std::endl;
562 delete ((
char *)(
char *)*return_val);
568 std::cout <<
"\n" <<
"Example 3:" << std::endl;
600 std::cout << *((std::string *)(std::string *)*ret) << std::endl;
603 delete ((std::string *)(std::string *)*ret);
608 std::cout <<
"threads Active:" << m.
threadsActive() << std::endl;
615 std::cout <<
"\n" <<
"Example 4:" << std::endl;
630 std::string *str1 =
new std::string(
"string1 from main");
631 std::string *str2 =
new std::string(
"string2 from main");
void * myfunc2(void *arg)
Example Thread function.
void * myfunc0(void *arg)
Example Thread function.
void * myStringFunc(void *arg)
Example Thread function returning an object.
int cancel_thread(pthread_t *tid)
cancel a thread
void TwaitStringThreads(ThreadMgr *mgr, T return_value)
Example template -waits for thread and prints object.
int threadsActive()
return the number of active threads
pthread_t createThread(void *(*thread_func)(void *), void *arg)
attempt to create a new thread and register it
A basic thread management class.
bool no_threads_terminated()
answers the question "are there no theads terminated?"
void * myfunc1(void *arg)
Example Thread function.
void waitStringThreads(ThreadMgr *mgr, void **return_value)
generic wait for string-centric threads
int condWait(void **thread_return_val)
wait on a condition variable for a thread to terminate
void * myfunc0 |
( |
void * |
arg | ) |
|
Example Thread function.
Simply counts and returns.
- Returns
- NULL is returned
Definition at line 781 of file threadDeath3.cc.
Referenced by main().
783 std::cout <<
"got here: myfunc0:BEGIN" << std::endl;
785 for(
int i=10000; i > 0; i--)
786 for(
int i=10000; i > 0; i--);
788 std::cout <<
"got here: myfunc0:END" << std::endl;
void * myfunc1 |
( |
void * |
arg | ) |
|
Example Thread function.
Simply counts and returns in less time than myfunc0
- Returns
- NULL is returned
Definition at line 798 of file threadDeath3.cc.
Referenced by main().
800 std::cout <<
"got here: myfunc1:BEGIN" << std::endl;
802 for(
int i=1000; i > 0; i--)
803 for(
int i=10000; i > 0; i--);
805 std::cout <<
"got here: myfunc1:END" << std::endl;
void * myfunc2 |
( |
void * |
arg | ) |
|
Example Thread function.
- Purpose:
- Allocate some memory and add data to the memory area. Then wait for some amount of time (in a for loop) and return a pointer to the allocated memory.
- Parameters
-
- Returns
- a char * cast to a void *
- Returns
- a pointer to the new memory memory area that was populated earlier
Definition at line 819 of file threadDeath3.cc.
Referenced by main().
821 std::cout <<
"got here: myfunc2:BEGIN" << std::endl;
824 std::cout <<
"|arg = " << (
char *)arg << std::endl;
827 char *tmp =
new char[10];
828 char x[10] = {
"123456789"};
833 std::cout <<
"myfunc2:" << tmp << std::endl;
835 for(
int i=10000; i > 0; i--)
836 for(
int i=10000; i > 0; i--);
838 std::cout <<
"got here: myfunc2:END" << std::endl;
void * myStringFunc |
( |
void * |
arg | ) |
|
Example Thread function returning an object.
- Purpose:
- Print the parameter from main, allocate memory and populate the new memory with data, and return a pointer to the new memory area (of type string pointer cast to a void *).
- Parameters
-
- Returns
- a string pointer cast to a void pointer
Definition at line 758 of file threadDeath3.cc.
Referenced by main().
765 std::cout <<
"myStringFunc printing:" << *(std::string *)arg << std::endl;
768 std::string *s =
new std::string(
"a string from myStringFunc");
769 std::cout <<
"myStringFunc returning:" << *s << std::endl;
template<class T >
void TwaitStringThreads |
( |
ThreadMgr * |
mgr, |
|
|
T |
return_value |
|
) |
| |
Example template -waits for thread and prints object.
- Purpose:
- Example template that waits for a thread and prints out the string that is the return value from the condWait. This is here for example only and EXPECTS that the Type is something printable.
- Parameters
-
mgr | A ThreadMgr pointer |
return_value | of type cast by calling function (see main for more details) |
- Returns
- nothing
- Note
- a call to this function:
TwaitStringThreads(p_mgr, (string **)ret);
Yields ret to be a (string **). When we want to send it to cout then we must cast it bacwards to get the contents of the pointer of the pointer (which is a string pointer in this case). Get it?
Definition at line 723 of file threadDeath3.cc.
References ThreadMgr::condWait(), and ThreadMgr::threadsActive().
Referenced by main().
728 mgr->
condWait((
void **)return_value);
730 if(*return_value != NULL)
740 std::cout <<
"TwaitStringThreads:" << **((T)return_value) << std::endl;
743 delete *return_value;
int threadsActive()
return the number of active threads
int condWait(void **thread_return_val)
wait on a condition variable for a thread to terminate
void waitStringThreads |
( |
ThreadMgr * |
mgr, |
|
|
void ** |
return_value |
|
) |
| |
generic wait for string-centric threads
wait for a managed thread to return
- Purpose:
- Wait for a thread managed by the ThreadMgr pointer to return.
- Parameters
-
mgr | A ThreadMgr class pointer |
return_value | (must be from the function that calls createThread) This is a detail of how the staticness of the return value from the pthread_join is implimented -I think. |
- Note
- This function could easily be made into a template. We're just doing this for now for demonstration purposes. Something along the lines of the following might do the trick:
template <class T> void waitStringThreads(ThreadMgr *mgr, T **return_value);
(Of coarse, then you would have to cast your pointer from the calling function or something like that, but that's probably not that big of a deal.)
- Warning
- : Due to stack undwinding, declaring the void** for the return value in this scope will simply not work. The return value pointer MUST be passed in or you will get a segmentation fault.
Definition at line 656 of file threadDeath3.cc.
References ThreadMgr::condWait(), and ThreadMgr::threadsActive().
Referenced by main().
691 mgr->
condWait((
void **)return_value);
693 if(*return_value != NULL)
696 std::cout <<
"waitStringThreads:" << *((std::string *)(std::string *)*return_value) << std::endl;
699 delete ((std::string *)(std::string *)*return_value);
int threadsActive()
return the number of active threads
int condWait(void **thread_return_val)
wait on a condition variable for a thread to terminate