多线程问题C ++ [英] Multithreading question C++

查看:58
本文介绍了多线程问题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 );



我没看到屏幕上的输出13,而我看到12和-5。

为什么会这样呢???谢谢。


I don''t see the output 13 on the screen, whereas I see 12 and -5.
Why is this the case??? Thank you.

推荐答案

应该真的附在文章上但无论如何。你看不到13和José的确如此。这告诉你什么?



我会让你自己解决这个问题我会提出一些相关问题。



这个程序附加了多少个游戏机/终端?



你是在多线程之前使用的控制台吗?



可以选择获得

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?


这篇关于多线程问题C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆