简体   繁体   中英

How to reliably kill a thread in Qt/C++?

I'm making a desktop programming game with C++11, Qt 5.6 (soon 5.7 once V-Play supports it), and QML. The user will be able to write arbitrary code to solve puzzles; however, the code should be entirely sandboxed and not cause problems with the rest of the application.

So I instantiate a scripting engine whose environment I completely control and run the player's code through that. I won't say what scripting engine because I don't want a solution to rely on the engine (plus, I may support multiple languages). When the player hits "Submit", I run the script asynchronously, so that the rest of the game is still responsive.

But here's my problem: What happens if the player's code takes a long time to run? Or worse, what if it's an infinite loop? The player will be making mistakes as they learn, so "they shouldn't do that" is not a valid answer here.

So I'll just let the player terminate their code at will, fine. But how can I do that without risking undefined behavior, memory leaks, a crash, or other things that may negatively affect the game?

Of relevance is the following:

  • The solution should be multi-platform.
  • The solution should not depend on what scripting engine I'm using.
  • I'm not passing around data between threads, so I don't need to worry about data races.
  • The QFuture s that QtConcurrent::run returns do not support QFuture::cancel .

You cannot rely on being able to safely kill C/C++ threads. Any thread which is doing any meaningful work will need to acquire locks and/or allocate memory to do its work; killing the thread will potentially leave some locks permanently held, or memory never deallocated. Depending on the details, this is likely to cause your application to run out of memory after many scripts are killed, or cause it to lock up entirely if the main thread tries to acquire locks held by a killed interpreter thread.

If you need to be able to interrupt running scripts, you will need to choose scripting engines which specifically allow for this. Not all will; you will need to avoid the ones that don't support it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM