Various C++ Examples (including IPC)  Version: 1.0.0
threadDeath2.cc
Go to the documentation of this file.
1 
17 #include <iostream>
18 #include <string>
19 #include <pthread.h>
20 
21 using namespace std;
22 
24 void *myfunc(void *arg);
25 
33 {
34  void *(*user_func)(void *);
35  void *argument;
36  void *other_info;
37 };
38 
54 void *func(void *arg)
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 }
73 
86 int main(int argc, char *argv[])
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 }
133 
149 void *myfunc(void *arg)
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 }
void * other_info
Definition: threadDeath2.cc:36
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
int main(int argc, char *argv[])
Definition: threadDeath2.cc:86
void * argument
Definition: threadDeath2.cc:35
void * myfunc(void *arg)
user defined function