home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / baclsema.zip / SEMTEST2.C < prev    next >
C/C++ Source or Header  |  1994-06-23  |  2KB  |  58 lines

  1. // semtest2 - Semaphore test program 2
  2.  
  3. #include <iostream.h>
  4. #include <ipc.h>
  5.  
  6. // ****************************************************************************
  7.  
  8. int main()
  9.  
  10. {
  11.    // print program title
  12.    cout << "semtest2 - Semaphore test program" << endl << endl;
  13.  
  14.    // access an event semaphore
  15.    cout << "accessing an event semaphore..." << endl;
  16.    ipcEventSemaphore event_sem("evtsem", semaccess);
  17.    if (!event_sem) {
  18.       cerr << "Error!  state = " << event_sem.rdstate() << endl;
  19.       return 1;
  20.    }
  21.  
  22.    // show the name, type and id for the semaphore
  23.    cout << "event semaphore name is " << event_sem.Name() << endl
  24.         << "type is " << (int)event_sem.Type() << endl
  25.         << "and id is " << event_sem.ID() << endl << endl;
  26.  
  27.    // create a mutex semaphore
  28.    cout << "creating a mutex semaphore..." << endl;
  29.    ipcMutexSemaphore mutex_sem("mtxsem", semcreate);
  30.    if (!mutex_sem) {
  31.       cerr << "Error!  state = " << mutex_sem.rdstate() << endl;
  32.       return 2;
  33.    }
  34.  
  35.    // show the name, type and id for the semaphore
  36.    cout << "mutex semaphore name is " << mutex_sem.Name() << endl
  37.         << "type is " << (int)mutex_sem.Type() << endl
  38.         << "and id is " << mutex_sem.ID() << endl << endl;
  39.  
  40.    // request the mutex semaphore
  41.    cout << "requesting the mutex semaphore..." << endl;
  42.    mutex_sem.Request();
  43.  
  44.    // post the event semaphore
  45.    cout << "press any key to post the event semaphore...";
  46.    cin.ignore();
  47.    cout << "posting the event semaphore..." << endl;
  48.    event_sem.Post();
  49.  
  50.    // wait for a key to release the mutex semaphore
  51.    cout << "press any key to release the mutex semaphore...";
  52.    cin.ignore();
  53.    cout << "releasing the mutex semphore..." << endl;
  54.    mutex_sem.Release();
  55.  
  56.    return 0;
  57. }
  58.