Results 1 to 3 of 3

Thread: Sharing between two Threads

  1. #1
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Sharing between two Threads

    When I create two instances of a Qthread derived class and run both threads.
    Which area/space do they share now?

    I know, that using static makes it possible. But without static variables?
    Just the pure QThread-class with "normal" private variables.

    I know its a bit stupid but,..all my tests showed my that they dont share anything.

    Example1:
    I counted on both instances an int var (private). No problem.

    Example2:
    I declared a var in run() and done sth with it on both threads...no problem.

    So where do they share sth?

  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: Sharing between two Threads

    Threads share code and data space. Each thread has an own stack. QThread objects themselves don't share anything. If you declare a global variable, it will be visible by both threads. If you want to avoid that, you can use shared memory. In both cases you'll have to synchronize those threads (using a global mutex so you won't escape from globals or statics).

  3. The following user says thank you to wysota for this useful post:

    donglebob (30th October 2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sharing between two Threads

    Ahh thats what I wanted to hear.
    I know that the theory says threads share...but i was confused with qthreads.

    In case of qthreads i must explicit share sth via global var, static var or shared mem.

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Sharing data between threads
    By bbui210 in forum Qt Programming
    Replies: 15
    Last Post: 19th October 2008, 17:56
  3. Sharing data across threads
    By jphn_crichton in forum Qt Programming
    Replies: 11
    Last Post: 5th May 2008, 18:29
  4. Once more: Threads in Qt4
    By high_flyer in forum Qt Programming
    Replies: 5
    Last Post: 9th August 2006, 18:35

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.