1,多线程状态。
- 初始化(init):该线程正在被创建。
- 准备(Ready):该线程在就绪列表中,等待CPU调度。
- 运行(Running):该线程正在运行。
- 阻塞(Blocked):该线程被阻塞挂起。
- Blocked状态包括:
- pend:锁,事件,信号量等阻塞;
- suspend:主动pend;
- delay:延时阻塞;
- pendtiem:因为锁、事件、信号量时间等 超时等待。
2,手动实现RALL管理mutex资源、锁的释放
class XMutex
{
public:
XMutex(mutex& mux):mut_(mux) //引用时的传参只能在构造函数中
{
cout << "lock" << endl;
mux.lock();
}
~XMutex()
{
cout << "unlock" << endl;
mut_.unlock();
}
private:
mutex& mut_; //保存传过来的
};
static mutex mut;
void testFunc(int status)
{
//锁的使用
XMutex look(mut);
if (status == 1)
{
cout << "status = " << status << endl;
return;
}
else
{
cout << "status = " << status << endl;
return;
}
}
void main()
{
testFunc(1);
testFunc(4);
testFunc(1);
}
每次使用后,都会由他的构造函数对锁资源进行释放。