- Sep 19, 2000
- 10,286
- 147
- 106
So, I'm trying to make a custom built thread pool object. My problem is, it crashes, unpredictably. I think I've narrowed down where the code is faulting and can't figure out why it it incorrect.
Here's how it works, the thread pool spawns 4 threads and passes into those threads a pointer to the threadpool class. each of those spawned threads will request jobs from the thread pool, and report back to the thread pool when their jobs are finished. The problem is, every so often the data that comes back from the thread pool will flip around (really weird).
This is the code I believe is where the problem is located.
So what gives? Often it is jobNumb that will swap places with job, which of course causes a segfault when job is called as a function.
The threadpool class can be found
header
Source
Here's how it works, the thread pool spawns 4 threads and passes into those threads a pointer to the threadpool class. each of those spawned threads will request jobs from the thread pool, and report back to the thread pool when their jobs are finished. The problem is, every so often the data that comes back from the thread pool will flip around (really weird).
This is the code I believe is where the problem is located.
typedef void(*Job)(void*);
struct Task
{
Job job;
void* input;
int jobNumb;
};
DWORD WINAPI thread(ThreadPool* input )
{
printf("Created Thread\n");
void* thisThread = GetCurrentThread();
while(!input->isDead())
{
Task task;
input->getJob(task);
if (task.job != NULL)
{
task.job(task.input);
input->finishJob(task.jobNumb);
}
else
{
SuspendThread(thisThread);
}
}
return 0;
}
void ThreadPool::getJob(Task& input)
{
EnterCriticalSection(&cs);
{
if(jobs.size() == 0)
{
input.job = NULL;
input.input = NULL;
}
else
{
input.job = jobs.front();
input.input = data.front();
input.jobNumb = tasks.front();
jobs.pop();
data.pop();
tasks.pop();
}
}
LeaveCriticalSection(&cs);
}
So what gives? Often it is jobNumb that will swap places with job, which of course causes a segfault when job is called as a function.
The threadpool class can be found
header
Source
