I use a new thread spawned from main thread to load library, which may take a long time (about 10 seconds) to load some libraries. When loading these libraries, users may close the window, so what's the best way to kill a thread like this safely and immediately? Of course, I don't want the resource leak to occur.
Asked
Active
Viewed 43 times
0
-
Why do you want to close the thread? Also, please provide a [mcve]. – mjwills Mar 12 '18 at 11:54
-
1You don't kill threads. You let them naturally complete. This means you signal to the code that's running to stop, and bail out of whatever work you're doing. There's no magic here. You need to write code to get your thread to respond to a "finish" signal. – spender Mar 12 '18 at 11:54
-
Exiting the main thread, will kill any other thread, as spender said, if you want to clean resources o make sure a operation is completed, you need to handle this by yourself – MrVoid Mar 12 '18 at 11:55
-
Now i use a thread to load the chromium embedding framework library, which may take about 10 seconds to finish. At this time, if user select to close Microsoft Word, it seems that it cannot be closed until the library finishes loading. – Lucas Mar 12 '18 at 12:35