Results 1 to 3 of 3

Thread: threads synchronization and Qmutex

  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

  2. #2

    Default threads synchronization and Qmutex

    NB : mutex, nWwait, nMwait, Mvotin, Wvotin, vote was declared as global varialbes in a headerfile
    global.h
    Qt Code:
    1. #ifdef GLOBAL
    2. #define Global
    3. #else
    4. #define Global extern
    5. #endif
    6.  
    7. Global QMutex mutex;
    8. Global QMutex Mvotin;
    9. Global QMutex Wvotin;
    10.  
    11.  
    12. Global bool vote;
    13.  
    14. Global int nWwait;
    15. Global int nMwait;
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 12th May 2008 at 20:13. Reason: missing [code] tags

  3. #3

    Default Re: threads synchronization and Qmutex

    hi gyuys this is me again !!
    just to tell you that i've found the solution and sychronization is doin' well now

    Qt Code:
    1. man::run() :methode
    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. semaphoreMan.acquire(1); // instead of Mvotin.lock();
    8. }
    9. else {// there is noone votin'
    10. vote = true;
    11. mutex.unlock();
    12.  
    13. }
    14. //-------The man is votin'
    15.  
    16. //------i've termineted my vote
    17.  
    18. mutex.lock();
    19. vote = false;
    20. if ( nWwait > 0 ) {
    21. nWwait--; //number of women in waitin for turn--
    22. vote = true;
    23. semaphoreWoman.release(1); // instead of Wvotin.unlock();
    24. }
    25. else {
    26. if ( nMwait > 0 ) {
    27. nMwait--;
    28. vote = true;
    29. semaphoreMan.release(1); // instead of Mvotin.unlock();
    30. }
    31. }
    32. mutex.unlock();
    33.  
    34.  
    35.  
    36. woman::run() : mthode
    37.  
    38. mutex.lock();
    39. if ( vote == true ) { // if there is someone votin'
    40. nWwait++; // number of women in waitin for turn++
    41. mutex.unlock();
    42. semaphoreMan.acquire(1); // instead of Wvotin.lock();
    43. }
    44. else {// there is noone votin'
    45. vote = true;
    46. mutex.unlock();
    47.  
    48. }
    49. //-------The woman is votin'
    50.  
    51. //------i've termineted my vote
    52.  
    53.  
    54. mutex.lock();
    55. vote = false;
    56. if ( nMwait > 0 ) {
    57. nMwait--; //number of men in waitin for turn--
    58. vote = true;
    59. semaphoreMan.release(1); // instead of Mvotin.unlock();
    60. }
    61. else {
    62. if ( nWwait > 0 ) {
    63. nWwait--;
    64. vote = true;
    65. semaphoreWoman.release(1); // instead of Wvotin.unlock();
    66. }
    67. }
    68. mutex.unlock();
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 12th May 2008 at 20:13. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.