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

basic thread functionality More...

#include <pthread.h>
#include <iostream>
#include <cstring>
Include dependency graph for threadDeath1.cc:

Go to the source code of this file.

Macros

#define CHAR_EXAMPLE
 make this a define to use the char * example More...
 

Functions

void * myfunc (void *arg)
 user defined function to call More...
 
void * func (void *arg)
 a function that calls the user defined function More...
 
int main (int argc, char *argv[])
 

Detailed Description

basic thread functionality

Purpose:
Example program 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 a char* or a string* depending on whether CHAR_EXAMPLE is defined within this file (see below).

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 threadDeath1.cc.

Macro Definition Documentation

#define CHAR_EXAMPLE

make this a define to use the char * example

Definition at line 24 of file threadDeath1.cc.

Function Documentation

void* func ( void *  arg)

a function that calls the user defined function

See myfunc for more details. This function does, however use pthread_exit() to demonstrate the passing of the return_value pointer back through pthread_join() in main

Definition at line 69 of file threadDeath1.cc.

References myfunc().

Referenced by main().

70 {
77  void *return_value = myfunc(arg);
78  pthread_exit(return_value);
79 }
void * myfunc(void *arg)
user defined function to call
Definition: threadDeath1.cc:28

Here is the call graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 81 of file threadDeath1.cc.

References func(), and myfunc().

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 }
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

Here is the call graph for this function:

void * myfunc ( void *  arg)

user defined function to call

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
Returns
some object from the heap
Parameters
someobject
Note
this function assumes that arg is a string pointer passed in from main -for demonstration purposes
 \warning do NOT call pthread_exit from here! This function will

behave the same if it is called directly or not and a returning normally does not affect the return_value parameter of a pthread_join().

Definition at line 28 of file threadDeath1.cc.

Referenced by func(), and main().

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 }