6

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'.

tmeloliveira
  • 65
  • 1
  • 1
  • 4

3 Answers3

11

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.

Siddhartha Ghosh
  • 2,628
  • 5
  • 15
  • 25
2

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).

winux
  • 402
  • 3
  • 10
1

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:

Henke
  • 1,466
  • 2
  • 9
  • 22
  • How is pacman here ? – An Ant Feb 25 '21 at 13:17
  • Not sure I understand what you are asking, but the package installer `pacman` here in MSYS2 is "the same" pacman as used in Arch-Linux, except of course it is the MSYS2 version of it. – Henke Feb 25 '21 at 16:32
  • I see. Well I use arch so was quite puzzled (and somewhat dazed) to see someone using the sacred program on windows ;) Of course, I too use windows (with basically just a couple videogames and Cygwin) , so any resource which would help we get this working ? – An Ant Feb 25 '21 at 17:09
  • > *so any resource which would help [m]e get this working ?* ~ * ~ * ~ I suggest you start with the first reference in my answer - [MYS2 - Installation](https://www.msys2.org/wiki/MSYS2-installation/) ~ * ~ * ~ Let me know if that works. - Once you have installed MSYS2 (which is **much** easier than installing cygwin, I think), just open up an MSYS2 terminal and pretend that you just opened a terminal window in (Arch-)Linux. ~ * ~ * ~ If you get it to work, consider upvoting my answer! – Henke Feb 26 '21 at 08:06
  • Almost **any** source code I pick from a classical Linux setting (in this case C / C++ code and possibly a Linux makefile) and throw at **MSYS2** will work *without any change what so ever* to the source code. It just works. It's (almost) as if I were running genuine Linux. - Very nice indeed. ~ * ~ Although I can understand that at first you are both *puzzled* and *dazed*, in the end you should most of all be ***amazed!*** :-) – Henke Feb 26 '21 at 08:12
  • I see, but is MSYS required or can Cygwin do much the same ? Pardon my ignorance, I've no idea what it is, I just have Cygwin installed in case if I even lose my sanity trying to keep my C ISO-compliant and decide to succumb to the rich POSIX API. I'll check it out , and +1. I sure as heck need a linux environment on my windows , and best of all something like the Arch I'm used to so thanks ! PS/EDIT : MSYS2 claims to provide nativity of code, but isn't that what Cygwin does too (*though the dll*) , and same for WSL ... What marks it from these different projects ? – An Ant Feb 26 '21 at 16:20
  • > *MSYS[2] required or can Cygwin do much the same ?* ~ * ~ [Compile something in MinGW MSYS2 and you are compiling it for Windows](https://stackoverflow.com/a/792142). If you have no need for Windows executables, then choose either one. I used cygwin about 5 years ago. I definitely prefer MSYS2. Side-note: MinGW and MSYS2 are synonyms - or maybe it is more correct to say that MSYS2 offers a very convenient way to install MinGW. If you ever need **native** Linux, check out [WSL](https://docs.microsoft.com/en-us/windows/wsl/install-win10#step-1---enable-the-windows-subsystem-for-linux). – Henke Feb 26 '21 at 16:50