由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-lpthread参数:
gcc -o pthread -lpthread XXX.cpp
如果在VScode中,可以直接设置tasks.json中的args参数:
"args": ["-g", "${file}", "-std=c++11", "-lpthread", "-o", "${fileBasenameNoExtension}.out"]
参考:https://blog.csdn.net/suiyueruge1314/article/details/106401791