2 gcc -Wall -Wextra thread_test.c -g -lpthread
10 #define KB(x) (x * 1024)
13 /* we want to call this func from a thread. */
14 int worker_func(int x
, void* y
, int z
) {
25 } worker_func_thread_data
;
27 static void* worker_func_child_thread(void* data
) {
28 worker_func_thread_data
*child_thread_data
= (worker_func_thread_data
*)data
;
29 child_thread_data
->result
= worker_func(child_thread_data
->x
, child_thread_data
->y
, child_thread_data
->z
);
33 /* returns NULL on success, otherwise error message string
34 * if an error happens, the pthread_attr_t member of ti gets
35 * automatically cleaned up. */
36 static const char* worker_func_thread_launcher(size_t stacksize
, worker_func_thread_data
** ti
, int x
, void* y
, int z
) {
37 const char* errmsg
= NULL
;
38 *ti
= calloc(1, sizeof(worker_func_thread_data
));
43 if((errno
= pthread_attr_init(&(*ti
)->attr
))) {
44 errmsg
= "pthread_attr_init";
48 if((errno
= pthread_attr_setstacksize(&(*ti
)->attr
, stacksize
))) {
49 errmsg
= "pthread_attr_setstacksize";
52 if((errno
= pthread_create(&(*ti
)->thread
, &(*ti
)->attr
, worker_func_child_thread
, *ti
))) {
53 errmsg
= "pthread_create";
61 pthread_attr_destroy(&(*ti
)->attr
);
65 static const char* worker_func_wait(int* result
, worker_func_thread_data
** ti
) {
66 const char* errmsg
= NULL
;
68 if((errno
= pthread_join((*ti
)->thread
, NULL
))) {
69 errmsg
= "pthread_join";
70 pthread_attr_destroy(&(*ti
)->attr
);
73 if((errno
= pthread_attr_destroy(&(*ti
)->attr
))) {
74 errmsg
= "pthread_attr_destroy";
76 *result
= (*ti
)->result
;
84 worker_func_thread_data
* child
;
87 if((errmsg
= worker_func_thread_launcher(KB(128), &child
, 0, NULL
, 1))) goto pt_err
;
88 if((errmsg
= worker_func_wait(&result
, &child
))) goto pt_err
;
90 printf("workerfunc returned %d\n", result
);