Re: timing problem with pthread_mutex_unlock and lock



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

.



Relevant Pages

  • Re: Merge filter inconsistant - MS Replication Problem.zip (0/1)
    ... I have attached the files you need to reproduce the problem. ... create_test_loc.sql - script to create test databases on subscriber ... create_test_rep.sql - script to create test databases on publisher ... fGetCurrentUserID.sql - script to create filter criteria function on ...
    (microsoft.public.sqlserver.replication)
  • Re: Ksh output garbled/out-of-sync
    ... When you open one of the commented blocks and run the script as --- ... For the "unordered" block, it hardly prints ... I am able to reproduce the problem on several Solaris 5.8 systems now. ...
    (comp.unix.shell)
  • Re: Query acting strangely
    ... script that can help me reproduce and diagnose the problem. ... A repro script consists of: ... the text of the query you're having trouble with. ...
    (microsoft.public.sqlserver.mseq)
  • Re: timing problem with pthread_mutex_unlock and lock
    ... > to reproduce the problem, compile first the code, and run the script. ... it will hang under a certain interleaving. ... and execute it a lot with help of the bash script */ ...
    (comp.programming.threads)
  • bsdutils: /usr/bin/script doesnt wait for command to finish before exiting
    ... The script command fails to wait for the command that it is executing to finish. ... Script started, file is typescript ... void dooutput; ...
    (Ubuntu)