home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / TASK.ZIP / SEM.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-21  |  1.1 KB  |  34 lines

  1. // SEM.HPP   Copyright 1989 by Dlugosz Software
  2. // from multitasking classes
  3.  
  4. class semaphore {
  5.    int value;
  6.    task_list blocked;
  7. public:
  8.    semaphore (unsigned initial_value);
  9.    semaphore();  //uses initial value of 1
  10.    ~semaphore() {}
  11.       //the destructor for the blocked list sends errors to waiting tasks
  12.    bool wait();
  13.       /* normally returns TRUE.  Will return FALSE if the semaphore is
  14.          destroyed before the Siginal is received.  */
  15.    void signal();
  16.    int count() { return value; }
  17.       /* a positive or 0 value indicates how many more wait's can be sent.
  18.          The absolute value of a negative value is the number of tasks
  19.          waiting for siginals.  */
  20.    // supply some synonyms for variety
  21.    bool operator--() { return wait(); }
  22.    void operator++() { signal(); }
  23.    operator int() { return value; }
  24.    };
  25.  
  26.  
  27. // a class to impose structure on semaphores used for mutual exclusion
  28. class resource_lock {
  29.    semaphore* sem;
  30. public:
  31.    resource_lock (semaphore &x) { sem= &x;  x.wait(); }
  32.    ~resource_lock() { sem->signal(); }
  33.    };
  34.