Below is a brief proof of concept I threw together from a larger project I'm working on. The project has a worker thread that is created using CreateThread
that monitors a directory for changes. I have certain cleanup code that needs to run like closing handles and freeing some memory.
The application does not run as a background process or service. It is run via the command line and runs until the user either closes the command window or presses Ctrl-C
.
Is there a better way to do this? I tried using atexit
but that apparently is not being called when the process is killed.
Note that I'm using C, not C++ and am not using MFC, AFX or any other API other than the Windows API. Based on comments, I guess another part of this is how do I properly terminate the application in a multithreaded environment? Is this ok, or should I call ExitProcess
within thread_func
?
UPDATE:
I've updated with Luke's comment below as to setting a flag to indicate that the application is terminating. I've also changed it to use atexit
to call the cleanup function instead of calling it directly.
#include <stdio.h>
#include <windows.h>
HANDLE thread_handle = INVALID_HANDLE_VALUE;
BOOL time_to_quit = FALSE;
DWORD WINAPI thread_func(LPVOID lpvd)
{
fwprintf(stdout, L"thread running...\n");
while (!time_to_quit)
{
/* do stuff */
Sleep(10);
}
return 0;
}
void cleanup()
{
/* clean up code here that needs to be run at exit */
fwprintf(stdout, L"cleaning up...\n");
CloseHandle(thread_handle);
}
BOOL WINAPI console_handler(DWORD event)
{
time_to_quit = TRUE;
return FALSE;
}
int wmain(int argc, wchar_t* argv[])
{
DWORD thread_id = 0;
thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, NULL, 0, &thread_id);
if (thread_handle != NULL)
{
atexit(cleanup);
fwprintf(stdout, L"thread created...\n");
SetConsoleCtrlHandler((PHANDLER_ROUTINE)console_handler, TRUE);
WaitForSingleObject(thread_handle, INFINITE);
}
return 0;
}