Multithreading - sharing C++ code between Windows and Linux

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

Multithreading - sharing C++ code between Windows and Linux

Postby Don Cross » 29 Dec 2008, 22:31

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!
Author of Chenard - http://cosinekitty.com/chenard
User avatar
Don Cross
 
Posts: 29
Joined: 13 Nov 2008, 04:13
Location: Florida, USA

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

Postby Pradu » 30 Dec 2008, 00:50

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.
User avatar
Pradu
 
Posts: 343
Joined: 12 Jan 2005, 19:17
Location: Chandler, Arizona, USA

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

Postby Dann Corbit » 30 Dec 2008, 02:35

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.
Dann Corbit
 

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

Postby Michel » 30 Dec 2008, 20:28

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).
Michel
 
Posts: 513
Joined: 01 Oct 2008, 12:15

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

Postby Ilari Pihlajisto » 03 Jan 2009, 05:28

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.
User avatar
Ilari Pihlajisto
 
Posts: 78
Joined: 18 Jul 2005, 06:58

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

Postby Michel » 04 Jan 2009, 16:11

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
Michel
 
Posts: 513
Joined: 01 Oct 2008, 12:15


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 43 guests