pthreads in C - pthread_exit

前端 未结 9 1380
北荒
北荒 2021-02-05 14:03

For some reason I thought that calling pthread_exit(NULL) at the end of a main function would guarantee that all running threads (at least created in the main funct

9条回答
  •  耶瑟儿~
    2021-02-05 14:37

    pthread_exit(3) exits the thread that calls it (but not the whole process if other threads are still running). In your example other threads use variables on main's stack, thus when main's thread exits and its stack is destroyed they access unmapped memory, thus the segfault.

    Use proper pthread_join(3) technique as suggested by others, or move shared variables into static storage.

提交回复
热议问题