PDA

View Full Version : threads synchronization and Qmutex



jiboon
2nd May 2008, 02:20
hi everyone !
ii have to make a program that simulate vote.
i gotta two classes inheited from QThreads "woman" and "man", just one thread can vote at time !
when a man finish the vote if there is a wowen in the queue, he gives here the turn, else the turn comes back to another man in the queue (if theere is)
here is my code :

man::run() :

mutex.lock();
if ( vote == true ) { // if there is someone votin'
nMwait++; // number of men in waitin for turn++
mutex.unlock();
Mvotin.lock();
}
else {// there is noone votin'
vote = true;
mutex.unlock();

}
//-------The man is votin'

for (int i = 1;i<4 ;i++) {// a loop just to see
qDebug() <<"i'm a man i'm votin"<<i;
_sleep(1000);
}
//------i've termineted my vote

mutex.lock();
vote = false;
if ( nWwait > 0 ) {
nWwait--; //number of women in waitin for turn--
vote = true;
Wvotin.unlock();
}
else {
if ( nMwait > 0 ) {
nMwait--;
vote = true;
Mvotin.unlock();
}
}
mutex.unlock();

woman::run() :

mutex.lock();
if ( vote == true ) { // if there is someone votin'
nWwait++; // number of women in waitin for turn++
mutex.unlock();
Wvotin.lock();
}
else {// there is noone votin'
vote = true;
mutex.unlock();

}
//-------The woman is votin'

for (int i = 1;i<4 ;i++) {// a loop just to see
qDebug() <<"i'm a woman i'm votin"<<i;
_sleep(1000);
}
//------i've termineted my vote

mutex.lock();
vote = false;
if ( nMwait > 0 ) {
nMwait--; //number of men in waitin for turn--
vote = true;
Mvotin.unlock();
}
else {
if ( nWwait > 0 ) {
nWwait--;
vote = true;
Wvotin.unlock();
}
}
mutex.unlock();

i don't know what goin' on :confused:! when i lunch many threads i figure out that there is more than one votin' " means i'm votin' loop" :eek:
plz help :crying:

jiboon
2nd May 2008, 02:30
NB : mutex, nWwait, nMwait, Mvotin, Wvotin, vote was declared as global varialbes in a headerfile
global.h

#ifdef GLOBAL
#define Global
#else
#define Global extern
#endif

Global QMutex mutex;
Global QMutex Mvotin;
Global QMutex Wvotin;


Global bool vote;

Global int nWwait;
Global int nMwait;

jiboon
3rd May 2008, 17:10
hi gyuys this is me again !! :p
just to tell you that i've found the solution and sychronization is doin' well now :D


man::run() :methode

mutex.lock();
if ( vote == true ) { // if there is someone votin'
nMwait++; // number of men in waitin for turn++
mutex.unlock();
semaphoreMan.acquire(1); // instead of Mvotin.lock();
}
else {// there is noone votin'
vote = true;
mutex.unlock();

}
//-------The man is votin'

//------i've termineted my vote

mutex.lock();
vote = false;
if ( nWwait > 0 ) {
nWwait--; //number of women in waitin for turn--
vote = true;
semaphoreWoman.release(1); // instead of Wvotin.unlock();
}
else {
if ( nMwait > 0 ) {
nMwait--;
vote = true;
semaphoreMan.release(1); // instead of Mvotin.unlock();
}
}
mutex.unlock();



woman::run() : mthode

mutex.lock();
if ( vote == true ) { // if there is someone votin'
nWwait++; // number of women in waitin for turn++
mutex.unlock();
semaphoreMan.acquire(1); // instead of Wvotin.lock();
}
else {// there is noone votin'
vote = true;
mutex.unlock();

}
//-------The woman is votin'

//------i've termineted my vote


mutex.lock();
vote = false;
if ( nMwait > 0 ) {
nMwait--; //number of men in waitin for turn--
vote = true;
semaphoreMan.release(1); // instead of Mvotin.unlock();
}
else {
if ( nWwait > 0 ) {
nWwait--;
vote = true;
semaphoreWoman.release(1); // instead of Wvotin.unlock();
}
}
mutex.unlock();