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
2010-06-28 16:51:51
+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
2010-06-28 16:52:57
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
2010-06-28 17:00:25
+1 for boost threads.
2010-06-28 17:10:24
@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.
2010-06-28 17:12:05
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.
2010-06-28 17:14:07
@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
2010-06-28 17:22:37
@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
2010-06-28 17:24:49
+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
2010-06-28 16:54:24
+1
A:
Or you can use ZThread, its pretty lightweight as opposed to boost::thread
Dark
2010-06-28 17:16:57
I tried building ZThread on windows and found it impossible. Any idea how I can build it on windows?
Langley
2010-06-28 21:03:27
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
2010-06-28 21:38:11