how to use thread

Just got a problem which wasted me some time. The problem is just:

 

Regarding a procedure A, which mainly implements a work B, I want to use a progress bar to show the progress of the work of B, because A need to run a lot of sub functions and take a lot of time. So inside A, right before executing the code directly related to B's work, I insert a few code to create a thread C to display the progress of B, and after the code of creating the thread C, I continue to put A's original code.

 

I thought C will work with A's code at the same time(sychronization), since it is thread. However the result is, although thread C starts to work, but just for one second, then A's code takes over CPU until A gets finished, and then B continues. This makes using thread C no sense.

 

Then I found the reason: because thread C is created inside the code of A, then CPU have more priority for A. It always tries to finish more work of  A.

 

Solution:

I put the code in A related to work B out and make it a separate thread D to do the work. Then in procedure A, I create the thread C at first, and then create thread D (which does the work B). Now these two threads have the same prority, and then their run are sychoronized.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章