threads synchronization and Qmutex
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 :
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:
threads synchronization and Qmutex
NB : mutex, nWwait, nMwait, Mvotin, Wvotin, vote was declared as global varialbes in a headerfile
global.h
Code:
#ifdef GLOBAL
#define Global
#else
#define Global extern
#endif
Global bool vote;
Global int nWwait;
Global int nMwait;
Re: threads synchronization and Qmutex
hi gyuys this is me again !! :p
just to tell you that i've found the solution and sychronization is doin' well now :D
Code:
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();