无法正确实现Mutex

时间:2014-10-13 17:01:34

标签: multithreading concurrency mutex semaphore

我正在尝试实现一个没有忙碌等待的Mutex。基本上,如果线程想要锁定,它会检查Mutex是否已被锁定,如果是,则将线程置于休眠状态并将其添加到FIFO队列中。当持有锁的线程解锁Mutex时,它会检查是否有任何线程正在等待访问关键区域,如果是,则从队列中删除线程并将其添加到“就绪”队列,该队列控制正在使用的线程的顺序。

我无法让Mutex工作,但它下面的信号量工作正常。有任何想法吗?感谢。

// DOESN'T WORK

class Mutex {

    Thread * thisThread;
    Thread * threadWithLock;

     lock() {
         // disable interrupts
         interrupts.disable();

         // if no-one has lock, give lock to the current thread and set the lock
         // else put the thread to sleep and add it to the waiting thread queue
         if (lockStatus == 0) {
             lock = 1
             threadWithLock = thisThread;
         } else {
             sleepingThreads.enqueue(thisThread);
             thisThread.sleep();
         }

         // re-enable previous interrupt status
         interrupts.revert();
    }


    unlock() {
        // disable interrupts
        interrupts.disable();

        // if there is a thread waiting for the lock, add it to the ready list
        if (sleepingThreads.isEmpty() == false) {
            Thread * t = sleepingThreads.dequeue();
            t.updateStatus(READY);
            threadReadyList.enqueue(t);
        }

        // release lock
        threadWithLock = null;
        lock = 0;

        // re-enable previous interrupt status
        interrupts.revert();
    }

}

// WORKS

class Semaphore {

    Thread * thisThread;

    down() {
        // disable interrupts
        interrupts.disable();

        readyCount -= 1;
        if (readyCount < 0) {
            sleepingThreads.enqueue(thisThread);
            thisThread.sleep();
        }

        // re-enable previous interrupt status
        interrupts.revert();
    }

    up () {
        // disable interrupts
        interrupts.disable();

        readyCount += + 1;
        if (readyCount <= 0) {
            Thread * t = null;
            t = sleepingThreads.dequeue();
            t.updateStatus(READY);
            threadReadyList.enqueue(t);
        }

        // re-enable previous interrupt status
        interrupts.revert();
    }

}

编辑:问题是我没有把锁交给队列中等待的下一个线程。它与中断状态无关。正确解锁()如下所示......

    unlock() {
        // disable interrupts
        interrupts.disable();

        // if there is a thread waiting for the lock, add it to the ready list,
        // and hand the lock over
        if (sleepingThreads.isEmpty() == false) {
            Thread * t = sleepingThreads.dequeue();
            t.updateStatus(READY);
            threadReadyList.enqueue(t);
            threadWithLock = t;
        } else {
            //release the lock
            threadWithLock = null;
            lock = 0
        }

        // re-enable previous interrupt status
        interrupts.revert();
    }

1 个答案:

答案 0 :(得分:0)

问题是我没有把锁交给队列中等待的下一个线程。它与中断状态无关。正确解锁()如下所示......

    unlock() {
        // disable interrupts
        interrupts.disable();

        // if there is a thread waiting for the lock, add it to the ready list,
        // and hand the lock over
        if (sleepingThreads.isEmpty() == false) {
            Thread * t = sleepingThreads.dequeue();
            t.updateStatus(READY);
            threadReadyList.enqueue(t);
            threadWithLock = t;
        } else {
            //release the lock
            threadWithLock = null;
            lock = 0
        }

        // re-enable previous interrupt status
        interrupts.revert();
    }