Various C++ Examples (including IPC)  Version: 1.0.0
threadDeath1.cc
Go to the documentation of this file.
1 
16 #include <pthread.h>
17 #include <iostream>
18 #include <cstring>
19 
20 using namespace std;
21 
22 //##########################################################
24 #define CHAR_EXAMPLE
25 //##########################################################
26 
28 void *myfunc(void *arg)
29 {
34  //print whatever is in arg as a string
38  cout << *(string *)arg << endl;
39 
40 #ifdef CHAR_EXAMPLE
41  //demonstrate a char * return
42  char *c = new char[256];
43  memset(c, 0, 256);
44  strcpy(c,"string from myfunc");
45  cout << "myfunc returning:" << c <<endl;
46 
52  //pthread_exit((void *)c);
53 
54  return( (void *)c);
55 
56 #else
57  //demonstrate a string * return.
58  string *s = new string("string from myfunc");
59  cout << "myfunc returning:" << *s <<endl;
60 
61  /* DO NOT CALL pthread_exit from here */
62  //pthread_exit((void *)s);
63 
64  return( (void *)s);
65 #endif
66 }
67 
69 void *func(void *arg)
70 {
77  void *return_value = myfunc(arg);
78  pthread_exit(return_value);
79 }
80 
81 int main(int argc, char *argv[])
82 {
83  //a thread id
84  pthread_t tid;
85 
86  //return var from a pthread_exit()/pthread_join()
87  void **ret;
88 
89  //a string to pass to myfunc()
90  string arg("string from main");
91 
92  //##########################################################
93  //##########################################################
94  cout << "example 1:" << endl;
95  //##########################################################
96 
97  //call a function tha calls the function
98  pthread_create(&tid, (pthread_attr_t *)NULL, func, (void *)&arg);
99 
100  //join the thread
101  pthread_join(tid, ret);
102 
103 #ifdef CHAR_EXAMPLE
104  //demonstrate a char * example
105  char *pc = ((char *)(char *)*ret);
106  cout << pc << endl;
107  delete pc;
108 #else
109  //demonstrate a string * example
110  string *ps = ((string *)(string *)*ret);
111  cout << *ps << endl;
112  delete (string *)ps;
113 #endif
114 
115  //##########################################################
116  //##########################################################
117  /* Same type of example as above: this one just calls the function
118  directly and uses more direct casting when accesing the return
119  value from the function.
120  */
121  //##########################################################
122  cout << "\n" << "example 2:" << endl;
123  //##########################################################
124 
125  //creat thread
126  pthread_create(&tid, (pthread_attr_t *)NULL, myfunc, (void *)&arg);
127 
128  //join thread
129  pthread_join(tid, ret);
130 
131 #ifdef CHAR_EXAMPLE
132  //access char through casts
133  cout << ((char *)(char *)*ret) << endl;
134  delete ((char *)(char *)*ret);
135 #else
136  //access string through casts
137  cout << *((string *)(string *)*ret) << endl;
138  delete ((string *)(string *)*ret);
139 #endif
140 
141  return 0;
142 }
143 
void * func(void *arg)
a function that calls the user defined function
Definition: threadDeath1.cc:69
void * myfunc(void *arg)
user defined function to call
Definition: threadDeath1.cc:28
int main(int argc, char *argv[])
Definition: threadDeath1.cc:81