Cracking the coding interview--Q18.5

题目

原文:

Suppose we have the following code:

class Foo {
public:
A(.....); /* If A is called, a new thread will be created and
* the corresponding function will be executed. */
B(.....); /* same as above */
C(.....); /* same as above */
}
Foo f;
f.A(.....);
f.B(.....);
f.C(.....);
i) Can you design a mechanism to make sure that B is executed after A, and C is executed after B?
iii) Suppose we have the following code to use class Foo. We do not know how the threads will be scheduled in the OS.
Foo f;
f.A(.....); f.B(.....); f.C(.....);
f.A(.....); f.B(.....); f.C(.....);
Can you design a mechanism to make sure that all the methods will be executed in sequence?

译文:

假设我们有以下代码:

class Foo {
public:
A(.....); /* 如果A被调用,一个新的线程将被创建,并且相应的函数将被执行*/
B(.....); /*同上 */
C(.....); /*同上 */
}
Foo f;
f.A(.....);
f.B(.....);
f.C(.....);

i) 你能设计一个机制确保B在A之后被调用,C在B后调用吗?
iii)假如我们有以下代码可以使用Foo类。我们不知道线程在系统中将会怎样被安排。
Foo f;
f.A(.....); f.B(.....); f.C(.....);
f.A(.....); f.B(.....); f.C(.....);
你可以设计一个机制确保所有方法将会按顺序被执行吗?


解答

第一问,初始的两个信号量都为0,函数A执行完后,信号量s_a会加1,这时B才可执行。 B执行完后信号量s_b加1,这时C才可执行。以此保证A,B,C的执行顺序。 注意到函数A其实没有受到限制,所以A可以被多个线程多次执行。比如A执行3次, 此时s_a=3;然后执行B,s_a=2,s_b=1;然后执行C,s_a=2,s_b=0; 然后执行B,s_a=1,s_b=1。即可以出现类似这种序列:AAABCB。

Semaphore s_a(0);
Semaphore s_b(0);
A {
    /***/
    s_a.release(1); // 信号量s_a加1
}
B {
    s_a.acquire(1); // 信号量s_a减1
    /****/
    s_b.release(1); // 信号量s_b加1
}
C {
    s_b.acquire(1); // 信号量s_b减1
    /******/
}

第二问代码如下,与第一问不同,以下代码可以确保执行顺序一定严格按照: ABCABCABC…进行。因为每一时刻都只有一个信号量不为0, 且B中要获取的信号量在A中释放,C中要获取的信号量在B中释放, A中要获取的信号量在C中释放。这个保证了执行顺序一定是ABC。

Semaphore s_a(0);
Semaphore s_b(0);
Semaphore s_c(1);
A {
    s_c.acquire(1);
    /***/
    s_a.release(1);
}
B {
    s_a.acquire(1);
    /****/
    s_b.release(1);
}
C {
    s_b.acquire(1);
    /******/
    s_c.release(1);
}


转自:http://hawstein.com/posts/18.5.html


---EOF---

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值