PDA

View Full Version : Using object created on main thread into another thread



^NyAw^
24th May 2013, 23:44
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,

ChrisW67
24th May 2013, 23:51
Accessing memory belonging to another thread without a locking mechanism preventing simultaneous access is asking for trouble.

^NyAw^
25th May 2013, 00:02
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,

wysota
25th May 2013, 08:25
So why don't you create it in the worker thread?

^NyAw^
25th May 2013, 12:00
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,

^NyAw^
25th May 2013, 17:59
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,

wysota
25th May 2013, 22:26
I think it is enough if you simply protect your camera with a mutex.