📄 synchlist.cc
字号:
// synchlist.cc// Routines for synchronized access to a list.//// Implemented by surrounding the List abstraction// with synchronization routines.//// Implemented in "monitor"-style -- surround each procedure with a// lock acquire and release pair, using condition signal and wait for// synchronization.//// Copyright (c) 1992-1993 The Regents of the University of California.// All rights reserved. See copyright.h for copyright notice and limitation // of liability and disclaimer of warranty provisions.#include "copyright.h"//----------------------------------------------------------------------// SynchList::SynchList// Allocate and initialize the data structures needed for a // synchronized list, empty to start with.// Elements can now be added to the list.//----------------------------------------------------------------------template <class T>SynchList<T>::SynchList(){ list = new List<T>(); lock = new Lock("list lock"); listEmpty = new Condition("list empty cond");}//----------------------------------------------------------------------// SynchList::~SynchList// De-allocate the data structures created for synchronizing a list. //----------------------------------------------------------------------template <class T>SynchList<T>::~SynchList(){ delete list; delete lock; delete listEmpty;}//----------------------------------------------------------------------// SynchList::Append// Append an "item" to the end of the list. Wake up anyone// waiting for an element to be appended.//// "item" is the thing to put on the list, it can be a pointer to // anything.//----------------------------------------------------------------------template <class T>voidSynchList<T>::Append(T item){ lock->Acquire(); // enforce mutual exclusive access to the list list->Append(item); listEmpty->Signal(lock); // wake up a waiter, if any lock->Release();}//----------------------------------------------------------------------// SynchList::Remove// Remove an "item" from the beginning of the list. Wait if// the list is empty.// Returns:// The removed item. //----------------------------------------------------------------------template <class T>TSynchList<T>::Remove(){ T item; lock->Acquire(); // enforce mutual exclusion while (list->IsEmpty()) listEmpty->Wait(lock); // wait until list isn't empty item = list->Remove(); ASSERT(item != NULL); lock->Release(); return item;}//----------------------------------------------------------------------// SynchList::Mapcar// Apply function to every item on the list. Obey mutual exclusion// constraints.//// "func" is the procedure to be applied.//----------------------------------------------------------------------template <class T>voidSynchList<T>::Mapcar(VoidFunctionPtr func){ lock->Acquire(); list->Mapcar(func); lock->Release(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -