Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Portable multithreading

Author: Scott Gasch

Date: 10:14:57 04/10/02

Go up one level in this thread


On April 10, 2002 at 11:58:36, Russell Reagan wrote:

>I'd like to write my program in portable C, so that I can test it out on my
>Windows machine as well as Unix/Linux. I've heard people suggest pthreads and I
>believe ACE is another that comes to mind.
>
>What is the best approach to implementing threads into a program in a portable
>way? Are any of the portable thread libraries any better than the rest?
>
>Initially I'm just using a seperate thread for input to simplify polling for
>input from console and winboard, but I would eventually (a long way off) like to
>implement some multiprocessor support, so I will need a poartbable threading
>method eventually, and I might as well implement it from the beginning.
>
>All advice is welcomed.
>
>Russell

The way I handle this is with one file, system.c, that contains all my platform
dependent code.  It has a function called SysCreateThread that is basically a
big #ifdef... it either calls the win32 CreateThread function or the pthread
function to create a thread.  Also in system.c I have code that does memory
management (allocate lockable memory, lock memory, unlock memory, etc...),
handles per-thread mutual exclusion (enter critical section, try enter critical
section, leave critical section), queries the OS (get available physical memory,
etc), and some string functions missing from some implementations of the C
runtime lib.  Oh and also the input thread you are talking about...
SysPeekNextInput, SysReadNextInput, SysBlockingRead are all in there.

I designed it this way long before I tried to port the code the first time... I
was developing on FreeBSD/gcc using pthreads but isolated the code in this
manner to make a port to win32 easier.  When the time came to port it was easy;
I had Dann's initial port / workaround, but also I had isolated the code in
system.c.

Scott



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.