c - 如何在多线程中使用 printf()

标签 c linux multithreading printf multiprocess

我正在实现一个使用不同内核的多线程程序,许多线程同时执行。每个线程都调用 printf(),结果不可读。

如何使 printf() 成为原子的,以便一个线程中的 printf() 调用不会与 printf()调用另一个?

最佳答案

POSIX 规范

POSIX 规范包括以下函数:

Versions of the functions getc(), getchar(), putc(), and putchar() respectively named getc_unlocked(), getchar_unlocked(), putc_unlocked(), and putchar_unlocked() shall be provided which are functionally equivalent to the original versions, with the exception that they are not required to be implemented in a fully thread-safe manner. They shall be thread-safe when used within a scope protected by flockfile() (or ftrylockfile()) and funlockfile(). These functions can safely be used in a multi-threaded program if and only if they are called while the invoking thread owns the (FILE *) object, as is the case after a successful call to the flockfile() or ftrylockfile() functions.

这些函数的规范提到:

flockfile() 等规范包括一揽子要求:

All functions that reference (FILE *) objects, except those with names ending in _unlocked, shall behave as if they use flockfile() and funlockfile() internally to obtain ownership of these (FILE *) objects.

这将取代本答案之前版本中的建议代码。 POSIX 标准还规定:

The [*lockfile()] functions shall behave as if there is a lock count associated with each (FILE *) object. This count is implicitly initialized to zero when the (FILE *) object is created. The (FILE *) object is unlocked when the count is zero. When the count is positive, a single thread owns the (FILE *) object. When the flockfile() function is called, if the count is zero or if the count is positive and the caller owns the (FILE *) object, the count shall be incremented. Otherwise, the calling thread shall be suspended, waiting for the count to return to zero. Each call to funlockfile() shall decrement the count. This allows matching calls to flockfile() (or successful calls to ftrylockfile()) and funlockfile() to be nested.

还有字符输入输出函数的规范:

这里记录了格式化输出函数:

printf() 规范中的一个关键条款是:

Characters generated by fprintf() and printf() are printed as if fputc() had been called.

注意“好像”的使用。但是,每个 printf() 函数都需要应用锁,以便在多线程应用程序中控制对流的访问。一次只有一个线程可以使用给定的文件流。如果操作是对 fputc() 的用户级调用,则其他线程可以散布输出。如果操作是用户级调用,例如 printf(),那么整个调用和对文件流的所有访问都会受到有效保护,以便在调用 之前只有一个线程使用它printf() 返回。

在系统接口(interface)部分:POSIX 的一般信息部分中关于 Threads 的主题,它说:

2.9.1 Thread-Safety

All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the following functions1 need not be thread-safe.

…a list of functions that need not be thread-safe…

… The getc_unlocked(), getchar_unlocked(), putc_unlocked(), and putchar_unlocked() functions need not be thread-safe unless the invoking thread owns the (FILE *) object accessed by the call, as is the case after a successful call to the flockfile() or ftrylockfile() functions.

Implementations shall provide internal synchronization as necessary in order to satisfy this requirement.

豁免函数列表不包含fputcputcputchar(或printf()等等)。

解释

2017-07-26 重写。

  1. 流上的字符级输出是线程安全的,除非在未先锁定文件的情况下使用“解锁”函数。
  2. 高级函数,例如 printf() 从概念上讲,在开始时调用 flockfile(),在结束时调用 funlockfile(),这意味着 POSIX 定义的流输出函数在每次调用时也是线程安全的。
  3. 如果您希望对单个线程的文件流操作进行分组,您可以通过显式调用 flockfile() 和相关流(不干扰系统对 *lockfile() 函数的使用。

这意味着您无需为自己创建互斥锁或等效机制;该实现提供了允许您在多线程应用程序中控制对 printf() 等的访问的功能。

…此答案的先前版本中的代码已删除,因为不再相关…

关于c - 如何在多线程中使用 printf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23586682/

相关文章:

c - 无法在动态链接库中找到过程入口点axiom_attribute_create

C 使用指针调用函数时出现问题

c - 如何在循环中从服务器向客户端接收和发送数据

c - 如果用户输入错误,则使一段代码循环回到代码中的某个点

linux - 如何将被盗的数据包返回 Netfilter

linux - .so : need to find out which function(s) are executed on loading

ISO C 中的跨平台并发库

mysql - 我可以从 Vim 视觉选择执行 MySQL 查询并在新缓冲区中输出

c++ - 在创建线程时无法理解此错误

java - 多线程程序的处理器数量