Yes its in different thread. I am fetching few records from data base & playing with it main thread besides I am launching a thread parallel which fetches all the data from data base in thread.
Why reference: I need the data in my class (from where I created my thread & start) so I am sending a vector of vector as reference & In thread it fetches all the data once my thread finishes I have all the data in my class, I am using reference here just to avoid one more finished signal with the data as argument (to avoid copy).
Thanks :-)
In your example both sender and receiver are "this" so if you don't want to copy, why not just have the data as a member?
In any case, if you really must allocate the data structure on the main thread, then hand it to the secondary thread, you might want to do that via pointers, the reference cannot be stored, so even with a direct connection it will only be available in the slot, not in any code executed by the other thread.
Still a very strange pattern, maybe some more code would clarify what you are actually trying to do?
Cheers,
_
There was a typo in my question (i directly copied from my project code & while changing it I messed up) & here is the simplified code
Qt Code:
Ex: typedef std::vector< std::vector<int> > myData; qRegisterMetaType<myData>("myData"); class testClass // I want to pass this obj as reference { public: int a; }; //In my main thread (myclass) : QThread m_thread & workerClass m_worker are the class members & below is constructor code connect(&m_thread, SIGNAL(started()), this, SLOT(startThread())); connect(this, SIGNAL(startMyThread(myData&)), &m_worker, SLOT(heavyWork(myData&))); m_worker.moveToThread(&m_thread); m_thread.start(); void myclass::startThread() { myData obj; emit startMyThread(obj); }To copy to clipboard, switch view to plain text mode
So here I am getting same error:
when I make it direct connection (connect(this, SIGNAL(startMyThread(myData&)), &m_worker, SLOT(heavyWork(myData&)), Qt:QObject::connect: Cannot queue arguments of type 'myData&'
(Make sure 'myData&' is registered using qRegisterMetaType().)irectConnection)
, the slot is getting executed in the main thread.
i am just looking for a way to pass reference to the thread. i can do it with the pointer as you suggested, But as I am using vector of vector, using pointer to the vector of vector may complicate or confuse or may lead to some memory leaks which I don't want & exactly for the same reason we have reference.
Last edited by prasad_N; 30th December 2015 at 17:40.
Thanks :-)
You may not need them; however, if your data needs to marshalled across a thread boundary you might need them if they aren't already defined for std:: vector<>Sorry but I did not get how implementing this operators related to my problem.
Passing a reference is possible, just not via a queued connection.
A queued connection needs to transfer the data via an event, so each signal argument needs to be stored in a QVariant and then put into an event that is sent to the receiver via the receiver thread's event loop.
There the arguments are extraced again and the slot is called.
Aside from references being very in appropriate due to implicit life time assumption (easily leading to destroy before use like in your code), they cannot be stored (neither in a QVariant but also not in a normal member variable).
What feature?
To store a reference like a pointer? That you would have to take up with the C++ standard comittee.
Cheers,
_
you are referring to below function I think & myData obj; is a class member in my actual code & It never gets destructed before thread finishes. again this is due to writing some code on fly sorry for that.
So what connect should I use, Direct connection making slot to execute in main thread. I think other connections more or less same as these 2 connections except unique connection.Qt Code:
void myclass::startThread() { myData obj; //this is class member in actual code & never gets destructed before thread finishes, I just wanted to show the reference variable problem here so I just didn't care this emit startMyThread(obj); }To copy to clipboard, switch view to plain text mode
passing reference to thread when It is queued connection if there is no other way.
Thanks :-)
myData obj is destroyed immediately after emiting signal. Slot in another thread is run in the future when the object referenced by reference no longer exists.
Then you should probably not have used a local variable definition.
It depends on your requirements.
If you want the slot to execute in the context of the emitter thread, use a direct connection.
If you want to use the signal/slot bridge as a mean to conveniently copy data between threads, use a queued connection.
I wouldn't get my hope up. If the C++ Standard committee wanted to allow assigment of reference variables, that would have been possible a long time ago, especially since that has always been possible for pointers.
I.e. if C programmers want to refer to the same object from two different variables (aliasing), they use pointers.
C++ programmers can also use pointers and can use references for the special case where the alias can be initialized with the address of the object, e.g. passing a reference to a constructor, storing it in a reference member.
Cheers,
_
@prasad_N:Then you should probably not have used a local variable definition.
Or at least written it as a commented-out line, with an explanation that "obj" is a class member variable. How are we to understand that when you write it as you did, you weren't posting your actual code? A lot of the discussion here was based on your incorrect posting, and it could have been avoided if you had posted the actual code in the first place.
And this has the added benefit over storing a pointer member variable in that you are absolutely assured that the member variable is a valid reference to an object instance, whereas anything can be assigned to a pointer member, including nothing.C++ programmers can also use pointers and can use references for the special case where the alias can be initialized with the address of the object, e.g. passing a reference to a constructor, storing it in a reference member.
There was a mistake & people are over emphasizing on that rather than a actual question, Anyhow I will take care from next time while posting.
None of them are not my requirements. As per my requirement & Replay's from you guys I think there is no solution for this at least for now (passing reference across threads if it it is queued connection).
Thanks :-)
Yes, the only reference that can be stored is a pointer.
Cheers.
_
Bookmarks