Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, November 2007
how to update global variable in qthread?


Message 1 in thread

Hi

In my apps, a global var will be updated in qthread,and main thread
will do something according to the global var. The thread's time
consuming. So when main thread go to the
' if( global_var == somevar ) ', the global var has not been updated
in the thread,so always error
If I wait the thread,the main GUI will block. So how can i do in this situation?

Thanks.

-- 
 [ signature omitted ] 

Message 2 in thread

On Friday 02 November 2007 14:51:01 jiang jefix wrote:
> Hi
>
> In my apps, a global var will be updated in qthread,and main thread
> will do something according to the global var. The thread's time
> consuming. So when main thread go to the
> ' if( global_var == somevar ) ', the global var has not been updated
> in the thread,so always error
Why not just get the worker thread to emit a signal when it has done what it 
needs to do? You can then connect to this signal in the main thread which can 
then do it's processing safe in the knowledge that the thread has finished 
its work. You could even pass the important variable as the argument in the 
signal, remocing the need for a global variable.

> If I wait the thread,the main GUI will block. So how can i do in this
> situation?
Indeed this is the situation that splitting the work into a separate thread 
hopes to avoid. :-)

Hope this helps,

Sean

--
 [ signature omitted ] 

Message 3 in thread

On 11/2/07, jiang jefix <jefix214@xxxxxxxxx> wrote:
> ' if( global_var == somevar ) ', the global var has not been updated
> in the thread,so always error
Leaving alone the fact that global variables are inherently bad, you
need to declare it as "volatile" otherwise compiler can optimize out
the fetching from a memory every time you access the variable, for
example by placing it in the register. "Volatile" qualifier will tell
compiler to assume nothing about this variable and leave our all
optimizations based on possible assumptions (in price of performance
hit, of course).

-- 
 [ signature omitted ]