windows下vs2017中使用pthread

1号坑:报错无法找到pthread头文件

下载缺失的文件

地址:ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip

解压exe文件

打开下载好的exe文件,点击界面上的extract。位置看你自己喜好,记住就行。

配置

聚焦目标

我们用到的主要是“Pre-built.2”这个文件夹下的三个文件夹,分别是动态链接库、头文件、静态链接库。

include配置

右击项目——>属性——按照图示点选,将include的路径填写到”包含目录那一栏内”

dll配置

1.把dll下的x86文件夹下的两个文件,即pthreadGC2.dll与pthreadVC2.dll拷贝到C:\Windows\SysWOW64下(用于64位程序的运行)
2.把dll下的x64文件夹下的五个文件,拷贝到C:\Windows\System32下(用于32位程序的运行)
3.一定按照上述来配置,否则出问题,你可能会以为我写错了,为什么32的要往64的放,64的反而要往32的放呢?我没写反,反正我就是这么弄的,如果不这么弄,会报应用程序无法正常启动0xc000007b。注意,千万注意啊。

目标达成

不再报错了。

再次感谢尾部贴出的博主链接。

2号坑:windows下玩phtread还可能遇见这个问题: C2011 “timespec”:“struct”类型重定义

windows下玩phtread还可能遇见这个问题: C2011 “timespec”:“struct”类型重定义
https://blog.csdn.net/user11223344abc/article/details/80536809

3号坑:无法解析的外部符号 xxxxxx,该符号在函数 _main 中被引用

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

测试程序


    #include <iostream>
    // 必须的头文件
    #include<pthread.h>

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

    #define NUM_THREADS 5

     //线程的运行函数
    void* say_hello(void* args)
    {
        cout << "hello runoob!" << endl;
        return 0;
    }

    int main()
    {
        // 定义线程的 id 变量,多个变量使用数组
        pthread_t tids[NUM_THREADS];
        for (int i = 0; i < NUM_THREADS; ++i)
        {
            //参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
            int ret = pthread_create(&tids[i], NULL, say_hello, NULL);
            if (ret != 0)
            {
                cout << "pthread_create error: error_code=" << ret << endl;
            }
        }

        //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
        pthread_exit(NULL);

        return 0;
    }

Thanks

https://blog.csdn.net/qianchenglenger/article/details/16907821

猜你喜欢

转载自blog.csdn.net/user11223344abc/article/details/80536280