So I've got a question about a possible technique or idea using threads, but don't know how what it's called or how to implement it. Say you have a small function that you can run in parallel. So you create a thread for it. It finishes its task, but instead of wanting it to destruct, you want to suspend it for an indefinite amount of time. Then when you need its help again (say to spilt up the time initializing a huge array or processing the elements in a huge array), you wake it up, it jumps to the beginning of a function which does something (say in this case helps initialize an array), and when it finishes it gets suspended again. So it's like this worker you can call to lend an extra set of hands whenever you need it to. This would improve efficiency because each time you need a little help you aren't constructing and destrucing a thread and all the initialization of each one, for just a small service. With all the setup, it wouldn't really improve much at all. Does anyone know what this is called or how to implement it (using the Boost library would be the best solution, but I'll take one using standard Windows functions if you got it)? The only way I've come up with is to put everything you'd want it to do in the same function, and just have the thread suspend itself after each little bit of code, and have the main thread resume it when necessary, but I know it's bad design and relies on a global boolean value... If you don't know what I mean, maybe the code below will help explain (but just think of it not all in the same function). I've been told it can be done (but not how) and that it might be called a worker thread, but search results have turned up nothing. Thanks for any help, even if it's only the name of the idea of doing something this way.
#include <windows.h>
DWORD WINAPI doThreadProcessing(void* param);
bool stop = false;
HANDLE thread;
const int arrLength = 100000;
int arr1[arrLength];
int main(int argc, char** argv)
{
int i = 0;
unsigned long int threadID;
thread = CreateThread(NULL, 0, doThreadProcessing, (void*) i, 0, &threadID);
//I know the thread has to have THREAD_SUSPEND_RESUME in a security attribute
int x;
for(x = 0; x < arrLength; x+=2)
{
arr1[x] = 0;
}
//do some stuff......
while(!stop)
{
Sleep(10);
}
stop = false;
ResumeThread(thread);
for(x = 0; x < arrLength; x+=2)
{
if(arr1[x] >= 5)
arr1[x] = 5;
}
CloseHandle(thread);
return 0;
}
DWORD WINAPI doThreadProcessing(void* param)
{
int y;
for(y = 1; y < arrLength; y+=2)
{
arr1[y] = 0;
}
stop = true;
SuspendThread(thread);
for(y = 0; y < arrLength; y+=2)
{
if(arr1[y] >= 5)
arr1[y] = 5;
}
SuspendThread(thread);
//...etc... for small tasks that wouldn't really benefit from
//contructing and destructing a new thread each time
return 0;
}