views:

88

answers:

6

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?

+2  A: 

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

Nikolai N Fetissov
+2  A: 

You should be looking at the boost library.

Noah Roberts
+8  A: 

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
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
+1 for boost threads.
@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.
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: 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
@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
+1  A: 

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
I'd be interested in hearing why this answer got a downvote.
Fred Larson
:) Thanks for the upvote. I was curious about the downvote myself...
Tim Coker
A: 

You can also look at QThread from Nokia's Qt

David Menard
+1  A: 

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

Dark
I tried building ZThread on windows and found it impossible. Any idea how I can build it on windows?
Langley
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