![]() |
| | #1 |
| Registered User Join Date: Jun 2008
Posts: 37
| Need help with multiple threads 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. Code: typedef void(*Job)(void*);
struct Task
{
Job job;
void* input;
int jobNumb;
};
Code: 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;
}
Code: 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);
}
*Attached a sample of the broken code.* Last edited by Cogman; 06-30-2009 at 02:20 PM. |
| Cogman is offline | |
| | #2 |
| Registered User Join Date: Jun 2008
Posts: 37
| Well, for anyone interested, I found the problem. My crossprod thread function was called using the stdcall method and not with the cdecl method that it should have been called. So my stack was getting smashed. Let this be a lesson for all you function pointer people, always use the correct calling method . |
| Cogman is offline | |
| | #3 |
| Super Moderator Join Date: Aug 2001
Posts: 7,470
| Incorrect calling convention can also cause stack overflows since no one pops off of the stack b/c the compiler thinks the caller/callee is going to do it.
__________________ If you aim at everything you will hit something but you won't know what it is. |
| Bubba is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multiple Threads, One listener. | PING | Networking/Device Communication | 3 | 03-27-2009 12:19 PM |
| Multiple Threads | NuNn | C Programming | 3 | 03-14-2009 11:29 PM |
| Question about Multiple threads and ring buffer. | qingxing2005 | C Programming | 2 | 01-15-2007 12:30 AM |
| Race condition: getting multiple threads to cooperate | FlyingDutchMan | C++ Programming | 10 | 03-31-2005 05:53 AM |
| Modeless Dialogs in Multiple threads | MrGrieves | Windows Programming | 0 | 06-22-2004 01:33 PM |