[vtkusers] a problem about multithread and VTK again

Lichan.Hong at parc.com Lichan.Hong at parc.com
Mon Apr 1 13:08:57 EST 2002


 
I did run into a very similar problem myself when writing my VTK-based Java application and was able to find a 
solution to solve my problem. 
 
Basically, as you have found out, the main reason that your idea doesn't work is that VTK is not thread safe.
One way that you can try is, once you enter the method associated with A, you essentially take over the
system event queue. Somewhere inside the infinite loop, you check the system event queue to see if button 
B has been pressed. For my application, this strategy works well with JDK1.3.1, but somehow doesn't 
work with JDK1.4.0. It seems to me that the system event queue is not implemented correctly in JDK1.4.0.
 
Another way to solve your problem is to get rid of the infinite loop in the method associated with A. When
button A is pressed, what you do is that you create and start a thread, say MyThread. After that, method A 
executes ONLY ONCE the code that you have in the original infinite loop. Now, when button B is pressed, 
you basically set some boolean flag in your main application. The key idea is how you design MyThread. 
What I would do is let the thread sleep for approximately the time which the code in the original infinite loop 
takes to execute. When the thread wakes up, it takes one of the following two choices. If the boolean flag 
in the main application has been set by method B, MyThread terminates without doing anything. On the 
other hand, if the boolean flag has not been changed, you basically ARTIFICIALLY generate a button A 
pressed event and add it to the system event queue of the main application. The system event queue 
will then do the scheduling job for you automatically and execute the code in method A. Personally, I 
found coding in this way is more elegant, since you may not want to mess up with the system event
queue.
 
Hope this helps.
 
Lichan Hong

-----Original Message-----
From: lian jiang [mailto:jl_vtk at yahoo.com]
Sent: Friday, March 29, 2002 9:00 PM
To: vtkusers
Subject: [vtkusers] a problem about multithread and VTK again



Hi, all: 

         I  posted the mail once but did not receive any response. So I have to post it again and hope someone can give me a help. Sorry to bother you all. 
         I met a problem about VTK rendering window and multithread.  In my JAVA-VTK program, I have two button, A and B. In the method associated with A, there is an infinite loop, in which the actors are rendered from the first frame to last frame repeatedly in a rendering window. I set a globale variable "boolean jump=false". The infinite loop should exit by judging the value of "jump". In the method associated with button B, I set the variable as true.
        When I click A, the program will enter the infinite loop. I intend to exit the loop whenever I click the button B. But here is a problem. Once the program enters the dead loop, the GUI seems to be stucked and cann't respond to any user action. So the method of B can not set any global variable. 
         From the view of thread, it is easy to understand--because the two buttons are both in the GUI thread, the dead loop will surely block the GUI thread so that button B is disabled.    I tried to solve the problem by multithread. Although the loop can exit as I imaged,  there still are some problems. If I put the dead loop in a thread, the rendering window can not be refreshed. IF I put the method of B into a thread, button B will also be disable once the program goes into the dead loop.  I don't know whether it is a common problem for all kinds of window refreshing.  I had studied multithread used in VTK for some time. As far as I know, VTK window will cause some problems in multithread because VTK calls opengl which is not safe for thread. But I am not sure whether the refreshing problem described above is caused by VTK or other reasons.  Is it a common problem for all kinds window refreshing?  Is there a way to solve this problem?  Thank you very much. 


BEST WISHES 

Lian 





  _____  

Do You Yahoo!?
Yahoo! Greetings - send greetings for Easter, Passover

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20020401/75e6e7cf/attachment.htm>


More information about the vtkusers mailing list