Results 1 to 7 of 7

Thread: Communication/data sharing between threads

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Communication/data sharing between threads

    Hi!

    EDIT: SKIP THIS PART I screwed up and it is not an issue anymore.
    I'm experiencing some serious difficulties that I perceive as very strange indeed!
    I'm programming a multi Thread app and all of a sudden the compiler starts to complain about "xxx" does not name a type when I declare "xxx" under private in my .h file. I can't figure out what the heck the difference is from half an hour ago.

    HERE IS THE PROBLEM:
    I have two threads (+ a GUI-thread) thread one and thread two.
    I can write and read back OK from thread one to Thread twos public variables. But when I try to read them in thread two I get nonesense result: 7.11756e-307.
    How could this be?

    EDIT2:
    When I write to thread twos public vars from the GUI-thread it sticks and is the same when read back from within thread two... Does thread one lack some kind of writing-rights? I think it's strange that it can write and read back the same result, though.
    Perhaps it is easier to only use the GUI-thread's public vars for inter threaded communication but I cant seem to get that going either...

    Cheers!
    /Tottish
    Last edited by Tottish; 14th April 2010 at 16:43.

  2. #2
    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: Communication/data sharing between threads

    If You want to communicate betwen threads with public vars You must synchronise them. Ie. with QMutex

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication/data sharing between threads

    As Lesiok states, global variables must be suitably protected so that they are only used by one thread at a time otherwise you will see undesired effects. See the QMutex documentation for examples of why and how.

  4. #4
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication/data sharing between threads

    Yeah, I've read that but I was lead to believe that this was only necessary when the variables in question had multiple threads that wanted to write to them. Only one of my threads write to a variable and the other threads are strictly readers.
    Isn't that true?

    /Tottish

    EDIT: Tested with mutex. No difference.
    Last edited by Tottish; 15th April 2010 at 09:40.

  5. #5
    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: Communication/data sharing between threads

    Show us some code : declaration of this variables and code accessing them. The glass ball is not working today

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication/data sharing between threads

    If you have a thread that writes and a thread that reads, you need locking unless the variables are simple types such as 'char'.

    Have a look at QReadLocker & QWriteLocker. The former locks for read access, the later locks for write access.

  7. #7
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Communication/data sharing between threads

    dear Lesiok,
    I have some how such a problem with data sharing with different threads.
    but the difference is that when I wanna read a static array in different array they know it by different address :-??

Similar Threads

  1. Communication between Threads
    By Wan-Hi in forum Newbie
    Replies: 11
    Last Post: 24th January 2013, 13:52
  2. Sharing between two Threads
    By donglebob in forum General Programming
    Replies: 2
    Last Post: 30th October 2008, 09:58
  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. Threads communication
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 31st March 2006, 15: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.