在使用 pthread_create() 函数在C程序中创建线程时,有时候会出现类似以下的错误
解决方法
- 首先确保 有头文件 pthread.h (#include <pthread.h>)
- 在使用gcc编译程序时,在命令末尾加上
-pthread
,如gcc -o test test.c -pthread
Note
有些人建议在编译程序时加上 -lpthread
。这其实是平台差异,见Compiling Threaded Programs
根据不同的编译器/平台,选择不同的方法。