windows pthread 配置

  1. 下载pthread, 下载地址
  2. 项目配置(测试使用绝对路径,最好跟据自己的项目修改成相对路径):
    1. C/C++
      1. 常规->附加包含目录:D:\Project\third_party\pthreads\Pre-built.2\include
      2. 预处理器->预处理器定义:增加 HAVE_STRUCT_TIMESPEC;
    2. 链接器(开发环境为x64,故配置x64的,如果为32位,需要改成x86):
      1. 预处理器->附加库目录:D:\Project\third_party\pthreads\Pre-built.2\lib\x64
      2. 输入->附加依赖项:添加 pthreadVC2.lib;
  3. 测试代码:
    1. #include <iostream>
      #include<pthread.h>
      
      using namespace std;
      #define NUM_THREADS 5
      
      void* say_hello(void* args)
      {
      	cout << "hello wolrd!" << endl;
      	return 0;
      }
      
      int main()
      {
      	pthread_t tids[NUM_THREADS];
      	for (int i = 0; i < NUM_THREADS; ++i)
      	{
      		int ret = pthread_create(&tids[i], NULL, say_hello, NULL);
      		if (ret != 0)
      		{
      			cout << "pthread_create error_code=" << ret << endl;
      		}
      	}
      
      	pthread_exit(NULL);
      
      	return 0;
      }
发布了70 篇原创文章 · 获赞 48 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/fwb330198372/article/details/85626222