Thread: Synchronization problem

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Synchronization problem

    I seem to be at a loss on how to solve this problem...
    Let me try explaining it via a model.

    There are two types of threads: F and M. Both types of threads execute the same function.
    M threads may not run while there are F threads running and vice versa.

    So the question is... how do we implement this entirely with semaphores?

    I have been thinking something like

    Now, there are three semaphores (they must be semaphores!): F, M and E.

    Code:
    OppositeSemaphore->down();
    if (++(*GenderCount) == 1)		
    	GenderSemaphore->down();	
    OppositeSemaphore->up();
    (OppositeSemaphore = F for M threads and M for F threads, and GenderSemaphore = F for F threads and M for M threads.)
    (Obviously the last of both types of threads will do up on the semaphore once they leave the function.)

    ...but of course, this won't work.
    An extra level of semaphores won't work either since we can't block with mutual exclusion while waiting for another semaphore (we'll block all threads!).

    So thinking about it... I am at a loss right now.
    Any ideas?
    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.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I would approach it like a readers-writers problem, but where multiple writers are allowed simultaneous access as well.

    Study the pseudo code on in the link above. Try to adapt it to allow multiple writers.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Synchronization problem, sort of producer/consumer
    By jpenney in forum C Programming
    Replies: 3
    Last Post: 01-25-2012, 03:34 PM
  2. time synchronization problem
    By freeindy in forum C Programming
    Replies: 1
    Last Post: 04-19-2007, 06:25 AM
  3. Synchronization ...
    By Deb in forum C Programming
    Replies: 5
    Last Post: 04-12-2007, 11:26 PM
  4. thread synchronization
    By l2u in forum Windows Programming
    Replies: 2
    Last Post: 08-23-2006, 01:12 PM
  5. synchronization problem
    By michele in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 12:36 PM