使用线程在同一时刻运行多个任务时,可以使用互斥锁来同步多个任务,阻止一个任务干扰另一个任务的资源。也就是说,使用互斥锁来保证同一时刻只允许一个任务访问共享资源。
线程间协作:多个线程一起工作来共同解决某个问题(问题的某一部分必须在另一部分被解决之前解决完毕)。
ZThread库中使用 互斥锁 并允许 挂起 的基类Condition,可以通过wait()挂起一个任务(不是忙等待),当外部状态发生改变时,通过signal()或broadcast()来唤醒这个任务。
wait()的工作方式:它会释放被Condition对象所控制的互斥锁Mutex(很关键),并且挂起线程。正因为会释放被Condition对象所控制的互斥锁Mutex,这就意味着该Mutex可以被其他线程获得,使当前线程挂起(等待其他线程的某个作务完成),并把运行控制权交给其他线程。
下面是一个关于线程协助的简单例子:一个线程给Car上蜡,一个线程给Car抛光。抛光线程在上蜡完成之前不能进行工作、上蜡线程在Car可以再上蜡外套之前必须等到抛光线程完成:
- #include <vld.h>
- #include "stdafx.h"
- #include "zthread/Mutex.h"
- #include "zthread/Guard.h"
- #include "zthread/Condition.h"
- #include "zthread/CountedPtr.h"
- #include <iostream>
- using namespace ZThread;
- using namespace std;
- class Car
- {
- public:
- Car(): condition(Lock), bIsWaxon(false){}
- void Waxed() //上蜡
- {
- Guard<Mutex> g(Lock);
- bIsWaxon = true;
- condition.signal(); //唤醒等待condition的线程
- }
- void Buffed() //抛光
- {
- Guard<Mutex> g(Lock);
- bIsWaxon = false;
- condition.signal(); //唤醒等待condition的线程
- }
- //
- void WaitforWaxing() //等待上蜡
- {
- //这个保护锁很特别,它会在wait()调用时释放掉。
- Guard<Mutex> g(Lock);
- while (bIsWaxon == false)
- condition.wait();
- }
- void WaitforBuffing() //等待抛光
- {
- Guard<Mutex> g(Lock);
- while(bIsWaxon == true)
- condition.wait();
- }
- private:
- Mutex Lock;
- Condition condition; //用于线程间协作
- bool bIsWaxon; //是否上蜡
- };
- //上蜡任务
- class WaxOn : public Runnable
- {
- public:
- WaxOn(CountedPtr<Car>& ACar): pCar(ACar){}
- void run()
- {
- try
- {
- while (!Thread::interrupted())
- {
- cout << " Wax On! " <<endl;
- Thread::sleep(200);
- pCar->Waxed();
- pCar->WaitforBuffing(); //上完蜡之后如果再想上蜡,必须等待抛光之后,挂起线程
- }
- }
- catch (Interrupted_Exception& e)
- {
- cerr << " Jarry Wax On : " << e.what() <<endl;
- }
- }
- private:
- CountedPtr<Car> pCar;
- };
- //抛光任务
- class WaxOff : public Runnable
- {
- public:
- WaxOff(CountedPtr<Car>& ACar) : pCar(ACar){}
- void run()
- {
- try
- {
- while (!Thread::interrupted())
- {
- pCar->WaitforWaxing(); //抛光之前必须先完成上蜡操作
- Thread::sleep(200);
- cout <<" Wax Off !" <<endl;
- pCar->Buffed();
- }
- }
- catch (Interrupted_Exception& e)
- {
- cerr << "Jarry Wax Off : " << e.what() <<endl;
- }
- }
- private:
- CountedPtr<Car> pCar;
- };
- int main()
- {
- try
- {
- CountedPtr<Car> car(new Car);
- ThreadedExecutor executor;
- executor.execute(new WaxOff(car));
- executor.execute(new WaxOn(car));
- cin.get();
- executor.interrupt(); //执行器的中断,会为其控制下的所有线程调用interrupt()
- cin.get();
- }
- catch(Synchronization_Exception& e)
- {
- cerr << "Jarry Main :" <<e.what() <<endl;
- }
- return 0;
- }
代码中,wait()的调用被置于一个while循环中,用这个循环来检查相关条件。主要基于两个原因:
1、很可能当某个线程得一个信号signal()时,其他一些条件可能已经改变了,但这些条件在这里与调用wait()的原因无关。如果有这种情况,该线程在其相关的条件改变这前将再一次被挂起。
2、在该线程从其wait()函数中醒来之时,可能另外某个任务改变了一些条件,因此这个线程就不能或者没兴趣在此时执行其操作了。于是再次调用wait()重新挂起。
——基于以上这两个原因在调用wait()时总会出现,故总是将wait()置于while循环中来测试与线程相关的条件(如本例中的 bool bIsWaxon)。