Results 1 to 3 of 3

Thread: threads synchronization and Qmutex

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Question 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 :
    Qt Code:
    1. man::run() :
    2.  
    3. mutex.lock();
    4. if ( vote == true ) { // if there is someone votin'
    5. nMwait++; // number of men in waitin for turn++
    6. mutex.unlock();
    7. Mvotin.lock();
    8. }
    9. else {// there is noone votin'
    10. vote = true;
    11. mutex.unlock();
    12.  
    13. }
    14. //-------The man is votin'
    15.  
    16. for (int i = 1;i<4 ;i++) {// a loop just to see
    17. qDebug() <<"i'm a man i'm votin"<<i;
    18. _sleep(1000);
    19. }
    20. //------i've termineted my vote
    21.  
    22. mutex.lock();
    23. vote = false;
    24. if ( nWwait > 0 ) {
    25. nWwait--; //number of women in waitin for turn--
    26. vote = true;
    27. Wvotin.unlock();
    28. }
    29. else {
    30. if ( nMwait > 0 ) {
    31. nMwait--;
    32. vote = true;
    33. Mvotin.unlock();
    34. }
    35. }
    36. mutex.unlock();
    37.  
    38. woman::run() :
    39.  
    40. mutex.lock();
    41. if ( vote == true ) { // if there is someone votin'
    42. nWwait++; // number of women in waitin for turn++
    43. mutex.unlock();
    44. Wvotin.lock();
    45. }
    46. else {// there is noone votin'
    47. vote = true;
    48. mutex.unlock();
    49.  
    50. }
    51. //-------The woman is votin'
    52.  
    53. for (int i = 1;i<4 ;i++) {// a loop just to see
    54. qDebug() <<"i'm a woman i'm votin"<<i;
    55. _sleep(1000);
    56. }
    57. //------i've termineted my vote
    58.  
    59. mutex.lock();
    60. vote = false;
    61. if ( nMwait > 0 ) {
    62. nMwait--; //number of men in waitin for turn--
    63. vote = true;
    64. Mvotin.unlock();
    65. }
    66. else {
    67. if ( nWwait > 0 ) {
    68. nWwait--;
    69. vote = true;
    70. Wvotin.unlock();
    71. }
    72. }
    73. 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
    Last edited by jpn; 12th May 2008 at 20:12. Reason: missing [code] tags

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.