[cs33211] Project

Mikhail Nesterenko mikhail at cs.kent.edu
Sat Oct 13 17:36:32 EDT 2012


> I am having a hard time getting the threads to unblock in the right order.
> When I notify threads at the end of the zone that this particular thread is
> finished, the thread that I want to take the lock next is the one that had
> to wait first. But, I keep getting the situation where the thread that
> takes the lock is the one that was most recently blocked. Can you help?

Wake up all threads and let them sort out which one is right and which
one is wrong. Put the wrong threads back to sleep. Take a look at this
code:

  http://vega.cs.kent.edu/~mikhail/classes/os.f12/Threads_Examples/C++11/Synchronization/LocksCVs/readersWritersLCV.cpp

Note the use of  rdr.notify_all() function to wake up all the threads
and this idiom to  block the wrong threads.

   while(nw > 0){
      cout << "reader " << id << " blocks, nw= " << nw << " nr= " <<
      nr << endl;
      rdr.wait(mtx);
    }

Thanks,
-- 
Mikhail


More information about the cs33211 mailing list