bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-commoncpp] (no subject)


From: Luca Tessitore
Subject: [Bug-commoncpp] (no subject)
Date: Thu, 10 Aug 2000 14:24:48 +0200

Good Morning,
        'm working with CommonC++ library under Solaris 8 and i found a bug in
Mutex. When a thread exit from mutual exclusion (Mutex::LeaveMutex()) other
thread can't enter in mutex too. Please see code belowe

 
 #include <thread.h>
 #include <config.h>
 #include <macros.h>
 #include <socket.h>
 
 class ThreadTest : public Thread
 {
   private:
    int  _threadID;
    Mutex * mutex;
 
    void Run()
    {
       cout << "Starting Thread " << _threadID << "\n";
 
       /*
       while (!mutex->TryEnterMutex()) {
          cout << "Mutex is busy ... sleep 1 second ("<< _threadID << ")\n";
          this->Sleep(1000);
       }
       */
       mutex->EnterMutex();
 
       for (int i=0; i<10; i++)
       {
 
          cout << "Thread ID="<< _threadID <<", loop number="<<(i+1)<< endl;
          this->Sleep(500);
 
       }
 
       mutex->LeaveMutex();
    };
 
    void Final()
    {
       cout << "Thread " << _threadID <<" exiting."<< endl;
       delete this;
    };
 
 public:
 
    ThreadTest(int threadID, Mutex * mutexPar): _threadID(threadID)
    {
       mutex = mutexPar; // initialise the mutex
    };
 
 };
 
 int main()
 {
    Mutex *m = new Mutex();
 
    ThreadTest *thread1 = new ThreadTest(1,m);
    ThreadTest *thread2 = new ThreadTest(2,m);
    ThreadTest *thread3 = new ThreadTest(3,m);
 
    cout << "Starting 3 threads named 1,2,3." << endl;
    thread1->Start();
    thread2->Start();
    thread3->Start();
  
    sleep(30);
 
    cout << "Main exiting." << endl;
    delete m;
    return 0;
 }
 
 When Thread1 finish is work Thread 2 stay blocked on mutex->EnterMutex()
 forever. I try the same code under Windows and Linux and all work better
 (when Thread1 leave mutual exclusion, Thread2 enter, work, leave and after
 Thread3 make is job).
 
 Sorry for my English :-)
 
 I hope it can be usefull for you!!!!
 
 Luca Tessitore
 


============================
Dott. Ing. Luca Tessitore

Address:        V. Circonvallazione 16
                10090 Montalenghe (TO)

Phone:  +39 011 9839295
Office: +39 011 5647084
Mobile: +39 0347 9778972
============================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]