July 29th, 2003, 10:32 AM
Windows/Linux critical section code example
I am working on developing a server that mediates access to a shared resource (an array of objects, in this case) and need to have the code run on Win32 and Linux (i386). I have seen some examples of Win32 code that I can get to compile, but not any Linux code (I am running RedHat 8). I will have a pool of threads that can all read from the object array, but when it comes time to write to the array I want to block all access except for the thread doing the writing. The wait time for a thread to complete its use of the resources should be on the order of a few milliseconds or less, so the writing thread can block as long as all new read (and other write) threads block until it is finished.
Can someone please offer up an example program that will run on Win32 or Linux with the proper #ifdefs?
I would also appreciate advice on how to test the server to know that I am not leaving myself open to conflicts.
July 31st, 2003, 10:55 PM
Am I asking too much?
I didn't think I was asking so much, but I haven't got any feedback from the 20+ people who have looked at my question. I have, for example, found a working example of Win32 critical section code here:
by Xiangyang Liu called "Making your C++ code thread-safe". I am looking for an equivalent example that works in Linux. I saw an example in this forum (hence my post here) under the title "Mutex driving me insane..... " posted by phantom_turtle that had some example linux code posted by dwise1_aol, but was unable to get it to compile. Rather than contact dwise1_aol directly, I though I would post my query on the board so that others could input. My google search on "linux+critical+section+implementation" did not turn up anything meaningful to me.
Can any one help me out?
July 31st, 2003, 11:36 PM
well what you asked for in the first example sounds like a royal pain in the a$$, that's why i dont think you recieved a reply:D windows threads and posix threads, from my experience, are completely different in the implementation. but if your just looking for some examples of linux threading, then here is a link to the *nix God's webpage, W Richard Stevens. This link has the source code for his book Interprocess Communications, http://www.kohala.com/start/unpv22e/unpv22e.html
August 1st, 2003, 08:34 AM
Thanks! I will look through Stevens' examples and see if I can find something I can understand. I have is Unix Network Programming Volume 1, perhaps it is time to get Volume 2.
August 2nd, 2003, 12:25 AM
The reason you didn't get an answer is that the way critical sections work under Linux (or more precisely Posix) is quite different from win32. There isn't a simple one to one mapping.
The more usual trick is to hide the system specific stuff behind a set of library functions. Implementing such a library is not difficult, but it's more than just changing the names of a few functions. For example, what may take one function call under win32 might take 2-3 function calls under posix.
There are a couple of options available to you. Both will require some effort, but should point you in the right direction
You might try having a look at the ACE library. You will be able to find a link to ACE through....
ACE is actually a full blown multi-platform networking library for C++, and [buried in there] includes functions and object types that encapsulate concepts such as thread creation and management, synchronisation [i.e. mutexes, critical sections, etc].
IIRC, the Boost library [again for C++] contains some functions related to concurrent programming (i.e. multithreading, multitasking, synchronisation). The boost library may be found at