Thread: Need help with multiple threads

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    62

    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.*
    Last edited by Cogman; 06-30-2009 at 02:20 PM.

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    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 .

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Threads, One listener.
    By PING in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-27-2009, 12:19 PM
  2. Multiple Threads
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 03-14-2009, 11:29 PM
  3. Question about Multiple threads and ring buffer.
    By qingxing2005 in forum C Programming
    Replies: 2
    Last Post: 01-15-2007, 12:30 AM
  4. Race condition: getting multiple threads to cooperate
    By FlyingDutchMan in forum C++ Programming
    Replies: 10
    Last Post: 03-31-2005, 05:53 AM
  5. Modeless Dialogs in Multiple threads
    By MrGrieves in forum Windows Programming
    Replies: 0
    Last Post: 06-22-2004, 01:33 PM