Threading on both Windows and Linux
I have seen tutorials on the interne开发者_StackOverflow中文版t 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?
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.
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...)
You can start with boost::thread
. The library provides an abstraction layer and works internally against native threading APIs of each supported platform.
You should be looking at the boost library.
Or you can use ZThread, its pretty lightweight as opposed to boost::thread
You can also look at QThread from Nokia's Qt
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).
I suggest TinyThread++ or TinyCThread. I started using TinyCThread, I found it amazingly simple and it supports many systems including Windows and Linux.
精彩评论