Object1 a;
Object2 b;
Object3 c;
handle mutexa;
handle mutexb;
handle mutexc;
handle mutexd;
void threadfunction1()
{
mutexa.acquire();
op(a); // 对对象a的读写操作
mutexa.release();
mutexb.acquire();
op(b); // 对对象b的读写操作
mutexb.release();
commonFun();
}
void threadfucntion2()
{
mutexa.acquire();
op(a); // 对对象a的读写操作
mutexa.release();
mutexd.acquire(); // 这里用的是 mutexd,跟threadfun1不同
op(b); // 对对象b的读写操作
mutexd.release();
commonFun();
}
void commonFun()
{
mutex.acquire();
printf(“1111111\n”);
printf(“2222222\n”);
printf(“3333333\n”);
printf(“4444444\n”);
op(c); // 对对象c的读写操作
mutex.release();
}
void main()
{
thread1; // 调用的是function1;
thread2; // 调用的是function1;
.
.
thread50; // 调用的是function1;
//-------------
thread51; // 调用的是function2;
thread52; // 调用的是function2;
.
.
thread100; // 调用的是function2;
start();
}
以上的这段代码,关于对象b,是否成功被保护了?
问题2. 每个进程中访问临界资源的那段代码称为临界区(Critical Section),根据临界区的概念,commonfun 属于临界区,还有哪些代码是临界区?
问题3.commonfun中的输出语句 会是
1111
2222
3333
4444
1111
2222
3333
4444
还是 会出现乱序情况
1111
1111
2222
3333
3333