CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2011
    Posts
    5

    Question Multithreading Strange behaviour

    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.

    Code:
    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.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Multithreading Strange behaviour

    Looks like what? Like this?
    Code:
    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
    2
    And what did you expect?
    Best regards,
    Igor

  3. #3
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Multithreading Strange behaviour

    >> (LPTHREAD_START_ROUTINE)ThreadFunc
    Never cast a parameter that expects a function pointer. If it doesn't compile then it's wrong.

    gg

  4. #4
    Join Date
    Mar 2011
    Posts
    5

    Re: Multithreading Strange behaviour

    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...

  5. #5
    Join Date
    Mar 2011
    Posts
    5

    Question Re: Multithreading Strange behaviour

    >>>>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.

    Code:
    #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;
    }

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Multithreading Strange behaviour

    >> 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

  7. #7
    Join Date
    Mar 2011
    Posts
    5

    Re: Multithreading Strange behaviour

    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?

  8. #8
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Multithreading Strange behaviour

    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

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Multithreading Strange behaviour

    Quote Originally Posted by bladefistx2 View Post
    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).

  10. #10
    Join Date
    Mar 2011
    Posts
    5

    Re: Multithreading Strange behaviour

    Ok... Great - thanks for the tips Arjay and Codeplug.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured