c - 在同时运行的线程中调用 printf 是否线程安全?

标签 c multithreading thread-safety pthreads

<分区>

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

假设 thread1 和 thread2 相似,并且在它们的作业结束时它们都 printf。它是线程安全的还是他们必须以某种方式锁定 printf?

和stdout有关系吗?如果在每次 printf 之后执行 fflush(stdout) 会怎样?它会改变什么吗?

最佳答案

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).

引用Thread-safety and POSIX.1

注意:某些函数可以是可重入的或不可重入的,具体取决于它们的参数。

关于c - 在同时运行的线程中调用 printf 是否线程安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4353651/

相关文章:

c# - 跨线程操作无效,即使使用 InvokeRequired

c - malloc 成功还是失败?

c - 带 EOF 的额外循环

c - C中#include的尴尬问题

c - epoll(EPOLLET)取决于读取字节数?

linux - 在 Vala 中是否有某种等同于 .NET 的 BackgroundWorker 的东西?

c# - 等待事件引发线程取消

c++ - 使用 native Windows API 的 win32 线程安全队列实现

c++ - std::lock_guard 导致未定义的行为

python threading.Timer : do i need to protect the variable accessed inside the callback?