在 Visual Studio 2019 中配置 pthread 库的多线程支持,可以按照以下步骤进行:
https://sourceforge.net/projects/pthreads4w/files/pthreads-w32/
下载最新版本的 pthread 库。https://pan.baidu.com/s/13ZSPfFEQyd73TZNivaeUIA?pwd=li3y
提取码:li3y
C:\pthread
。C:\pthread\Pre-built.2\include
。lib
文件所在目录,例如 C:\pthread\Pre-built.2\lib\x64
和C:\pthread\Pre-built.2\lib\x86
。pthreadVC2.lib
和 pthreadVCE2.lib
。#include
。编写多线程代码,例如:
#include
#include
using namespace std;
void* func(void* arg)
{
cout << "Hello from thread!" << endl;
pthread_exit(NULL);
}
int main()
{
pthread_t thread;
int rc = pthread_create(&thread, NULL, func, NULL);
if (rc)
{
cout << "Error:unable to create thread," << rc << endl;
return -1;
}
pthread_join(thread, NULL);
return 0;
}
编译并运行程序,如果一切正常,应该可以看到输出“Hello from thread!”。