简体   繁体   中英

How to set up pthreads on windows?

I found an implementation for pthreads on Windows here , but I couldn't get it to work right. Can anyone help me to install pthreads ? Like where to put DLLs, .lib, and .h files?

Also, as an environment I'm not using Visual Studio, but Codeblocks with Mingw.

I usually develop on Linux, but this project has to be on Windows, and I've already got some code implemented using pthreads, so I don't want to use Windows Threads from 'windows.h'.

The .dll can go in any directory listed in your PATH environment variable.

The .lib file can go in any directory listed in your LIB environment variable.

The .h files can go in any directory listed in your INCLUDE environment variable.

Also see the FAQs page of the link you shared .

Read Q6, Q7, Q8.

For Visual C++ Users (not MingW), follow this steps:

1) download " ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip "

2) in the extraction folder, open the subfolder "Pre-built.2" and you should see the following files:

  • "Pre-built.2\\dll\\x64\\pthreadVC2.dll" OR "Pre-built.2\\dll\\x86\\pthreadVC2.dll"
  • "Pre-built.2\\lib\\x64\\pthreadVC2.lib" OR "Pre-built.2\\lib\\x86\\pthreadVC2.lib"
  • "Pre-built.2\\include\\pthread.h"
  • "Pre-built.2\\include\\sched.h"
  • "Pre-built.2\\include\\semaphore.h"

3) For Visual Studio C++ x64 projects, go to project properties and add the following paths accordingly:

  • Additional lib Files add "yourpath\\Pre-built.2\\lib\\x64\\"
  • Additional dll files add "yourpath\\Pre-built.2\\dll\\x64\\"
  • Additional include files add "yourpath\\Pre-built.2\\include\\"

Choose the correct files according to your project build (x64 or x86).

To install gcc in MSYS2 on Windows:

pacman -Syu gcc

That's it!

By installing gcc in MSYS2, you have automatically set up pthreads as well.

You may want to see an example which proves that pthreads is working .

In short - the exact same code as on Linux works fine if you compile and run inside an MSYS2 terminal. (Not necessarily always true, but true for the example in the link.)

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM