c - pthread_join causes segmentation error (simple program) -


i trying work multi-threaded programs, having problems pthread_join function. code below simple program using show pthread_join crashing. output code be:

before create  child thread  after create  segmentation fault (core dumped) 

what causes pthread_join give segmentation fault?

#include <pthread.h> #include <stdio.h>  void * dostuff() {     printf("child thread\n");     return null; }  int main() {     pthread_t p1;      printf("before create\n");     pthread_create(&p1, null, dostuff(), null);     printf("after create\n");      pthread_join(p1, null);     printf("joined\n");      return 0; } 

because in call pthread_create call function, , returns null pthread_create fail. not initialize p1 , (probably) cause undefined behavior in pthread_join call.

to fix pass function pointer pthread_create call, not call it:

pthread_create(&p1, null, dostuff, null); /* no parantehsis --------^^^^^^^ */ 

this should teach check return values of function calls, pthread_create return non-zero on failure.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -