Re: timing problem with pthread_mutex_unlock and lock
- From: m1ngleg02@xxxxxxxxxxxxxx
- Date: 2 Nov 2005 15:13:37 -0800
hello loic,
thanks for your answer. fortunately i got it reproduced with a small
program, see below.
to reproduce the problem, compile first the code, and run the script.
it should run without a problem.
now launch a processor intensive task. after a few tries with the
script, it should hang.
i am unfortunately not sure that you will be able to reproduce it, a
slow processor may be an advantage (i use a 850 mhz duron). at my
system for example it hangs at counter 11 after 4 times executing the
script fast. but also got it after 2 times at counter 2, well i
indicate that it should be possible to hang it.
i am of course not sure that my original code has not some bug, but,
egal, how would you get around the problem of knowing whether a process
has locked itself already?
or is their a smarter way for: i want to stop a bunch of slave threads,
let the master thread do something, and let them again go (by
unlocking)? the unlocking problem is easy, prepare the data and unlock
them.
so want some way of stopping threads, and i want to be sure that they
*really* stopped.
i looked in the manual, man 3 pthread_mutex_init, and got the idea of
using
while(pthread_mutex_trylock(&mutex_slave)!=EBUSY){;}. but, as i
understand, trylock is really locking anyway, not just *trying*, isn't
it? then it is useless for my problem.
cc -o test example.c -lpthread
example.c
----------
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<pthread.h>
#include<time.h>
pthread_t threads[2];
pthread_mutex_t mutex_master = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_slave = PTHREAD_MUTEX_INITIALIZER;
void function(int);
void main(void){
int id[2],t;
pthread_mutex_lock(&mutex_master);
for(t=0;t<2;++t){
id[t]=pthread_create(&threads[t], NULL, function,t);
}
for(t=0;t<2;++t){
pthread_join(threads[t],NULL);
}
pthread_mutex_destroy(&mutex_master);
pthread_mutex_destroy(&mutex_slave);
}
void function(int t){
struct timespec req;
req.tv_sec=0;
req.tv_nsec=0;
if(t!=0){
pthread_mutex_unlock(&mutex_master);
//nanosleep(&req,NULL); /* or try running with a high processor
load, and execute it a lot with help of the bash script */
pthread_mutex_lock(&mutex_slave);
}
else{
pthread_mutex_lock(&mutex_master);
pthread_mutex_unlock(&mutex_slave);
pthread_mutex_lock(&mutex_slave);
}
}
----------
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
../test
echo The counter is $COUNTER
let COUNTER-=1
done
.
- Follow-Ups:
- Re: timing problem with pthread_mutex_unlock and lock
- From: Arndt Muehlenfeld
- Re: timing problem with pthread_mutex_unlock and lock
- From: David Schwartz
- Re: timing problem with pthread_mutex_unlock and lock
- References:
- Re: timing problem with pthread_mutex_unlock and lock
- From: loic-dev
- Re: timing problem with pthread_mutex_unlock and lock
- Prev by Date: Re: Concurrency with DSMLs and MDD
- Next by Date: Sub thread and Thread ?
- Previous by thread: Re: timing problem with pthread_mutex_unlock and lock
- Next by thread: Re: timing problem with pthread_mutex_unlock and lock
- Index(es):
Relevant Pages
|