Merci, c ce que je pensais faire a peu pres.
Au debut je pensais faire une class read/write access pour une meilleure optimisation, avec semaphore et gate, ms c plus long a implementer.
Un truc ce ce style :
Code :
- class RWLock : private Mutex {
- private:
- Semaphore write_lock; // used as a one-at-a-time release valve
- Gate read_barrier; // used to block/wakeup readers
- unsigned int writer_count; // # of writers waiting for or holding the lock
- unsigned int reader_count; // # of readers holding the lock
- public:
- ReadLock(void) {
- writer_count = 0;
- reader_count = 0;
- }
- void ReadLock(void) {
- Mutex::Lock();
- // wait until there are no more writers holding the lock
- while (writer_count > 0) {
- Mutex::Unlock();
- read_barrier.Wait();
- Mutex::Lock();
- }
- reader_count++;
- Mutex::Unlock();
- }
- void WriteLock(void) {
- Mutex::Lock();
- writer_count++; // this stops new readers from getting a lock
- write_lock.Wait(); // wait until the write lock is available
- read_barrier.Close(); // give new readers something to wait for
- Mutex::Unlock();
- }
- void Unlock(void) {
- Mutex::Lock();
- if (writer_count > 0) { // we must be a writer
- writer_count--;
- if (writer_count > 0) // another writer is waiting
- writer_lock.Post(); // let it go
- else
- read_barrier.Open(); // open the floodgates
- }
- else {
- reader_count--;
- // if we're the last reader and a writer is waiting, let it go
- if ((reader_count == 0) && (writer_count > 0)) {
- writer_lock.Post();
- }
- Mutex::Unlock();
- }
- };
|
Mais j'aurais du mal a rendre cette solution multiplatforme..
Ta solution suffira, et est tout aussi sure. Merci encore.