멀티 스레딩 및 잠금에 대한 많은 종류의 인터페이스를 보았습니다.이 인터페이스를 조작하는 방법은 무엇입니까?
다른 사람들이 대기 기능을 구현할 수 있습니다) 하나 개의 클래스와 취득을 (있는 동안
이
는그들 중 일부는 아래의 예제와 같은 2 개 개의 다른 클래스를 포함, 나를 좌절 느낌.
내 질문은 : 왜 우리는 객체 지향 프로그래밍에서 이와 같은 잠금을 설계합니까?
이러한 개체를 조작하는 방법은 무엇입니까?
class Lock
{
public:
Lock();
~Lock();
// Acquire the lock.
void
acquire()
{ this->lock_->acquire(); }
// Release the lock.
void
release()
{ this->lock_->release(); }
private:
// This class can not be copied.
Lock(const Lock&);
Lock& operator=(const Lock&);
friend class Condvar;
Lock_impl*
get_impl() const
{ return this->lock_; }
Lock_impl* lock_;
};
class Condvar
{
public:
Condvar(Lock& lock);
~Condvar();
// Wait for the condition variable to be signalled. This should
// only be called when the lock is held.
void
wait()
{ this->condvar_->wait(this->lock_.get_impl()); }
// Signal the condition variable--wake up at least one thread
// waiting on the condition variable. This should only be called
// when the lock is held.
void
signal()
{ this->condvar_->signal(); }
// Broadcast the condition variable--wake up all threads waiting on
// the condition variable. This should only be called when the lock
// is held.
void
broadcast()
{ this->condvar_->broadcast(); }
private:
// This class can not be copied.
Condvar(const Condvar&);
Condvar& operator=(const Condvar&);
Lock& lock_;
Condvar_impl* condvar_;
};