I was wondering how to implement threading in a production setting? I've seen and tried various examples, but they all show basically the same thing - startup in main, run their function, clean up, and then the program exits.
I don't know the proper terminology, but I was looking for two maybe three functions to run simultaneously with a loop in main. I tried a small test program and was wondering if it's setup correctly.
A structure is used as the argument for each function.
Expand|Select|Wrap|Line Numbers
- int running = 1; // global variable
- DWORD WINAPI function_1(LPVOID);
- DWORD WINAPI function_2(LPVOID);
- main() {
- HANDLE hndThreads[2];
- DWORD threadIDs[2];
- // other initialization
- hndThreads[0] = CreateThread(...);
- hndThreads[1] = CreateThread(...);
- if(handles are valid) {
- while(running == 1) {
- // user interaction; user
- // input sets running = 0 to quit
- }
- WaitForMultipleObjects(2, hndThreads, TRUE, INFINITE);
- // close handles
- }
- // clean up main
- }
- DWORD WINAPI function_1(LPVOID lpParam) {
- // sdata is typedef struct
- sdata test = *((sdata *)lpParam);
- do {
- // manipulate test
- } while(running == 0);
- return 0;
- }
Thank you