4

I have seen tutorials on the internet for making multithreaded applications in C++ on Windows, and other tutorials for doing the same on Linux, but not for both at the same time. Are there functions that would work even if they were compiled on either Linux or Windows?

Langley
  • 75
  • 2
  • 4

8 Answers8

16

You would need to use a library which contains an implementation for both pthread on Linux and the Win32 threading library on Windows (CreateThread and friends).

Boost thread is a popular choice which abstracts the system away.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
  • Is there a way to do it without using external libraries? I'm writing a program that I would like to keep free of them. – Langley Jun 28 '10 at 17:00
  • @Langley boost is such a commonly used library with parts being incorporated into C++0x that you might as well not consider it an external, third party-kind of library and have it installed and readily available at all times and on all platforms. If you are providing an API for people and don't want to require boost, then it's easy enough to write a wrapper which hides the use of boost in, say, a pimpl. – stinky472 Jun 28 '10 at 17:12
  • 1
    You cannot avoid external libraries for this in any case. It is not a standard feature of C++ (at least until std::thread becomes available with C++0x). If you are going for an external library, however, boost is non-intrusive and easy to build. Contrast this to QT, another solution, which requires installing a giant, commercially-licensed framework with code that has to be custom-compiled through a meta-object compiler. – stinky472 Jun 28 '10 at 17:14
  • 2
    @stinky472: you consider GPL/LGPL commercial ? and you only need to use the MOC if you are going to use qt's signals and slots. – smerlin Jun 28 '10 at 17:22
  • @Langley: Yes, you can have two code paths, one to handle pthread, one to handle Win32 threads. You can then wrap this into a library, at which point you might as well use an external one and not go through the whole process :) – Yann Ramin Jun 28 '10 at 17:24
5

You can use POSIX threads and use this library to get pthreads on Windows.

http://sourceware.org/pthreads-win32/

(This is probably only a good option if you're already very used to doing threading on a POSIX system...)

Tim Coker
  • 6,484
  • 2
  • 31
  • 62
2

You can start with boost::thread. The library provides an abstraction layer and works internally against native threading APIs of each supported platform.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
2

You should be looking at the boost library.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
1

Or you can use ZThread, its pretty lightweight as opposed to boost::thread

Dark
  • 195
  • 7
  • I tried building ZThread on windows and found it impossible. Any idea how I can build it on windows? – Langley Jun 28 '10 at 21:03
  • I used it a few years ago, and it compilled nicely. It was already same 2.3.2 version, as i recall. Have you used autotools? Or defined ZT_WIN32, if building w/o GNU make? What's your problem particularly? – Dark Jun 28 '10 at 21:38
0

A portable option is also present in TBB's threads. Of course, TBB encourages you to use the concept of tasks rather than threads, but if you ever need just threads, then this example could help (you'll have to convert the deprecated headers and thread declarations to the new ones).

Community
  • 1
  • 1
Nav
  • 19,885
  • 27
  • 92
  • 135
0

I suggest TinyThread++ or TinyCThread. I started using TinyCThread, I found it amazingly simple and it supports many systems including Windows and Linux.

avner
  • 775
  • 1
  • 8
  • 11
0

You can also look at QThread from Nokia's Qt

David Menard
  • 2,261
  • 3
  • 43
  • 67