Results 1 to 3 of 3

Thread: communicating between threads

  1. #1
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default communicating between threads

    I'm trying to acquire data which is stored in a buffer. The instance of the buffer is private to my producer thread. How will I be able to access this data in my consumer thread?

    This is the producer thread header.

    Qt Code:
    1. #ifndef COMPUTINGTHREAD_H
    2. #define COMPUTINGTHREAD_H
    3.  
    4. #include <QThread>
    5.  
    6. class ImageBuffer;
    7.  
    8. class ComputingThread : public QThread
    9. {
    10. public:
    11. ComputingThread();
    12. ~ComputingThread();
    13.  
    14. void haltComputing();
    15. void startTransmitting();
    16. void stopTransmitting();
    17.  
    18. protected:
    19. void run();
    20.  
    21. private:
    22. volatile bool halt;
    23. volatile bool transmit;
    24. ImageBuffer* imageBuffer;
    25. };
    26.  
    27. #endif
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: communicating between threads

    You can use signal/slots. Even with threads. Or you use a normal pointer and access the data via a getter function.

  3. #3
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: communicating between threads

    As said previously, you can emit a signal from your producer thread. If the receiving object has been created in your consumer thread, its slot will execute in the consumer thread's event loop. A pointer to your buffer can then be passed as argument, or just anything that would be relevant.

    Be careful that such inter-threads signals are by default asynchronous - that is, you have to make sure by yourself that the producer's buffer will remain valid until the consumer has finished with it. You can achieve synchronous behavior by playing with the type argument of QObject::connect().

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. Threads in Qt
    By freekill in forum Qt Programming
    Replies: 4
    Last Post: 11th November 2009, 18:49
  4. No context switch between Threads and parent ?
    By donglebob in forum Qt Programming
    Replies: 15
    Last Post: 29th October 2008, 22:49

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.