Results 1 to 8 of 8

Thread: Does it make sense to use a QSemaphore for acessing data inside the same thread?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Does it make sense to use a QSemaphore for acessing data inside the same thread?

    If you have only one thread, then it is impossible for the value of any variable to change in between writing it and reading it. If you write a value to the variable, it will never change no matter how many times you read it, until you write it again.

    I think you don't understand the concept of a thread. It is a single continuous string of execution of instructions from beginning to end. It doesn't matter whether you have branches or loops, signals, slots, whatever, only one instruction ever gets executed at any time. The situation you are imagining, where a variable might get changed in between writing and reading it, within the same thread just simply can't happen. If the read instruction follows the write instruction in the execution sequence, that value that is read will be the value that was just written. There's no way for some gremlin to sneak in and change the value.

    So there is absolutely no need for semaphores or any other kind of access protection in a single-threaded program.

    But as soon as you create a program with more than one thread, and you share a memory location among them, then if any thread writes to the location, then you must protect it.

  2. The following user says thank you to d_stranz for this useful post:

    Momergil (7th January 2014)

  3. #2
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    165
    Thanked 2 Times in 2 Posts

    Default Re: Does it make sense to use a QSemaphore for acessing data inside the same thread?

    Hello d_stranz,

    well, so I was guessing And I imagine that the fact the slot that changes a varible's value was connected to a signal from another thread don't change the situation, correct?

    Well, that was my doubt =]

    Thanks,

    Momergil

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Does it make sense to use a QSemaphore for acessing data inside the same thread?

    If your app has only one thread, then all the signals and slots are in that thread. There is no between-threads connection. There is no magic about signals and slots. They are just C++ method calls underneath all the Qt wrappings.

Similar Threads

  1. Replies: 1
    Last Post: 28th March 2012, 18:58
  2. what would make more sense?
    By jajdoo in forum Newbie
    Replies: 1
    Last Post: 27th July 2011, 22:54
  3. Replies: 7
    Last Post: 27th January 2011, 08:48
  4. Replies: 7
    Last Post: 13th November 2010, 07:58
  5. Replies: 6
    Last Post: 29th April 2009, 18:17

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.