mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-18 08:07:08 +08:00
Terminate threads before to exit main()
It is very difficult and risky to assure that a running thread doesn't access a global variable. This is currently true, but could change in the future and we don't want to rely on code that works 'by accident'. The threads are still running when ThreadPool destructor is called (after main() returns) and this could lead to crashes if a thread accesses a global that has been already freed. The solution is to use an exit() function and call it while we are still in main(), ensuring global variables are still alive at threads termination time. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
@@ -196,9 +196,9 @@ void ThreadPool::init() {
|
||||
}
|
||||
|
||||
|
||||
// d'tor cleanly terminates the threads when the program exits.
|
||||
// exit() cleanly terminates the threads before the program exits.
|
||||
|
||||
ThreadPool::~ThreadPool() {
|
||||
void ThreadPool::exit() {
|
||||
|
||||
for (size_t i = 0; i < threads.size(); i++)
|
||||
delete threads[i];
|
||||
|
||||
Reference in New Issue
Block a user