-4

I have successfully created a single thread using CreateThread().

Now I want to create 'n' number of threads but each with a different ThreadProc().

I have tried the following code but using it, 'n' number of threads are created all performing the same task (since Threadproc() function af all threads is same.)

    //Start the threads
for (int i=1; i<= max_number; i++) 
{
CreateThread( NULL, //Choose default security
              0, //Default stack size
              (LPTHREAD_START_ROUTINE)&ThreadProc,
              //Routine to execute. I want this routine to be different each time as I want each  thread to perform a different functionality.
              (LPVOID) &i, //Thread parameter
              0, //Immediately run the thread
              &dwThreadId //Thread Id
            ) 
}

Is there any way I can create 'n' number of Threads each with a different Thread procedure?

Ayse
  • 2,676
  • 10
  • 36
  • 61
  • 1
    Er... pass each one a pointer to a different thread procedure! I'm afraid I don't understand why doing this is difficult, or what you're trying to do. Judging by the downvotes, neither does anyone else. Can you explain what you're trying to do and why? – David May 02 '13 at 07:33
  • 1
    @DavidM : "pass each one a pointer to a different thread procedure!" I think that is what I was looking for :) Thank you so much :) – Ayse May 02 '13 at 07:45
  • 1
    Hah! No worries, glad it was so simple :) – David May 03 '13 at 10:13

1 Answers1

2

Try this:

DWORD WINAPI ThreadProc1(  LPVOID lpParameter)
{
  ...
  return 0 ;
}

DWORD WINAPI ThreadProc2(  LPVOID lpParameter)
{
  ...
  return 0 ;
}

...

typedef DWORD (WINAPI * THREADPROCFN)(LPVOID lpParameter);

THREADPROCFN fntable[4] = {ThreadProc1, ThreadProc2, ...} ;

//Start the threads
for (int i = 0; i < max_number; i++) 
{
  DWORD ThreadId ;

  CreateThread( NULL,
                0,
                (LPTHREAD_START_ROUTINE)fntable[i],
                (LPVOID) i,
                0,
                &ThreadId
              ) ;
}

This will start max_number threads with different thread procedures (TreadProc1, ThreadProc2, etc.) as defined in fntable.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Is it possible to create two threds with same thread Procedure call and different parameters passed to thread procedure call for each Thread. e.g. for thread#1, Thread Procedure is "threadProc()" and parameter is "thread1". for thread#2, Thread Procedure is "threadProc()" and parameter is "thread2" – Ayse May 02 '13 at 08:05
  • 1
    That's exactly what you did in your example, except that you must write "(LPVOID)i" instead of "(LPVOID)&i". – Jabberwocky May 02 '13 at 08:19
  • I had been trying the above mentioned code. It is compiled successfully but when the code is exxecuted, I get the error that solution.exe has stopped working. Could you suggest me a solution Please? – Ayse May 07 '13 at 07:19
  • Unhandled exception at 0x003bf9a8 in Creating40Threads.exe: 0xC0000005: Access violation. – Ayse May 07 '13 at 07:22
  • Where does it crash ? Your debugger should tell you. Show your code. – Jabberwocky May 08 '13 at 07:33
  • Problem Solved by replacing `(LPTHREAD_START_ROUTINE)&fntable[i]` with `(LPTHREAD_START_ROUTINE)fntable[i]`. I even myself don't how did this happen – Ayse May 08 '13 at 07:54