Recently, I used MiniGUI to build a program with threads. This program is used to browse images in folders.
In a window, it is very time-consuming to load a large image using loadbitmapfromfile, so a thread is opened during window initialization to load the image, to ensure that the main thread does not block the image loading process.
However, in practice, you must consider the following problem: When I right-click the image to request the thread to load the next image, it takes some time to load the image. If I press the exit key to exit the window; when exiting the window, all the data (variables) in the window will be destroyed, and the image loading thread may still use the data (variables) in the window, so that there is a conflict between the two, this may cause the program to crash.
The solution is to avoid the image loading thread from using the global data of the window, or copy the imported global data as the local data of the thread first, because the thread time is mainly spent on loadbitmapfromfile, as long as the functions such as loadbitmapfromfile use local variables of the current thread rather than global variables of the window, conflicts can be avoided.
In addition, you can set an ID variable. When the window exits, the ID variable is set to 1, and the thread adds a judgment: If the ID variable is 1, the thread exits immediately.
Summary:
The problem is: Conflicts between the parent thread and the Child thread on data usage
Solution: first, the sub-thread tries its best to use local variables. The input main thread variables should also be copied as local variables for use.
Second, set an identifier variable to notify the subthread when the parent thread exits.
Related simplified code:
Void loadbitmapthreadfunc (void * PTR) // image loading thread <br/>{< br/> printf ("loadbitmap thread start ...... /n "); <br/> char * selimagename; <br/> bitmap BMP; <br/> int COUNT = 0; <br/> pthread_detach (pthread_self ()); </P> <p> loadbitmapthread_t * pdata = (loadbitmapthread_t *) PTR; <br/> while (pdata-> ustartflag) <br/>{< br/> If (pdata-> ucrestart = 1) <br/>{< br/> sem_wait (& (pdata-> lock )); <br/> pdata-> ucrestart = 0; <br/> pdata-> stopflag = 0; <br/> char file_path [0, 255]; <br/> selimagename = pdata-> currdirectroy-> array [pdata-> sel]-> filename; <br/> strcpy (file_path, pdata-> currdirectroy-> path_name ); <br/> strcat (file_path, "/"); <br/> strcat (file_path, selimagename); <br/> printf ("file_path = % s/n ", file_path); <br/> If (pdata-> stopflag = 0) // ID variable for thread exit <br/>{< br/> If (count) <br/>{< br/> # If 0 <br/> unloadbit Map (& BMP); <br/> # else <br/> gfimage_unloadbitmap (& BMP); <br/> # endif <br/>}< br/> If (! Loadbitmapfromfile (hdc_screen, & BMP, file_path) // BMP is a local variable <br/>{< br/> count ++; <br/> If (pdata-> stopflag = 0) <br/> {<br/> sendmessage (pdata-> hwnd, pai_setimage, (wparam) & BMP, 0); <br/> invalidaterect (pdata-> hwnd, null, true ); <br/>}< br/> else <br/> {<br/> If (pdata-> stopflag = 0) <br/>{< br/> sendmessage (pdata-> hwnd, pai_setimage, 0, 0); <br/> invalidaterect (pdata-> hwnd, null, true ); <br/>}< br/> printf ("Send message over/N "); <br/>}</P> <p> pdata-> stopflag = 0; <br/> sem_post (& (pdata-> lock )); <br/>}< br/> else <br/>{< br/> usleep (10000 ); <br/>}< br/> # If 0 <br/> unloadbitmap (& gfile_image ); <br/> # else <br/> gfimage_unloadbitmap (& BMP); <br/> # endif <br/>}</P> <p>
Add loadbitmapthreadbuf. stopflag = 1 to the exit code of the parent thread;