由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 求教三进程间同步的问题,原题是CC150第5版的16.5
相关主题
two functons and two threadsBloomberg C# developer interview.
read-write locker的实现昨天onsite被问到的 multithreading 题目
请教L家生成H2O水分子的题。muliti-process 之间的communication 和multi- threads之间的
failed bloomberg phone interviewmulti-threading guru们
一道多线程的面试题又tmd的面砸了一个,还是贴贴面经
交通灯OO design哪道题在哪里可以找到??MITBBS 面试题第一题
求问一道multithreading问题qualcomm 新鲜电面面经
问一道multithreading的题哪里有mutex和semaphore例子?
相关话题的讨论汇总
话题: void话题: second话题: foo话题: third话题: first
进入JobHunting版参与讨论
1 (共1页)
n*****g
发帖数: 178
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);
}
感觉这样写有点问题,求指教!
s*****n
发帖数: 5488
2
有什么问题?semiphore太heavy了吧。不如用condition variable.

ThreadA

【在 n*****g 的大作中提到】
: 题目是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

n*****g
发帖数: 178
3

貌似我搞混了,这里我用wait和signal其实就是想用condition variable。
不过cond应该和mutex一起用吧?我觉得应该这么写,不知道有没问题:
void first()
{
lock();
signal(second);
unlock();
}
void second()
{
lock();
wait(first);
unlock();
lock();
signal(third);
unlock();
}
void third()
{
lock();
wait(second);
unlock();
}

【在 s*****n 的大作中提到】
: 有什么问题?semiphore太heavy了吧。不如用condition variable.
:
: ThreadA

g**x
发帖数: 373
4
int i=0;
mutex m;
cond c;
void first() {
{
lock(m);
i++;
signal(c);
unlock(m);
}
void second() {
lock(m);
while (i!=1) {
wait(m, c);
}
i++;
signal(c);
unlock(m);
}
void third() {
lock(m);
while (i!=2) {
wait(m,c);
}
//...
unlock(m);
}

【在 n*****g 的大作中提到】
:
: 貌似我搞混了,这里我用wait和signal其实就是想用condition variable。
: 不过cond应该和mutex一起用吧?我觉得应该这么写,不知道有没问题:
: void first()
: {
: lock();
: signal(second);
: unlock();
: }
: void second()

n*****g
发帖数: 178
5

ThreadA
不知道三楼的对吗?

【在 n*****g 的大作中提到】
: 题目是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

1 (共1页)
进入JobHunting版参与讨论
相关主题
哪里有mutex和semaphore例子?一道多线程的面试题
LinkedIn 面经交通灯OO design哪道题在哪里可以找到??
C语言高手帮我看看下面代码,哪里错了啊,谢了求问一道multithreading问题
n thread rendezvous怎么做用semaphore问一道multithreading的题
two functons and two threadsBloomberg C# developer interview.
read-write locker的实现昨天onsite被问到的 multithreading 题目
请教L家生成H2O水分子的题。muliti-process 之间的communication 和multi- threads之间的
failed bloomberg phone interviewmulti-threading guru们
相关话题的讨论汇总
话题: void话题: second话题: foo话题: third话题: first