basic thread functionality
More...
#include <pthread.h>
#include <iostream>
#include <cstring>
Go to the source code of this file.
|
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[]) |
|
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.
make this a define to use the char * example
Definition at line 24 of file threadDeath1.cc.
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().
77 void *return_value =
myfunc(arg);
78 pthread_exit(return_value);
void * myfunc(void *arg)
user defined function to call
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Definition at line 81 of file threadDeath1.cc.
References func(), and myfunc().
90 string arg(
"string from main");
94 cout <<
"example 1:" << endl;
98 pthread_create(&tid, (pthread_attr_t *)NULL,
func, (
void *)&arg);
101 pthread_join(tid, ret);
105 char *pc = ((
char *)(
char *)*ret);
110 string *ps = ((
string *)(
string *)*ret);
122 cout <<
"\n" <<
"example 2:" << endl;
126 pthread_create(&tid, (pthread_attr_t *)NULL,
myfunc, (
void *)&arg);
129 pthread_join(tid, ret);
133 cout << ((
char *)(
char *)*ret) << endl;
134 delete ((
char *)(
char *)*ret);
137 cout << *((
string *)(
string *)*ret) << endl;
138 delete ((
string *)(
string *)*ret);
void * func(void *arg)
a function that calls the user defined function
void * myfunc(void *arg)
user defined function to call
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
-
arg | is a string object pointer passed in as a void * |
- Returns
- a string object that is dynamically allocated
- Returns
- some object from the heap
- Parameters
-
- 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().
38 cout << *(
string *)arg << endl;
42 char *c =
new char[256];
44 strcpy(c,
"string from myfunc");
45 cout <<
"myfunc returning:" << c <<endl;
58 string *s =
new string(
"string from myfunc");
59 cout <<
"myfunc returning:" << *s <<endl;