Page 1 of 1

Multithreading - sharing C++ code between Windows and Linux

PostPosted: 29 Dec 2008, 22:31
by Don Cross
I am fairly experienced with multithreaded C++ programming in Windows. Until today I thought that true multithreading (as opposed to multiple processes with separate address space) was not available in Linux. However, I did a little research and discovered the Native POSIX Threading Library (NPTL):
http://www.ddj.com/linux-open-source/184406204
http://en.wikipedia.org/wiki/Native_POS ... ad_Library

I now have a few simple test programs using NPTL compiling with gcc on my Ubuntu box.

The obvious next step for me as a chess programmer is to ask, how can I write C++ code that will enable multithreading in my chess engine and will compile in both Windows and Linux? The obvious thing is to learn NPTL, then write my own small class library with conditional compilation to hide the differences between Windows and Linux: creating threads, mutex locking, etc.

But first I thought I would ask here to see if there is a more elegant way, for example an already coded and debugged library.

Thanks in advance for any ideas!

Re: Multithreading - sharing C++ code between Windows and Li

PostPosted: 30 Dec 2008, 00:50
by Pradu
I don't know much about the differences between NPTL and the regular POSIX threading libraries I use (comes with GCC), but how about the Boost threading library? The standard threading library for C++09 will probably be based off of the Boost library. You can start using Boost threads now and when C++09 compilers come out, you can replace it by using std::thread.

I created a threading wrapper for Windows and Linux for my C program like you proposed in your post. But it was a hassle and probably not nearly as good as the Boost libraries. I am going to use the Boost libraries for my yet to be C++ program, Dirty.

The Boost libraries also contain many other useful cross-platform libraries other than threads, like Boost Filesystem and Boost ASIO (network and low-level I/O programming).

If you ever want to write cross-platform GUI programs for you engine you can try wxWidgets. The learning curve is initially pretty high compared to other GUI toolkits. Nevertheless, I like it now and I've use it for all of my GUI projects.

Re: Multithreading - sharing C++ code between Windows and Li

PostPosted: 30 Dec 2008, 02:35
by Dann Corbit
Don Cross wrote:I am fairly experienced with multithreaded C++ programming in Windows. Until today I thought that true multithreading (as opposed to multiple processes with separate address space) was not available in Linux. However, I did a little research and discovered the Native POSIX Threading Library (NPTL):
http://www.ddj.com/linux-open-source/184406204
http://en.wikipedia.org/wiki/Native_POS ... ad_Library

I now have a few simple test programs using NPTL compiling with gcc on my Ubuntu box.

The obvious next step for me as a chess programmer is to ask, how can I write C++ code that will enable multithreading in my chess engine and will compile in both Windows and Linux? The obvious thing is to learn NPTL, then write my own small class library with conditional compilation to hide the differences between Windows and Linux: creating threads, mutex locking, etc.

But first I thought I would ask here to see if there is a more elegant way, for example an already coded and debugged library.

Thanks in advance for any ideas!


I like ACE:
http://www.cs.wustl.edu/~schmidt/ACE.html
It abstracts not only threading but shared memory, processes, and all other operating system services.

It's the bee's knees and the cat's pajamas.

Re: Multithreading - sharing C++ code between Windows and Li

PostPosted: 30 Dec 2008, 20:28
by Michel
Look at the Glaurung source.

It is quite easy to abstract the differences between WIndows and Linux threading
(after all the same primitives are used).

Re: Multithreading - sharing C++ code between Windows and Li

PostPosted: 03 Jan 2009, 05:28
by Ilari Pihlajisto
Michel wrote:Look at the Glaurung source.

It is quite easy to abstract the differences between WIndows and Linux threading
(after all the same primitives are used).


Most Linux developers are used to synchronizing threads with condition variables, which aren't available in Windows XP and earlier. They're also somewhat difficult to abstract: http://www.cs.wustl.edu/~schmidt/win32-cv-1.html

But if one only needs threads and mutexes, it's a piece of cake.

Re: Multithreading - sharing C++ code between Windows and Li

PostPosted: 04 Jan 2009, 16:11
by Michel
Most Linux developers are used to synchronizing threads with condition variables, which aren't available in Windows XP and earlier. They're also somewhat difficult to abstract: http://www.cs.wustl.edu/~schmidt/win32-cv-1.html


It seems you are right. I thought such a basic primitive would be present in every OS.

WIndows has semaphores though and you can get a long way with semaphores.

It seems condition variables appeared in Vista.

http://msdn.microsoft.com/en-us/library/ms682052(VS.85).aspx