0

Summarised question: What is the best method to monitor thread variables in a function external to thread?

Original Question: I am looking to get a better understanding of the best practices when using Windows threading. I understand there are more modern methods for threading but I am restricted to using CreateThread. The current setup contains a WaitForSingleObject function inside the function called by CreateThread that updates some pointers. I need to access these pointers externally to the thread. What is the best practices to access data within the WaitForSingleObject function? Passing parameters into the function is straightforward using the LPVOID params argument, but there doesn't seem to be an output option other than the thread ID and using globals.

HANDLE WINAPI CreateThread( __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, __in SIZE_T dwStackSize, __in LPTHREAD_START_ROUTINE lpStartAddress, __in_opt LPVOID lpParameter, __in DWORD dwCreationFlags, __out_opt LPDWORD lpThreadId ); 
CiunasBothar
  • 81
  • 2
  • 8
  • 2
    Please show your code. – selbie Oct 03 '19 at 06:43
  • @selbie I can't in this case as it's for work. I can put together some mock code if required but I was just looking for general advice – CiunasBothar Oct 03 '19 at 06:49
  • I can certainly help, but your question is very rambling. And it's easier for me to give answers in terms a coding example that you have already provided. – selbie Oct 03 '19 at 06:52
  • 2
    You could pass a pointer to a structure or class object as the `LPVOID` parameter, and use the data members of that struct/class in place of globals. – Adrian Mole Oct 03 '19 at 08:09
  • Not sure what you mean by `access data within the WaitForSingleObject function`. Do you mean how to access data within the routine you passed as LPTHREAD_START_ROUTINE? Also it's not clear how what do you expect as output. What are you trying to achieve? Is the worker thread performing some short task asynchroniously? Is it constantly doing work and you want to observe changes? The best practices for how to share data would change accordingly. – Sirotnikov Oct 03 '19 at 11:41

1 Answers1

0

Thanks to Adrian for the advice. Passing a pointer to a structure worked. A simplified version of the working code is below.

#include <Windows.h>
#include <iostream>

struct MyStruct {
    int var;
};

DWORD WINAPI mythread(LPVOID lpParameter)
{
    MyStruct *PSTRUCT = (MyStruct*)lpParameter;
    PSTRUCT->var = 22;
    std::cout << "Thread function value: " << PSTRUCT->var << std::endl;
    return 0;
}

int main(int argc, char* argv[])
{
    MyStruct *PSTRUCT;
    HANDLE myhandle;
    DWORD mythreadid;

    PSTRUCT = reinterpret_cast<MyStruct*>(HeapAlloc(GetProcessHeap(), 0, sizeof(MyStruct)));
    PSTRUCT->var = 0;
    myhandle = CreateThread(0, 0, mythread, (LPVOID)PSTRUCT, 0, &mythreadid);
    WaitForSingleObject(myhandle, INFINITE);
    std::cout << "Main function value: " << PSTRUCT->var << std::endl;
    getchar();
    return 0;
}
CiunasBothar
  • 81
  • 2
  • 8