# HG changeset patch # User koda # Date 1365878070 -7200 # Node ID d3033e3a4113aac118ad28b433dcdde483bbfc9d # Parent 7e9c6efe90fd1e726ac8dece228944a41947df65 let's try to kill the thread right away and protect the thread variable with a mutex diff -r 7e9c6efe90fd -r d3033e3a4113 hedgewars/uAI.pas --- a/hedgewars/uAI.pas Sat Apr 13 20:13:52 2013 +0200 +++ b/hedgewars/uAI.pas Sat Apr 13 20:34:30 2013 +0200 @@ -36,15 +36,18 @@ var BestActions: TActions; CanUseAmmo: array [TAmmoType] of boolean; StopThinking: boolean; - ThinkThread: PSDL_Thread = nil; StartTicks: Longword; + ThinkThread: PSDL_Thread; + ThreadLock: PSDL_Mutex; procedure FreeActionsList; begin AddFileLog('FreeActionsList called'); + SDL_LockMutex(ThreadLock); if (ThinkThread <> nil) then - SDL_WaitThread(ThinkThread, nil); - ThinkThread:=nil; + SDL_KillThread(ThinkThread); + ThinkThread:= nil; + SDL_UnlockMutex(ThreadLock); with CurrentHedgehog^ do if Gear <> nil then @@ -360,7 +363,7 @@ switchImmediatelyAvailable: boolean; Actions: TActions; begin -AddFileLog('Thread started'); +AddFileLog('Think thread started'); dmgMod:= 0.01 * hwFloat2Float(cDamageModifier) * cDamagePercent; StartTicks:= GameTicks; currHedgehogIndex:= CurrentTeam^.CurrHedgehog; @@ -440,7 +443,9 @@ end; Me^.State:= Me^.State and (not gstHHThinking); +SDL_LockMutex(ThreadLock); ThinkThread:= nil; +SDL_UnlockMutex(ThreadLock); Think:= 0; end; @@ -471,8 +476,10 @@ end; FillBonuses((Me^.State and gstAttacked) <> 0); -AddFileLog('Enter Think Thread'); + +SDL_LockMutex(ThreadLock); ThinkThread:= SDL_CreateThread(@Think{$IFDEF SDL13}, 'think'{$ENDIF}, Me); +SDL_UnlockMutex(ThreadLock); end; //var scoreShown: boolean = false; @@ -519,14 +526,13 @@ begin StartTicks:= 0; ThinkThread:= nil; + ThreadLock:= SDL_CreateMutex(); end; procedure freeModule; begin - if (ThinkThread <> nil) then - SDL_KillThread(ThinkThread); - ThinkThread:= nil; FreeActionsList(); + SDL_DestroyMutex(ThreadLock); end; end.