Mutual Exclusion

This is a discussion on Mutual Exclusion within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying to understand algorithm to this mutual exclusion problem/example: Code: repeat (while turn != 0); // critical section ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    11

    Mutual Exclusion

    Hello,

    I'm trying to understand algorithm to this mutual exclusion problem/example:

    Code:
    repeat
    (while turn != 0);
    // critical section
    turn = 1;
    // non-critical section
    until false;
    
    repeat
    (while turn != 1);
    // critical section
    turn = 0;
    // non-critical section
    until false;
    Why is there a ; after the while statement as if its doing nothing? Does the statement after ; still get executed?

    It seems a bit backwards to me. Every example of this solution I see implemented this way.

    Why dont they write
    Code:
    (while turn == 1){
    // critical section
    }
    
    (while turn == 0){
    // critical section
    }
    This seems to be straight forward. Am I missing something here?

  2. #2
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    That is not C++ code, more like a pseudo-code, but it would be one of the least understandable pseudocodes out there. I have no idea what these codes are suppose to illustrate, sorry.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Probably it should be
    while (turn != 0);
    in real code.
    It would loop while turn != 0, but the body is empty. That is, basically it does nothing.
    This relies on that turn would have to change due to external factors or it would be an endless loop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    11
    makes sense!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Producer/Consumer W/ mutual exclusion
    By pantherman34 in forum C Programming
    Replies: 10
    Last Post: 05-04-2010, 11:03 AM
  2. Mutual Exclusion and Running a Single Copy [open,lockf,getpid]
    By hosseinyounesi in forum Linux Programming
    Replies: 11
    Last Post: 09-23-2009, 02:16 AM
  3. Mutual exclusion with threads
    By axr0284 in forum C++ Programming
    Replies: 10
    Last Post: 12-21-2005, 07:31 AM
  4. Mutual Exclusion Locks
    By sglass in forum C Programming
    Replies: 3
    Last Post: 03-21-2002, 12:31 PM
  5. Mutual linking
    By Mox in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2001, 06:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21