|
From: | Michael Goffioul |
Subject: | Re: Passing variables up to the GUI |
Date: | Sun, 14 Apr 2013 18:17:23 -0400 |
On the contrary, I think it's the heart of the problem. The line
"rep->count++" is the critical one in the above code. While one thread
is executing it, another one can be doing "rep->count++" or
"--rep->count". Those operations are not atomic, takes several CPU
cycles and can be interrupted by other threads. The problem is that the
resulting values rep->count is then undetermined.
That's true if we're talking OS threads. However, Octave is running in a QThread, not an OS thread.
I'm assuming that Qt has done some kind of magic behind the scenes to make that signal/slot mechanism work out. How do you know that the copy-constructor is done in one thread and the destructor is done in another thread?
Yes, but that Matrix representation is associated with the octave_value. If there is nothing in the worker thread that is going to touch that octave_value representation (that was a condition of what I proposed) then the GUI thread is free to increase the rep count, so long as it decreases the object's thread count before returning.
1) Create a "local" duplicate of the octave_value in the routineYour question is the critical one. My supposition is hinged on the fact that Qt has to do something to make cross-thread signals work. Hence they have this QThread class. That question is still in air here. I've searched the Internet for that answer, but haven't found any just yet.that processes the desired command, on the stack or via "new" or
whatever. That has a rep count of 1.
2) Emit the signal that creates a copy of that local octave_value,
thereby increasing the rep count to 2.
3) Destroy the local copy off the stack or via "delete". That
brings the rep count back down to 1. *And* from this point forward
there is no code inside the Worker thread that will touch the
representation data of the octave_value object because it has gone
out of scope to any remaining code in the thread.
4) Sometime later the slots are processed in the GUI thread and that
representation data still exists, unaltered by anything in the
Worker thread.
5) All slots finish, and Qt calls the destructor for the
octave_value. The representation count goes down to 0 and the data
is deleted from memory.
What makes you think 3) and 5) won't be executed concurrently?
[Prev in Thread] | Current Thread | [Next in Thread] |