Jan 17, 2009

concurrency

Suppose we have a class:

class Foo {

Public:
A(.....); //if A is called, a new thread will be created and the corresponding function will be executed.
B(.....); //if B is called, a new thread will be created and the corresponding function will be executed.
C(.....); //if C is called, a new thread will be created and the corresponding function will be executed.

......
}

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(.....);

Questions (Part A):
1. Can you explain multithread synchronization mechanism?
2. Can you design a mechanism to make sure that B is executed after A, and C is executed after B?

semaphore b=-n,c=-n; //n is the number of threads
A(...)
{
...
V(b)
}

B(...)
{
...
V(c)
}

C(...)
{
...
}

Questions (Part B):
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(.....);

Q: 1. Can you design a mechanism to make sure that all the methods will be executed in sequence?
mutex s;
Foo f;
P(s)
f.A(.....);
f.B(.....);
f.C(.....);
f.A(.....);
f.B(.....);
f.C(.....);
V(s)

No comments:

Post a Comment