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();
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();
To copy to clipboard, switch view to plain text mode
i don't know what goin' on
! when i lunch many threads i figure out that there is more than one votin' " means i'm votin' loop" 
plz help
Bookmarks