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?
8 Answers
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.
5 Comments
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...)
Comments
You can start with boost::thread. The library provides an abstraction layer and works internally against native threading APIs of each supported platform.
Comments
Or you can use ZThread, its pretty lightweight as opposed to boost::thread
2 Comments
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).
Comments
I suggest TinyThread++ or TinyCThread. I started using TinyCThread, I found it amazingly simple and it supports many systems including Windows and Linux.