Results 1 to 7 of 7

Thread: Using object created on main thread into another thread

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Using object created on main thread into another thread

    Hi,

    I'm using a pointer of an object that is created into the main thread on a worker thread.
    This object is a camera device that uses the network to capture images.
    There is another thread that uses the image to process it like producer consumer algorithm.
    On fast speeds of capturing images sometimes I loose images(I know this because I'm using a PLC to trigger the camera and counting the number of triggers).
    Could the use of the camera object on another thread that the thread that created the object be a problem?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using object created on main thread into another thread

    Accessing memory belonging to another thread without a locking mechanism preventing simultaneous access is asking for trouble.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using object created on main thread into another thread

    Hi,

    The camera object is only used on the worker thread but it is created on the main thread because I can have the camera opened and the worker threads stopped.

    Thanks,
    Òscar Llarch i Galán

  4. #4
    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: Using object created on main thread into another thread

    So why don't you create it in the worker thread?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using object created on main thread into another thread

    Hi,

    Reading the code again I have to say that it's not true that I'm not using the camera object into differetn threads. It's used on the main thread by a timer that every 500ms checks the camera inputs and also it's used into the "producer" thread to capture images and into the "consumer" thread to activate or deactivate some camera outputs.
    I will try to disable the timer to check inputs and also move the capturing process into the "consumer" thread to use only one thread.

    So why don't you create it in the worker thread?
    Really don't know how to open the camera there without doint on the run method. I want to mantain the camera opened when the thread is not running. I can't use QObject::moveToThread because the camera object does not inherit from QObject.

    Thanks,
    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using object created on main thread into another thread

    Hi,

    Finally I found on new documentation that the camera object must not be shared between threads(and what if I have the old one?!!). In this moment it's not shared as it is only used on the worker thread but it still belong to the main thread. There are two examples of the library, first one creates the camera object on the worker thread and the second one creates the camera object on the CDialog(the examples are MFC, not Qt) and uses it into the worker thread. This second example uses the same way as I'm using. Now I remember another example that they provide that use the worker thread to paint images into the display and when I told them that it was not possible they said that it was a bad example
    So, thanks for your responses but in this moment I need to ask to library providers support about this problem. Monday will try to use this second version that creates the object on the main thread and usues it on the worker thread to see if it works.

    Thanks,
    Òscar Llarch i Galán

  7. #7
    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: Using object created on main thread into another thread

    I think it is enough if you simply protect your camera with a mutex.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    ^NyAw^ (26th May 2013)

Similar Threads

  1. Why my object stay in main thread?
    By honestapple in forum Newbie
    Replies: 4
    Last Post: 3rd April 2013, 04:29
  2. Replies: 1
    Last Post: 28th March 2012, 18:58
  3. Replies: 5
    Last Post: 22nd February 2011, 21:21
  4. Replies: 9
    Last Post: 28th November 2009, 20:31
  5. Replies: 16
    Last Post: 7th October 2009, 08:17

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.