Reputation: 18918
I am coding a boardgame in Qt where, after the player makes a move, the computer AI must pause and think for some time. However, while it is thinking, it seems the screen will not be updated until every line of code has been executed. Thus, the user would click on a square, see nothing happen for a few seconds, and then suddenly see the result of both his move and the computer's move.
In an attempt to fix this, I tried creating a new thread on which the AI runs its code, and then places its piece on the board. However, sometimes (and this is very inconsistent) the game crashes after the computer has made a move.
So can you guys either:
EDIT--I tried setting breakpoints as suszterpatt suggested, and the program seems to crash consistently in the debugger (it wasn't before I set the breakpoints).
Anyways, as I step through the program, it seems to go through the run
function fine, until it reaches the ending bracket, and then if I step through it jumps into line 317 on qthread_win.cpp
, which just says
finish(arg); //line 317
return 0;
If I step through that line, the debugger freezes up and Qt alerts me after 20 seconds. If I continue, I get the "This application has requested the Runtime to terminate it in an unusual way" message that I get when the program occasionally crashes when I'm not debugging.
What should I do now?
Upvotes: 1
Views: 2069
Reputation: 20881
The cause of the crash can be a variety of reasons but if I had to take a guess I'd say you're probably calling methods of a GUI object (label, textbox, your game board, etc.) from the AI thread.
The way threads communicate with one another in Qt is through a mechanism called signals and slots: the AI thread should expose a set of signals, i.e. 'beginThink', 'endThink', and the UI thread should register to those signals (with slots) and react accordingly. This is documented pretty throughly in the docs.
Upvotes: 5
Reputation: 14471
Try moving your code out of the separate thread. Once you have it working you can try moving it back and you will know any issues are threading related. I think your updating issue will go away if it's all on the same thread.
Upvotes: 0