Results 1 to 4 of 4

Thread: How do I access data between threads

  1. #1
    Join Date
    Dec 2009
    Posts
    29
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default How do I access data between threads

    I want to use a QUDPSocket inside a QThread and I need data to be available to both the qthread and main thread. To be thread safe, do I need to send information from one thread to another via signals or can I just have them reside in either one of them and acces them from both threads. there is no chance that data will be accessed simultaneously.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I access data between threads

    If you use signals and slots you also have to worry about thread safety. Although the signal/slot system itself is thread-safe, if you use direct connections across threads you still have to lock the data. If you are absolutely 100 % sure that your program isn't going to access the same data from different threads at the same time you can leave out locking. If there is a chance your code might, use locking. It's safer that way.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    658
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I access data between threads

    Quote Originally Posted by yodasoda View Post
    I want to use a QUDPSocket inside a QThread and I need data to be available to both the qthread and main thread. To be thread safe, do I need to send information from one thread to another via signals or can I just have them reside in either one of them and acces them from both threads. there is no chance that data will be accessed simultaneously.
    signals is the best way to transfer data from worker thread to main thread .... check blockingfortuneclientexample ... it will help u a lot ...
    "Behind every great fortune lies a crime" - Balzac

  4. #4
    Join Date
    Dec 2009
    Posts
    29
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: How do I access data between threads

    thanks. I would like to talk about the difference of having a QThread be instantiated in QMainWindow and a QThread being instantiated in main.cpp. It seems there is a big difference between both and one is not a full separate thread in itself. Also, I would of like to use waitforreadyRead with QThread inside QMainWIndow but then the function sometimes gives me a "glibc detected double free exception"

Similar Threads

  1. QThread Data Access Method
    By schan117 in forum Qt Programming
    Replies: 7
    Last Post: 18th August 2009, 07:25
  2. Replies: 2
    Last Post: 18th December 2008, 08:43
  3. Sharing data between threads
    By bbui210 in forum Qt Programming
    Replies: 15
    Last Post: 19th October 2008, 18:56
  4. Sharing data across threads
    By jphn_crichton in forum Qt Programming
    Replies: 11
    Last Post: 5th May 2008, 19:29
  5. how to access QCheckTableItem data with in a QTable.
    By ashukla in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2007, 12:55

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.