Various C++ Examples (including IPC)  Version: 1.0.0
threadDeath2.cc File Reference

slightly complex pthread example More...

#include <iostream>
#include <string>
#include <pthread.h>
Include dependency graph for threadDeath2.cc:

Go to the source code of this file.

Classes

struct  func_params
 wrapper function parameters/arguments More...
 

Functions

void * myfunc (void *arg)
 user defined function More...
 
void * func (void *arg)
 A wrapper function for the user defined function. More...
 
int main (int argc, char *argv[])
 

Detailed Description

slightly complex pthread example

Purpose:
Additional Example program to threadDeath1.cc that creates a thread that uses a function that calls a user defined function. Also demonstrates casing examples for the pthread_join(void **) return type. Often the return type from pthread_join() is confusing so we demonstrate only a string * usage here.

This program is slightly more complicated than threadDeath1.cc because we are passing around additional parameters.

The intention here is that we want to demonstrate passing pointers around when creating threads and then accessing those pointers as we go allong.

Definition in file threadDeath2.cc.

Function Documentation

void* func ( void *  arg)

A wrapper function for the user defined function.

Purpose:
This function wrapps the user defined function so that additional opperations may be performed on a thread once it is created. Unified signal handling or thread ID tracking can be performed from this function as well as pthread_cleanup_push() and pthread_cleanup_pop() operations.
Returns
NOTHING calls pthread_exit() to be joined later.
Parameters
argis a void pointer that is expected to point to a func_params object.
Note
This function should never return -it calls pthread_exit() instead

Definition at line 54 of file threadDeath2.cc.

References func_params::argument, func_params::other_info, and func_params::user_func.

Referenced by main().

55 {
56  //grab the parameters for redability (demonstration purposes)
57  struct func_params *params = (struct func_params *)arg;
58 
59  //print out other info from the paramater structure
60  cout << (char *)params->other_info << endl;
61 
62  //call the user function with the intended argument (void *)
63  void *return_value = params->user_func(params->argument);
64 
65  //exit thread
66  pthread_exit(return_value);
67 
71  return NULL;
72 }
void * other_info
Definition: threadDeath2.cc:36
wrapper function parameters/arguments
Definition: threadDeath2.cc:32
void *(* user_func)(void *)
Definition: threadDeath2.cc:34
void * argument
Definition: threadDeath2.cc:35
int main ( int  argc,
char *  argv[] 
)
Purpose:
Demonstrate the use of a wrapper function to call a user defined function from our wrapper function. A pointer to the user defined function is passed to the wrapper function through a func_params object. Casting is used to pass the func_params object through pthread_create to the wrapper function.

The func_params object also contains a pointer to an argument that will be passed on to the user defined function.

Definition at line 86 of file threadDeath2.cc.

References func_params::argument, func(), myfunc(), func_params::other_info, and func_params::user_func.

87 {
88  //track the thread ID
89  pthread_t tid;
90 
91  //capture the return value of our user defined function (ultimately
92  //from a call from pthread_exit in our wrapper function).
93  void **ret;
94 
95  //create a string to pass to our user defined function
96  string arg("string from main");
97 
98  //create a func_params object to pass to our wrapper function
99  struct func_params params;
100 
101  //set the members of the func_params object
102  params.user_func = myfunc; // pointer to user defined function
103  params.argument = &arg; // object passed to user function
104 
105  //more data -for demonstration
106  params.other_info = (void *)"literal string in main params";
107 
108  //create the thread (passing our wrapper function as the function
109  //argument)
110  pthread_create(&tid, (pthread_attr_t *)NULL, func, (void *)&params);
111 
112  //join the thread -simple enough
113  pthread_join(tid, ret);
114 
115  //check the return value
116  if(*ret != NULL)
117  {
118  //if the contents of the pointer to a pointer (ret) is not NULL...
119 
120  //print the contents -casting it as what we expect it to be
121  cout << *((string *)(string *)*ret) << endl;
122 
123  //delete the pointer because we don't want memory leaks
124  delete ((string *)(string *)*ret);
125  }
126 
127  //arg falls out of scope and is handled by the string destructor
128  //after return.
129 
130  //exit normally
131  return(0);
132 }
void * func(void *arg)
A wrapper function for the user defined function.
Definition: threadDeath2.cc:54
wrapper function parameters/arguments
Definition: threadDeath2.cc:32
void *(* user_func)(void *)
Definition: threadDeath2.cc:34
void * myfunc(void *arg)
user defined function

Here is the call graph for this function:

void* myfunc ( void *  arg)

user defined function

Purpose:
A user defined function. This function, for demonstration purposes, dynamically creates a string object that will be returned through a call to pthread_join() later on.
Note
This function SHOULD NOT call pthread_exit() as it is intended, in this case, to return to the wrapper function func(). Calling pthread_exit() from here will terminate the thread and will result in "undefined behavior".
Parameters
argis a string object pointer passed in as a void *
Returns
a string object that is dynamically allocated
Warning
DO NOT call pthread_exit

Definition at line 149 of file threadDeath2.cc.

Referenced by main().

150 {
151  //print out the argument passed in
152  cout << *(string *)arg << endl;
153 
154  //allocate for a new string object
155  string *s = new string("string from myfunc");
156 
157  //display our string: for demonstration
158  cout << "myfunc returning:" << *s <<endl;
159 
161  //pthread_exit((void *)s); // BAD, BAD, BAD
162 
163  //return the object as a void pointer
164  return( (void *)s);
165 }