Use pthreads in MinGW in Win7

2019-06-24 00:56发布

问题:

2015-1-20, I use MinGW in my Win7, try to compile my source code with MinGW and Pthreads. The MinGW has already support POSIX threads, In the "MinGW Installation Manager", we can install the pthreads dev package and pthreads lib. But when I compile my source codes, there's an error: struct timespec redefinition, first in pthread.h, then in unistd.h, I don't know why.

Before this, actually I install the minGW without the pthreads dev and lib package, I download the pthreads-for-win32 source code and build it locally. I use -I/somewhere -L/somewhere -lpthreadGC2 to link to my source code, and it worked.

But this time, it fail with MinGW self package. I need help. I cannot upload a picture to show you the details....

回答1:

Actually, I find a solution.

Don't include "unistd.h" after "pthread.h", use local winapi instead.

It's easy to code with "pthreads" with gcc in Linux, but this is not support in Win7, use "MinGW Installation Manager" (google "mingw" and download) to install. Choose the "mingw32-pthreads-win32" dev package in "MinGW Standard Library" when you install the mingw, then you don't need to download pthreads-for-win32 source code or build it locally. But the "pthread.h" will conflict with "unistd.h", in Windows, we should use Windows API instead of unix API.