Click to See Complete Forum and Search --> : Multithreading Strange behaviour
bladefistx2
March 30th, 2011, 06:42 PM
Hi there...
I'm new to this forum so please correct me if this is not the right place to post this...
I have created two threads in the following code, and was expecting the threads to behave quite differently than they actually did...
can someone please explain why this happens? I'd be more than grateful.
DWORD ThreadFunc(LPVOID p)
{
int n;
n = *(int *)p;
for (int i=0; i<5; i++)
{
printf("%d\n", n);
sleep(10);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n=2;
printf("Two Threads\n");
HANDLE hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadFunc, (LPVOID)&n, NULL, NULL);
// CloseHandle(hThread);
for (int i = 0; i<5; i++)
{
printf("----1\n");
sleep(10);
}
WaitForSingleObject(hThread,INFINITE);
char ch = getchar();
return 0;
}
Why does the program output look like this?
Thanks in advance.
Igor Vartanov
March 31st, 2011, 03:01 AM
Looks like what? Like this?
E:\Temp\596>596
Two Threads
----1
2
----1
2
----1
2
----1
2
----1
2
E:\Temp\596>596
Two Threads
----1
2
2
----1
2
----1
----1
2
2
----1
E:\Temp\596>596
Two Threads
----1
2
2
----1
----1
2
2
----1
----1
2And what did you expect?
Codeplug
March 31st, 2011, 08:30 AM
>> (LPTHREAD_START_ROUTINE)ThreadFunc
Never cast a parameter that expects a function pointer. If it doesn't compile then it's wrong.
gg
bladefistx2
April 4th, 2011, 05:12 AM
I saw your three examples of running it.
what I was expecting was actually the first example.
I still don't quite understand why I get a different output every time,
and more importantly why the threads are not synced,
i mean why aren't they intermittently showing up?
Thanks...
bladefistx2
April 4th, 2011, 05:39 AM
>>>>Correction<<<<
disregard my previous comment...
I just realized why this happens...
My next question, is a little more complex.
How come the number of times the threads print to screen is different
when I test this with different operating systems?
btw, i fixed a few places there should be S instead of s.
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
DWORD ThreadFunc(LPVOID p)
{
int n;
n = *(int *)p;
for (int i=0; i<5; i++)
{
printf("%d\n", n);
Sleep(10);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n=2;
printf("Two Threads\n");
HANDLE hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadFunc, (LPVOID)&n, NULL, NULL);
CloseHandle(hThread);
for (int i = 0; i<5; i++)
{
printf("----1\n");
Sleep(10);
}
WaitForSingleObject(hThread,INFINITE);
char ch = getchar();
return 0;
}
Codeplug
April 4th, 2011, 09:44 PM
>> btw, i fixed a few places...
But you didn't fix the error from post #3.
>> ... is different when I test this with different operating systems?
If you have code for some non-MS OS, then you will need to post that code as well.
gg
bladefistx2
April 5th, 2011, 08:40 AM
Ok, i'll fix the casting.
by different OS i mean windows XP, 2000, and 7... they all give different amount of output lines.
I think this has something to do with the Windows Scheduler.
Does some1 know anything about this?
Codeplug
April 5th, 2011, 09:19 AM
With correct code, you'll always see 10 lines.
If you remove the getchar() call, then you'll need a "fflush(stdout)" call instead.
gg
Arjay
April 5th, 2011, 11:54 AM
Ok, i'll fix the casting.
by different OS i mean windows XP, 2000, and 7... they all give different amount of output lines.
I think this has something to do with the Windows Scheduler.
Does some1 know anything about this?Windows is a pre-emptive multitasking operating system and its scheduler will schedule threads as it sees fit.
As such, you should never assume that threads will get executed in any sort of order.
As a programmer new to coding m/t applicationd, please read the previous sentence again because it is that important.
If you do need threads to be run in a particular order, then you need to control the order of execution via synchronization objects (e.g. critical sections, mutexes, events, and so on).
bladefistx2
April 8th, 2011, 07:16 AM
Ok... Great - thanks for the tips Arjay and Codeplug.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.