难道是线程安全调用printf在同时运行的线程? [英] Is it thread safe to call printf in threads that run simultaneously?

查看:454
本文介绍了难道是线程安全调用printf在同时运行的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  标准输出线程安全的C对Linux的?


说线程1和线程是​​类似的,在他们的工作他们都的printf的结束。它是线程安全的还是他们必须以某种方式锁定的printf?

难道到stdout有关?如果人做每一printf的后fflush(标准输出)?它是否在改变什么?


解决方案

  

在POSIX.1和C语言函数
  这对字符流操作
  (重新通过指针psented为对象$ P $
  类型文件)被POSIX.1c要求
  以这样的方式来实现该
  重入实现(见ISO / IEC
  9945:1-1996,第8.2节)


线程安全性和POSIX.1

请注意:有些功能是可重入或非重入的,这取决于他们的论点

Possible Duplicate:
stdout thread-safe in C on Linux?

Say thread1 and thread2 are similar and at the end of their jobs they both printf. Is it thread safe or do they have to lock printf somehow?

Is it related to stdout? What if one does fflush(stdout) after each printf? Does it change anything?

解决方案

The POSIX.1 and C-language functions that operate on character streams (represented by pointers to objects of type FILE) are required by POSIX.1c to be implemented in such a way that reentrancy is achieved (see ISO/IEC 9945:1-1996, §8.2).

refer to Thread-safety and POSIX.1

Note: Some functions can be reentrant or non-reentrant, depending on their arguments.

这篇关于难道是线程安全调用printf在同时运行的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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