Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hello,
 
I was reading this article:
Multithreading Tutorial[^]
which has following code in it, which I compiled successfully.
#include <stdio.h>
#include <windows.h>
#include <process.h>     // needed for _beginthread()
 
void  silly( void * );   // function prototype
 
int main()
{
    // Our program's first thread starts in the main() function.
 
    printf( "Now in the main() function.\n" );
 
    // Let's now create our second thread and ask it to start
    // in the silly() function.
 

    _beginthread( silly, 0, (void*)12 );
    // _beginthread( silly, 0, (void*)13 ); if I uncomment this I don't see 13 on output :(
 
    // From here on there are two separate threads executing
    // our one program.
 
    // This main thread can call the silly() function if it wants to.
 
    silly( (void*)-5 );
    Sleep( 1000 );
}
 
void  silly( void *arg )
{
    printf( "The silly() function was passed %d\n", (INT_PTR)arg ) ;
}
 
But if I add just one line to it, like this one:
_beginthread( silly, 0, (void*)13 );
I don't see the output 13 on the screen, whereas I see 12 and -5.
Why is this the case??? Thank you.
Posted 16-Apr-13 1:45am
Comments
   
Hi executed your code ok. I can see the number 13. Cheers.
Richard MacCutchan at 16-Apr-13 7:25am
   
If you have comments on CodeProject articles then please use the forum at the end of the article.
Dan page at 16-Apr-13 7:40am
   
Yes but that way I may have to wait for the answer too long - and this question might not necessarily be specifically related to that article, that is why I posted it here.
Richard MacCutchan at 16-Apr-13 9:01am
   
OK, for the sake of completeness I have run this code and I see -5, 12 and 13. So are you sure of your results?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Should really be attached to the article but anyway. You don't see 13 and José does. What does this tell you?
 
I'll let you work this one out for yourself but I'll pose a few relevant questions.
 
How many consoles/terminals is this program attached to?
 
Was the console you're using around before multithreading?
 
Given a choice of getting
112-35
or
12
-5
as output from your program which would your choose?
 
Now I guess you understand the problem. The reall interesting part is what's the best way to solve it given you can't rewrite the Console program, or even if you could?
  Permalink  
Comments
Richard MacCutchan at 16-Apr-13 9:31am
   
The code works fine, and prints each message on a separate line on the same console; as one would expect.
Matthew Faithfull at 16-Apr-13 10:06am
   
Lucky you. I don't believe there is any guarantee that's what everyone will get that and on Linux I know there isno such guarantee. It all depends on how the Console serializes the printf output which given how low level the printf backend can be is often 'not ideal'.
Richard MacCutchan at 16-Apr-13 10:11am
   
You are absolutely correct. But it's a fair bet with something this simple, that each printf() will complete before it gets pre-empted. However, if I were writing a 'real' multi-threaded application I would be using proper care to synchronise everything that needed it.
Matthew Faithfull at 16-Apr-13 10:13am
   
Im not a betting man :-)
Dan page at 16-Apr-13 11:13am
   
Now I see output 13, I really don't know what was the problem last time. Maybe it was because I was running the program using Ctrl+F5? although I recompiled the source I remember. anyway it works now.
Matthew Faithfull at 16-Apr-13 13:02pm
   
Be sure that its repeatable. Thats the point, the results can be inconsistent.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100