Saving kernel data in a thread, how to ensure its recording before exiting the game?

So, I save small images for the main data, which take very little time to save, for example .2 seconds, but I do this while the user views the scroll, therefore, to improve responsiveness, I Move the save to the stream. This works great, everything is saved and the application is responsive. However, there is one thing in the kernel-data + multithreading doco that bothers me:

"In Cocoa, only the main thread is not disconnected. If you need to save other threads, you must write additional code so that the main thread does not allow the application to stop working until the completion of the entire save operation."

Alright, how are you doing this? It should last only ~ 0.2 seconds, and it rarely happens, since the likelihood that the application will cease to exist somehow saves very little. How can I run something in the main thread that will prevent the application from exiting and not block gui?

thanks

+4
source share
1 answer

Make your save stream the "Save in progress" checkbox selected and check the main stream in the delegation application of the WillTerminate: method application. Obviously, you need to use a mutex to synchronize access to a character between two threads.

If saving is performed when the application tries to exit, the main thread does pthread_cond_wait; the save thread will wake it with pthread_cond_signal after the save is complete.

+3
source

Source: https://habr.com/ru/post/1306300/


All Articles