mutex example

F

friend.blah

can any one give a simple example for implementing mutex.
I have 3 threads (taking different input files )enter into the same
function at different times...

The 3 threads need to exit/leave the function till all the input files
reached till end which are taken as input to the threds.


thanks to all
 
P

Pascal J. Bourguignon

can any one give a simple example for implementing mutex.
I have 3 threads (taking different input files )enter into the same
function at different times...

The 3 threads need to exit/leave the function till all the input files
reached till end which are taken as input to the threds.


So you want the three threads in the same function at the same time.
So you want absolutely no mutual exclusion, but exactly the opposite,
you want mutual INCLUSION!

You should try to express what you want more clearly. For example,
you could start with something more grammatically correct. I have
really a hard time parsing this sentence:

"The 3 threads need to exit/leave the function till all the input
files reached till end which are taken as input to the threds."


Let's assume what you want is:


thread 1
|
|
v
----- enter function
|
|
v
(go on reading file 1)
| thread 2
| |
| |
| v
| ----- enter function
| |
| |
| v
| (go on reading file 2)
| |
| |
| |
| | thread 3
| | |
| | |
| | v
| | ----- enter function
| | |
| | |
| | v
| | (go on reading file 3)
v | |
----- reach eof v |
| ----- reach eof |
| | v
| | ----- reach eof
| | |
v v v
---------------------------------------------------- exit function
| | |
| | |
v v v


The synchronization at the exit function could be implemented with a
condition.


initialize:
- a counter = 0
- a mutex m
- a condition c



synchronized_exit(n)
mutext_lock(m)
increment counter
if (counter >= n) then
condition_broadcast(c)
counter=0
else
condition_wait(c,m)
endif
mutext_unlock(m)


All three threads shall call synchronized_exit(3) after reaching their EOF.


man pthread_cond_init pthread_cond_wait pthread_cond_broadcast
man pthread_mutex_init pthread_mutex_lock pthread_mutex_unlock
 
C

Chris Thomasson

can any one give a simple example for implementing mutex.

Sure... Here is some pseudo-code:
___________________________________________________________________
class mutex {
enum constant {
UNLOCKED = 0,
LOCKED = 1,
CONTENTION = 2
};

atomic_word m_state;
event m_waitset;

public:
mutex() : m_state(UNLOCKED) {}

void lock() {
if (ATOMIC_SWAP(&m_state, LOCKED)) {
while (ATOMIC_SWAP(&m_state, CONTENTION)) {
m_waitset.wait();
}
}
MEMBAR #StoreLoad | #StoreStore;
}

void unlock() {
MEMBAR #LoadStore | #StoreStore;
if (ATOMIC_SWAP(&m_state, UNLOCKED) == CONTENTION) {
m_waitset.set();
}
}
};
___________________________________________________________________



I have 3 threads (taking different input files )enter into the same
function at different times...

The 3 threads need to exit/leave the function till all the input files
reached till end which are taken as input to the threds.

Do you need mutual exclusion amongst the three threads or not? If you do,
then only a single thread will ever be executing code within the
critical-section at any one time. If not, then you could use a barrier
synchronization object at the end of the function. Basically, something like
Pascal suggested. Except, POSIX has a barrier already, you don't need to
implement one from mutexs and conditions. Something like; modulo any typos
with error checking omitted:

___________________________________________________________________
class barrier {
pthread_barrier_t m_waitset;

public:
barrier(unsigned count) {
pthread_barrier_init(&m_waitset, NULL, count);
}

~barrier() {
pthread_barrier_destroy(&m_waitset);
}

void wait() {
pthread_barrier_wait(&m_waitset);
}
};
___________________________________________________________________




You can use it like:
___________________________________________________________________
static barrier* g_barrier = NULL;

int main() {
barrier m_barrier(3);
g_barrier = &m_barrier;
{
spawn_threads();
join_threads();
}
return 0;
}

void Your_Function_For_The_Three_Threads(...) {
[...];
g_barrier->wait();
}

___________________________________________________________________



thanks to all

No problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,171
Messages
2,570,936
Members
47,472
Latest member
KarissaBor

Latest Threads

Top