求教三进程间同步的问题,原题是CC150第5版的16.5# JobHunting - 待字闺中
n*g
1 楼
题目是CC150第五版16.5题,题目是这样的:
Suppose we have the following code:
public class Foo {
public Foo() { . . . }
public void first() { ... }
public void second() { ... }
public void thirdQ { ... }
The same instance of Foo will be passed to three different threads. ThreadA
will call first, threads will call second, and threadC will call third.
Design a mechanism to ensure that first is called before second and second
is called before third.
这要换成C语言来问的话,是不是等同于这样问:一个foo函数,确保foo先被first调用
,然后再被second和third调用(first,second,和third来自不同的thread)?
如果等同话 ,用C语言,Semaphore如何解答呢?
我的想法是:
void first()
{
signal(sem1);
}
void second()
{
wait(sem1);
signal(sem2);
}
void third()
{
wait(sem2);
}
感觉这样写有点问题,求指教!
Suppose we have the following code:
public class Foo {
public Foo() { . . . }
public void first() { ... }
public void second() { ... }
public void thirdQ { ... }
The same instance of Foo will be passed to three different threads. ThreadA
will call first, threads will call second, and threadC will call third.
Design a mechanism to ensure that first is called before second and second
is called before third.
这要换成C语言来问的话,是不是等同于这样问:一个foo函数,确保foo先被first调用
,然后再被second和third调用(first,second,和third来自不同的thread)?
如果等同话 ,用C语言,Semaphore如何解答呢?
我的想法是:
void first()
{
signal(sem1);
}
void second()
{
wait(sem1);
signal(sem2);
}
void third()
{
wait(sem2);
}
感觉这样写有点问题,求指教!