Results 1 to 9 of 9

Thread: 2 questions about threads

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: 2 questions about threads

    Anybody could tell me if it should be better if I use a mutex to lock this shared boolean variable when I consult/modify it?
    Any variable (memory) that can be accessed by more then one thread needs to me mutexed.
    The fact its running well is only a matter of luck.
    I once coded an application with 6 threads that would crashed every few days... it was a pain in the a** to find the problem.
    With if you have not mutexed shared variables, the behaviour is unpredictable.

    But I don't understand why do you use threads in the first place.
    You have only two tasks, and one is only done once.
    In case the the first task needs to run in parallel to the main thread, then you can do it in a thread, but you can do the other task in the main thread, reducing the complexity of your code dramatically.
    Don't use threads unless it is absolutly needed.

  2. #2
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 2 questions about threads

    Hi high_flyer, I missed to say that both threads doesnt' run at the same time. Each of both runs in different moments of the application. And it's strongly needed a thread for the first case because I need to do a very lengthy operation on background, while in the main thread the user can do another things.

    The reason that I don't use a mutex while I consult/modify the shared boolean in the first thread is because these operations are done in just one machine instruction so the cpu never changes the current operating thread while these operations are executed and that's why I think that never could cause a problem, am I wrong?
    Last edited by Dark_Tower; 18th March 2006 at 11:12.

  3. #3
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 2 questions about threads

    Any suggestions for the questions about the second thread?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: 2 questions about threads

    Hi high_flyer, I missed to say that both threads doesnt' run at the same time. Each of both runs in different moments of the application. And it's strongly needed a thread for the first case because I need to do a very lengthy operation on background, while in the main thread the user can do another things.
    You don't need a thread for that, you can use processEvents() in your lengthy task.

    In general threads are needed when more then one task needs to be executed in prallel.
    Is this the case you have?
    I am not sure from what you said till now...
    The reason that I don't use a mutex while I consult/modify the shared boolean in the first thread is because these operations are done in just one machine instruction so the cpu never changes the current operating thread while these operations are executed and that's why I think that never could cause a problem, am I wrong?
    Again, if the cpu stays in one thread, you don't need threads, as threads are used when you want other processes to work in parallel, i.e CPU switches between them.
    As answer to what you said, here, is that you can't really know/ have control over what the CPU does so your statmet may or may not be true, depends on the system and your application.

  5. #5
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 2 questions about threads

    thanxs high_flyer for your suggestions

  6. #6
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 2 questions about threads

    Quote Originally Posted by high_flyer
    Again, if the cpu stays in one thread, you don't need threads, as threads are used when you want other processes to work in parallel, i.e CPU switches between them.
    As answer to what you said, here, is that you can't really know/ have control over what the CPU does so your statmet may or may not be true, depends on the system and your application.
    That's not quite true. The setting and getting of his boolean variable will be an atomic instruction and hence thead-safe. He may need a mutex though if he has a function that sets/gets the value and then executes further statements that depend on the value not having changed.
    Save yourself some pain. Learn C++ before learning Qt.

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: 2 questions about threads

    What I said is true, and has nothing to do with atomic instructions but with the fact that if the CPU is doing a task (not instruction) where it is not needed to share CPU time with other tasks, threads are not needed.

  8. #8
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 2 questions about threads

    I was referring to the second part of your statement, not the first.
    Save yourself some pain. Learn C++ before learning Qt.

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. Memory management questions (im new to Qt)
    By scarvenger in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2007, 07:41
  4. Sql questions
    By Nb2Qt in forum Qt Programming
    Replies: 4
    Last Post: 15th February 2007, 22:53

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.