windows 和 linux 多线程库是各自的,为了统一,在windows vs2010 安装 POSIX Threads
1.准备:pthreads-w32-2-9-1-release.zip
先进 https://sourceware.org/pthreads-win32/ 和 ftp://sourceware.org/pub/pthreads-win32 ,在下载pthreads-w32-2-9-1-release.zip 会报:
Not Found
The requested URL /pub/pthreads-w32-2-9-1-release.zip was not found on this server.
Apache Server at sourceware.org Port 443
已经不可以了
正确下载路径:https://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip 2019-04-14号还能用
2.然后就是配置工程依赖:因为股票软件依赖64位mysql,工程也是64位的,因此这边依赖的pthread也是64位的
a.项目->属性->配置属性->C/C++->常规->附加包含目录,包含目录里添加inlude路径:D:\Documents\Downloads\pthreads-w32-2-9-1-release\Pre-built.2\include
b.项目->属性->配置属性->链接器->常规->附加库目录,路径:D:\Documents\Downloads\pthreads-w32-2-9-1-release\Pre-built.2\lib\x64
c.项目->属性->配置属性->链接器->输入->附加依赖项,添加:pthreadVC2.lib
3. 此时还要把将pthreadVC2.dll放到和exe同样的目录下或者直接放到system32目录
不然执行会报下面的错误:
4.写测试代码,此时其他特殊说明都不需要了:
比如:#pragma comment(lib, "pthreadVC2.lib") ,因为已经被添加到依赖项了
具体代码:
#include <stdio.h> #include <pthread.h> #include <assert.h> //#pragma comment(lib, "pthreadVC2.lib") void* Function_t(void* Param); int main(int argc, char* argv[]) { pthread_t pid; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_create(&pid, &attr, Function_t, NULL); printf("====\n"); getchar(); pthread_attr_destroy(&attr); return 0; } void* Function_t(void* Param) { printf("Thread Starts.\n"); pthread_t myid = pthread_self(); printf("Thread ID=%d ", myid); return NULL; }
执行效果:
至此 即可开启 windows下 pthread 多线程编程
写代码的小熊猫,:)~