C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-30-2009, 01:29 PM   #1
Registered User
 
Join Date: Jun 2008
Posts: 37
Need help with multiple threads

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.

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);
}
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.

*Attached a sample of the broken code.*
Attached Files
File Type: cpp ThreadPool.cpp (3.0 KB, 21 views)
File Type: h ThreadPool.h (819 Bytes, 21 views)
File Type: cpp main.cpp (4.9 KB, 13 views)

Last edited by Cogman; 06-30-2009 at 02:20 PM.
Cogman is offline   Reply With Quote
Old 07-05-2009, 09:32 AM   #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   Reply With Quote
Old 07-05-2009, 09:40 AM   #3
Super Moderator
 
Bubba's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:20 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22