Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2017, How to make #include <pthread.h> work?

I feel like I've been searching for hours today on how to make #include <pthread.h> work in Visual Studio 2017 on Windows 10 and haven't come across an answer that has worked. Can someone please instruct me how to get this to work?

The error I receive is: cannot open source file "pthread.h"

like image 418
Jbags Avatar asked Feb 20 '18 21:02

Jbags


People also ask

How do I create a project in Visual Studio 2017?

Open the "Create a new project" dialog When you first open Visual Studio, the start window appears, and from there, you can select Create a new project. If the Visual Studio development environment is already open, you can create a new project by choosing File > New > Project on the menu bar.

Is Visual Studio Free 2017?

Visual Studio 2017 and other Products If you don't have a Visual Studio Subscription, you can create one for free by clicking on “Create a new Microsoft account” on the login page.


4 Answers

In Visual Studio 2017

  1. Project -> Manage Nuget Packages

  2. In browse Tab search for 'pthread'

  3. Select Install[Search Results in Nuget packages]

enter image description here

like image 72
Bala Ganesh Avatar answered Sep 29 '22 02:09

Bala Ganesh


For Visual Studio 2017, I've installed through NuGet Packages.

Try below steps,

  1. Go to Project > 'Manage NuGet Packages'
  2. Browse > search for 'pthread' > install
like image 23
Manoj M Avatar answered Sep 29 '22 03:09

Manoj M


Windows doesn't natively support pthreads.

There is an implementation of pthreads for Windows you could use. If you're just trying to get some code running on Windows ASAP it may be your best bet.

If you can modify the code, you might want to take a look at what pthread functions you are actually using; if all you need is a mutex a few #ifdefs around pthreads/winapi may be a better way to go.

There are also some projects which implement compatibility layers that aren't as expressive as the full pthreads API, but are more than enough for most projects. TinyCThread is one option (which I maintain, so take the recommendation with a grain of salt) which implements the C11 threads API.

Other options include:

  • GLib
  • libuv

OpenMP could also be a good choice. It is really easy to use, but operates at a higher level and so it may require a more substantial rewrite.

like image 29
nemequ Avatar answered Sep 29 '22 02:09

nemequ


If you have vcpkg installed, then you can just do:

vcpkg.exe install pthread

That will download some code from https://sourceforge.net/projects/pthreads4w, compile it and install it in your system. If vcpkg is setup correctly on your machine, Visual Studio should use these files automatically.

like image 41
chrset Avatar answered Sep 29 '22 01:09

chrset