0

I'm creating a thread for my game. But I found, that if close button is pressed, or the task is killed I cannot properly finish the work, deallocating all the resources I needed inside the program.

I found that Close handler exists, but the example given is an unknown magic to me, because I need to create something similar in ANSI-C.

   static BOOL CloseHandler(DWORD evt)
    {
        if (evt == CTRL_CLOSE_EVENT)
        {
            m_bAtomActive = false;

            // Wait for thread to be exited
            std::unique_lock<std::mutex> ul(m_muxGame);
            m_cvGameFinished.wait(ul);
        }
        return true;
    }

I know that Winapi has Mutex and conditional variables, but I do not know anything about std::atomic equivalent.

I have this thread start function and inside thread function GameThread I have a regular bool variable m_bAtomActive checked now.

void Start(void* _self)
{
    DWORD dwThreadID;
    HANDLE hThread;

    struct c_class* this = _self;
    this->m_bAtomActive = true;

    hThread = CreateThread(
        NULL,
        0,
        &GameThread,
        _self,
        0,
        &dwThreadID);

    WaitForSingleObject(hThread, INFINITE);
}

I'm bad at threading, what should I do to properly finish the work of my game?

All the additional details will be provided in the comments or on the chat.

UPD: The first thing seems to be easy, it is solvable with this line inside close handler

if(Active)
            InterlockedDecrement(&Active);

But the second and third is still under question. They might be created for the reason of CloseHandler killing the app before destruction, but I don't know for sure.

  • This answer could help you https://stackoverflow.com/a/22418949/8339821. The answer states that the solution can monitor any way a process in Windows can terminate. – user14063792468 Mar 31 '19 at 22:33
  • @ЯрославМашко - absolute unrelated to question "solution" – RbMm Mar 31 '19 at 22:45
  • @RbMm The question states: “what should I do to properly finish the work of my game?” Also the OP tells us that “...or the task is killed I cannot properly finish the work, deallocating all the resources...” The link above deals with this situation. – user14063792468 Mar 31 '19 at 22:53
  • 1
    @ЯрославМашко - absolute no. how is detect win32 process creation/termination in c++ related here ? really OP need send some notify from `CloseHandler` to `GameThread`. how send this - depend from how `GameThread` written. say in original example `GameThread` periodic check state of `m_bAtomActive` and `CloseHandler` simply change it state – RbMm Mar 31 '19 at 23:01
  • Equivalent to `atomic` functions could be the `InterlockedWhatever` functions (e.g. `InterlockedExchange`). [This answer](https://stackoverflow.com/questions/779996/reading-interlocked-variables) says that on Windows you use `volatile` to make a variable atomic (note: this doesn't necessarily work on other platforms than Windows). – user253751 Apr 01 '19 at 01:08
  • You shouldn't wait in Ctrl+C handler. There is no point in waiting and then doing nothing. If your main game thread periodically checks `m_bAtomActive` setting `m_bAtomActive` to `false` would be enough. If main thread is in blocking wait - unblock it with appropriate call (for `GetMessage` - `PostMessage`, for events `SetEvent` etc). – Daniel Sęk Apr 01 '19 at 06:17
  • Is this console application or GUI one? In the second case you should handle `WM_CLOSE` instead because you will never get `CloseHandler` called. – Daniel Sęk Apr 01 '19 at 06:26
  • Simply, you cannot guarantee to be able to 'manually' deallocate resources upon either internal or external process termination. You cannot safely stop an executing thread, (only the OS can do it safely, and only at process termination), and you cannot intercept 'end process' or 'kill -9', (else you could make unstoppable malware:). Design your system so that persistent resources, (e.g. temp files), are deleted at start-up instead of shutdown. – Martin James Apr 01 '19 at 07:25

0 Answers0