Results 1 to 4 of 4

Thread: problems with Threads.

  1. #1
    Join Date
    Feb 2009
    Posts
    7
    Qt products
    Platforms
    Unix/X11

    Default problems with Threads.

    how can i run ThreadA and block it and then run ThreadB and block it and again run ThreadA and block it.... upto 5 times

    I run two threads A and B simultaniously in my 'thereadmain.cpp' and i run two threads upto 5 times in thread.cpp. now my required output is
    A
    B
    A
    B
    A
    B
    A
    B
    A
    B
    But Its Prints : Thread A
    and Finished ,
    Finished.
    is it possible to print my required output through semaphores..
    i want to reun the above program without using sleep().
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problems with Threads.

    You are doing it the wrong way - you should acquire the semaphore at the beginning of your operation and release it afterwards. Unfortunately this will not cause both threads to do their work in sequence of A-B-A-B-etc. For that you need two semaphores or wait conditions. Each thread has to wait on one condition (and the other on the other) and when you want the other thread to run, signal the appropriate condition.

  3. #3
    Join Date
    Feb 2009
    Posts
    7
    Qt products
    Platforms
    Unix/X11

    Default Re: problems with Threads.

    i try with two semaphores but my output A-B-A-B.. is not coming.The code is below and where i release the semaphores for getting the proper result.

    Qt Code:
    1. void MyThread::run()
    2. {
    3.  
    4. for(int lCount = 0; lCount < 5; lCount++)
    5. {
    6. if(messageStr == "A")
    7. {
    8. cout << "lCount in ThreadA:" << lCount << endl;
    9. iSemA->acquire(1);
    10. cout << "Thread : " << messageStr.toAscii().data() << endl;
    11. iSemB->release(1);
    12. }
    13. else
    14. {
    15. cout << "lCount in ThreadB:" << lCount << endl;
    16. iSemB->acquire(1);
    17. cout << "Thread : " << messageStr.toAscii().data() << endl;
    18. iSemA->release(1);
    19. }
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 9th February 2009 at 10:44. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problems with Threads.

    You have to set one semaphore to 0 and the other one to 1 initially so that only one thread can run at the beginning. Otherwise you'll never be acquiring a semaphore when it's at 0. I would use wait conditions here though. Or a single thread...

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. canonicalFilePath() thread problems
    By magland in forum Qt Programming
    Replies: 10
    Last Post: 5th December 2007, 21:22
  4. Problems with threads and windows
    By SkripT in forum Qt Programming
    Replies: 15
    Last Post: 16th January 2006, 17:46

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.