Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I was reading this article:
Multithreading Tutorial[^]
which has following code in it, which I compiled successfully.
XML
#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:
C#
_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
Comments
José Amílcar Casimiro 16-Apr-13 7:02am    
Hi executed your code ok. I can see the number 13. Cheers.
Richard MacCutchan 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 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 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

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?
 
Share this answer
 
Comments
Richard MacCutchan 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 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 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 16-Apr-13 10:13am    
Im not a betting man :-)
Dan page 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900