66 void *(*func)(
void *);
87 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
91 static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
95 static pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;
132 ret = pthread_cancel(*tid);
209 arguments->
func = thread_func;
222 ret_val = pthread_create(&tid, (pthread_attr_t *) NULL,
func, (
void *)arguments);
227 addID(tid, arguments);
233 std::cout <<
"pthread_create FAIL" << std::endl;
244 int ret =
m_ids.size();
289 cancel_arg->
func = NULL;
290 cancel_arg->
arg = NULL;
304 pthread_cleanup_pop(1);
310 pthread_exit(tmpArg);
320 pthread_mutex_lock(arg->
m_mutex);
329 pthread_mutex_unlock(arg->
m_mutex);
363 if( (ret = pthread_join(tempID, return_val)) == 0)
375 std::cout <<
"pthread_join() = " << ret << std::endl;
392 m_ids.insert(std::make_pair(
id, arg));
419 std::map<pthread_t, ThreadMgr::func_arguments *>
m_ids;
448 int main(
int argc,
char *argv[])
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");
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);
728 mgr->
condWait((
void **)return_value);
730 if(*return_value != NULL)
740 std::cout <<
"TwaitStringThreads:" << **((T)return_value) << std::endl;
743 delete *return_value;
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;
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;
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;
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 * arg
user arguments for user function
ThreadMgr * thisObject
the this object -one per thread...EEEEK!
static void * func(void *arg)
internal thread function
void * myfunc2(void *arg)
Example Thread function.
void * myfunc0(void *arg)
Example Thread function.
void addID(pthread_t id, func_arguments *arg)
add a thread id to the id vector
void * myStringFunc(void *arg)
Example Thread function returning an object.
int removeTerminated(void **return_val)
remove a terminated thread LIFO
int cancel_thread(pthread_t *tid)
cancel a thread
pthread_cond_t * m_cond_var
condition variable
void TwaitStringThreads(ThreadMgr *mgr, T return_value)
Example template -waits for thread and prints object.
int threadsActive()
return the number of active threads
static void * addTerminated(ThreadMgr *arg)
add a thread id to the m_terminated stack
std::map< pthread_t, ThreadMgr::func_arguments * > m_ids
map of all thread ids and function attributes
static void shutdown_thread(void *arg)
shutdown a thread from pthread_cleanup_pop().
pthread_t createThread(void *(*thread_func)(void *), void *arg)
attempt to create a new thread and register it
pthread_mutex_t * m_mutex
mutex for data access (m_ids, m_terminated)
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 main(int argc, char *argv[])
the main function
void *(* func)(void *)
pointer to user defined function
structure for static wrapper function arguments
pthread_mutex_t * m_cond_mutex
mutex for condition variable
int condWait(void **thread_return_val)
wait on a condition variable for a thread to terminate
std::stack< pthread_t > m_terminated
stack of terminated thread ids
void(* cancel_func)(void *)
function to call when we are canceled