#1
  1. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    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.

    Thanks!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    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:

    http://www.codeproject.com/threads/xylock.asp

    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?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    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.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    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....

    http://www.cs.wustl.edu/~schmidt

    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

    http://www.boost.org

IMN logo majestic logo threadwatch logo seochat tools logo