奇怪的输出,当我使用的pthread和printf [英] weird output when I use pthread and printf

查看:192
本文介绍了奇怪的输出,当我使用的pthread和printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是英语为母语,所以有一些grammatic错误或缺乏前pression的。
我对此感到抱歉,但请容忍的。

I'm not native in English, so there are some grammatic error or lack of expression. I'm sorry for that, but please be tolerate.

我写的pthread使用的程序。

I write a program using pthread.

环境:Windows 7,CYGWIN_NT-i686的6.1 Cygwin的,海湾合作委员会(GCC)4.5.3

Environment:windows 7 , CYGWIN_NT-6.1 i686 Cygwin , gcc (GCC) 4.5.3

源$ C ​​$ C

The source code

#include<stdio.h>
#include<pthread.h>

void *th_func(void *p)
{
    int iLoop = 0;

    for(iLoop = 0;iLoop<100;iLoop++)
    {
        printf("Thread Thread Thread Thread\n");
    }

    return;
}

int main()
{
    int iLoop = 0;
    pthread_t QueThread;

    printf("Main : Start Main\n");

    printf("Main : Start Create Thread\n");
    pthread_create(&QueThread,NULL,th_func,NULL);
    printf("Main : End Create Thread\n");

    for(iLoop = 0;iLoop<100;iLoop++)
    {
        printf("Main Main Main Main\n");
    }

    pthread_join(QueThread,NULL);

    printf("Main : End Main\n");

    printf("---------------\n");

    return 0;
}

当我编译源$ C ​​$ C,没有任何警告或错误,但它的输出是怪异。

When I compile the source code, there are no warnings or errors,but it's output is weird.

它的一部分输出

Main : Start Main
Main : Start Create Thread
Thread Thread Thread ThreThread Thread Thread Thread
Main Main Main Main
Thread Thread Thread Thread
Main Main Main Main

我想知道这种现象的原因。

I want to know the cause of such phenomenon.

在此输出。主营:结束创建线程是不是在结束第3行打印completely.And,换行符\\ n发新话题发新话题\\ n消失

In this output,"Main : End Create Thread" is not printed completely.And at line 3,a newline"\n" at the end of "Thread Thread Thread Thread\n"disappear.

大家是输出这样吗?
它不会每次发生,但有时会发生。

Is everyone output like this? It does not occur everytime,but occurs sometime.

如果我使用互斥体安全地调用printf,怪异的输出似乎停止了。

If I use mutex to call printf safely,the weird output seem to be stopped.

Posix的说printf的是线程安全的,并根据Cygwin.com下,cygwin提供POSIX风格的API。然而,有意想不到的输出。

Posix says printf is thread-safe,and according to Cygwin.com,cygwin provides posix-style API. However, there is unexpected output.

时的printf真正线程安全的?
有谁知道呢?

Is printf really thread-safe? Does anyone know anything about it?

11/3我为我的延迟反应抱歉。

11/3 I'm sorry for my delayed response.

我执行同一程序100倍的Linux(Ubuntu的),并没有出现这个输出。

I executed the same program 100 times in Linux(Ubuntu), and this output did not occur..

11/8每个人,谢谢你回答我的问题。我将这个问题显得模糊不清关闭这个问题为主题。之后我搜索到了这个问题,这refferencing Q&放大器;一个,我可以再问问题。如果你不介意的话,请给我一只手。 :)

11/8 Everyone,Thank you for answer my question. I am going to close this question for the theme of this question becomes obscure. After I search into this problem with refferencing this q&a,I may ask question again. If you don't mind, please give me a hand. :)

我似乎没有有能力关闭的问题!
另外,我不明白为什么在输出一些词消失的原因。

I seem no to have ability to close question! In addition, I have not understood the reason why some words on the output disappear.

推荐答案

这看起来可能是Cygwin的一个bug,或者也可以是配置错误。这里有几个答案表明,线程安全的只承诺该功能不会造成伤害的方案,并且线程安全并不一定意味着一个功能是原子。但是,据我所知,POSIX没有正式定义线程安全(如果任何人有一个指针指向这样的定义,请张贴在一个评论)。

This looks like it may be a bug in Cygwin, or maybe something is misconfigured. Several answer here indicate that 'thread safe' only promises that the function won't cause harm to the program, and that thread safety doesn't necessarily mean that a function is 'atomic'. But, as far as I know, POSIX doesn't formally define 'thread safe' (if anyone has a pointer to such a definition, please post it in a comment).

然而,不仅POSIX指定的printf()是线程安全的,的 POSIX还规定了

However, not only does POSIX specify that printf() is thread safe, POSIX also specifies that:

这是引用(FILE *)的对象应表现为,如果他们使用flockfile()和funlockfile()在内部获得这些(FILE *)对象的所有权的所有功能。

All functions that reference ( FILE *) objects shall behave as if they use flockfile() and funlockfile() internally to obtain ownership of these ( FILE *) objects.

由于的printf()隐式引用标准输出 FILE * 对象,所有的的printf()调用应该是原子相对于彼此(以及使用其它任何功能标准输出)。

Since printf() implicitly references the stdout FILE* object, all printf() calls should be atomic with respect to each other (and any other function that uses stdout).

请注意,这可能不会对其他系统的事实,但以我的经验它确实如此众多的多线程系统。

Note that this might not be true on other systems, but in my experience it does hold true for many multi threaded systems.

这篇关于奇怪的输出,当我使用的pthread和printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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