Results 1 to 14 of 14

Thread: Two threads need to print on main window

  1. #1
    Join Date
    Jun 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Two threads need to print on main window

    Hi all,

    Can u help me with my task ? i need to make GUI app, which will cout on window:

    Thread1
    Thread2
    Thread1
    Thread2

    i made a window which print Thread1 all the time, i made new object, with printing thread2, made a connection, but it doesn't work. I'll be glad if some1 will help me. in attach u can find some cpp files.i think i can't to that (dialog.cpp line 12,13)
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Two threads need to print on main window

    "It doesn't work" is not a very useful problem description.

    What does happen and what do you expect to happen?

    Cheers,
    _

  3. #3
    Join Date
    Jun 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Two threads need to print on main window

    Finally i made some progress. Sorry for my lack of information. Right know i have two threads, and i need them to print something like that:
    Thread1
    Thread2
    Thread1
    Thread2

    Im making this as GUI App. Right now i don't know how to make global QMutex to lock them, cause right now it's like:

    Thread1
    Thread1
    Thread1
    Thread2
    Thread2
    Thread1
    …..

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two threads need to print on main window

    Class MyThread and MyThread2 should be derived from one class which will have a defined static QMutex

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Two threads need to print on main window

    One thing that would work is to have two semaphores.
    The first thread acquires the semaphore1 and releases semaphore2, the second thread does the opposite. The string sending happens in between the two semaphore operations in both threads.
    After start, the main thread releases semaphore1, thus allowing the first thread to start with the ping-pong.

    Cheers,
    _

  6. #6
    Join Date
    Jun 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Two threads need to print on main window

    I'm trying to resolve this problem with QSemaphore. I made two global semaphores ( first, second), and my thread methods look like):
    Qt Code:
    1. void MyThread::thread()
    2. {
    3. for (int i = 0 ; i < 100; i++)
    4. {
    5. // stop here and wait for first available
    6. if (first.available())
    7. {
    8. first.acquire(1); // first is now 0, so it's not available (blocked)
    9. numberChange(QString("Thread 1"));
    10. second.release(1); // release second semaphore, so he can start
    11. }
    12. }
    13. void MyThread::thread2()
    14. {
    15. for ( int i = 0; i < 100; i++)
    16. {
    17. if (second.available())
    18. {
    19. second.acquire(1); // after acquire this semaphore should be block untill first thread is done
    20. numberChange(QString("Thread 2"));
    21. first.release(1); // release first semaphore so void MyThread::thread() can lock his semaphore
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    Is my thinking correct ? What I'm missing ?
    My main problem is' how to make them stop and wait. Right now it's looks like they don't communicate correctly.

    Thanks you
    Last edited by anda_skoa; 29th June 2014 at 18:34. Reason: missing [code] tags

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Two threads need to print on main window

    That looks about right, though the available() checks do not make them stop as the comment suggested. Just remove them

    How did you create them and how did you pass them to the two thread objects?

    Cheers,
    _

  8. #8
    Join Date
    Jun 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Two threads need to print on main window

    Hi Anda,

    When i removed available() i get deadlock after first cout "Thread 1".


    I made a new class with .h and .cpp

    h.
    #ifndef SEMA_H
    #define SEMA_H
    #include <QSemaphore>

    extern QSemaphore first;
    extern QSemaphore second;


    #endif // SEMA_H

    .cpp
    #include "sema.h"

    QSemaphore first(1); // is this a binary semaphore ?
    QSemaphore second(0); // 0 cause in void MyThread::thread() i have "second.release(1)". 0 should prevent after first loop to acquire "second.available(2)";

    I have found that, if first or second is not available, the loop is still going, and i think i need to stop it and wait for another semaphore.

    Maybe here is the problem. I found this global semaphore "solution" on web.
    But this don't work as i want, so i'm doing something wrong.

    Thank you Anda for quick reply
    Last edited by zychu02; 29th June 2014 at 19:16.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Two threads need to print on main window

    Global variables are not very nice but should work nevertheless.

    Can you attach a minimum buildable example that shows the problem?

    Cheers,
    _

  10. #10
    Join Date
    Jun 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Two threads need to print on main window

    First .h files

    Second part .cpp files
    Attached Files Attached Files

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Two threads need to print on main window

    You are not starting the threads, you run two mehods in the main thread, one after the other.

    To start a thread call its start() method.
    This requests a platform thread and lets it execute the run() method.

    Cheers,
    _

  12. #12
    Join Date
    Jun 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Two threads need to print on main window

    in mythread.h i can change first void thread() to void run(), and in cpp too. But what with the second ? I cannot have two run() methods. How to solve this . I think i'll need to buy some fine quality beers for your help Anda.

    Thank you,
    Last edited by zychu02; 30th June 2014 at 08:17.

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Two threads need to print on main window

    There are a couple of option:

    - Two thread classes
    - Switching behavior depending on an argument passed to the thread class constructor
    - A templated thread class that delegates to a method of an instance of the template argument
    - Two implementations of QRunnable and a dedicated threadpool with two threads.
    - Something similar but with two dedicated threads.

    The second one is probably the easiest given your current code.
    The run() method evaluates the stored argument value and then calls either thread() or thread2()

    Cheers,
    _

  14. #14
    Join Date
    Jun 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Two threads need to print on main window

    Anda..... it's working ! Thank you so much, for your time and patience. Send me private msg with your address so i can send you "Żubrówka" bottle.

    Thank you again, have a nice day,

Similar Threads

  1. Replies: 2
    Last Post: 17th February 2011, 12:30
  2. Replies: 3
    Last Post: 23rd December 2010, 06:55
  3. Replies: 2
    Last Post: 4th August 2010, 19:10
  4. Replies: 11
    Last Post: 11th August 2008, 09:14
  5. Replies: 15
    Last Post: 23rd March 2007, 16:16

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.