0

I have created simple application for Windows CE 5.0 as a exercise for some multithreaded work. In WinMain I am calling twice function for creating threads:

 myThread = CreateThread(NULL, 0, myThreadFunc(), NULL, 0, NULL);
 myThread2 = CreateThread(NULL, 0, myThreadFunc2(), NULL, 0, NULL);

And functions executed in threads looks like this:

LPTHREAD_START_ROUTINE myThreadFunc()
{
    Sleep(3000);
    MessageBox(NULL, _T("thread 1"), _T("thread 1"), MB_OK);
    return 0;
}

LPTHREAD_START_ROUTINE myThreadFunc2()
{
    Sleep(2000);
    MessageBox(NULL, _T("thread 2"), _T("thread 2"), MB_OK);
    return 0;
}

I was expecting that program shows dialog box from thread 2 after 2 secs and after next 1 sec show the dialog box from thread 1. But actually when i am running this program it shows at first dialog box from thread 1 after 2 secs and then dialog box from thread 2 after next 3 secs. It seems like this treads are running sequentially and not simultaneously as I expected. Can anybody explain me this behavior, please?

I am using Windows CE 5.0, Windows eMbedded Visual C++ and Emulator for Windows CE STANDARDSDK_500.

Thank you for your help in advance.

Grizzly
  • 1
  • 1
  • 3
  • That sounds a little off, but remember your main thread spawning the other threads is also running. Perhaps try having both threads wait on a starting event you set by the main thread and see if the results are more predictable. – Damon8or Dec 13 '11 at 21:34
  • If you dump it to the debug output or console (i.e. not sending it through the GWES windowing system which really, really likes all UI access to be done on the pump thread) what do you see? – ctacke Dec 13 '11 at 21:57

1 Answers1

0

Edit: Right answer is in comments.

Thank you for your effort. I just realized i have incorrect calling of function createThread(). Instead of:

myThread = CreateThread(NULL, 0, myThreadFunc(), NULL, 0, NULL);

I have to type:

myThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&myThreadFunc, NULL, 0, NULL);
Grizzly
  • 1
  • 1
  • 3
  • 1
    Ouch, no. You should never have to cast the function pointer to get CreateThread() to accept it. Instead, fix your function declaration which is currently a function taking no parameters and returning an LPTHREAD_START_ROUTINE. Check out the definition of that type and the MSDN, but if it doesn't compile without casting, the code is wrong! – Ulrich Eckhardt Jan 15 '13 at 20:00