c++ pthread库使用

c++ pthread库使用

  • 1. pthread库安装
  • 2. 测试demo
  • 3. 配置include/lib路径
    • 3.1 添加include路径
    • 3.2 配置lib文件路径
  • 4.显示结果
  • 5. 遇到的bug
  • 参考文献

1. pthread库安装

打开ftp://sourceware.org/pub/pthreads-win32,下载一个安装包,如pthreads-w32-2-8-0-release.exe
c++ pthread库使用_第1张图片
执行exe,解压到自定义文件夹。

2. 测试demo

#include 
#include 
using namespace std;
//#pragma comment(lib, "pthreadVC2.lib")

void* thread(void* a)
{
	for (int i = 0; i < 30; i++)
	{
		printf("线程执行第 %d 次\n", i + 1);
	}
	return NULL;
}

int main()
{
	pthread_t id;
	int ret = pthread_create(&id, NULL, thread, NULL);
	if (ret != 0)
	{
		cout << "线程创建错误!" << endl;
		exit(-1);
	}
	for (int i = 0; i < 30; i++)
	{
		printf("main函数执行第 %d 次\n", i + 1);
	}
	pthread_join(id, NULL);
	return 0;
}

3. 配置include/lib路径

3.1 添加include路径

c++ pthread库使用_第2张图片

3.2 配置lib文件路径

c++ pthread库使用_第3张图片
c++ pthread库使用_第4张图片

4.显示结果

c++ pthread库使用_第5张图片

5. 遇到的bug

pthread 无法解析的外部符号 __imp_pthread_join

由于pthread库为32位,把平台从X64改为X86重新配置路径。
在这里插入图片描述

参考文献

[1] ftp://sourceware.org/pub/pthreads-win32
[2] windows 下使用pthread 原创

你可能感兴趣的:(c++,c++,pthread)