为什么使用`printf()`不会引起溢出?

发布于 2025-01-23 08:43:21 字数 1244 浏览 0 评论 0原文

我正在对printf()进行一些实验,我的代码如下:

#include <iostream>       // std::cout
#include <thread>         // std::thread
 
void foo() 
{
    while(1) printf("a");
}

void bar()
{
  while(1) printf("b");
}

int main() 
{
  std::thread first (foo);     // spawn new thread that calls foo()
  std::thread second (bar);  // spawn new thread that calls bar(0)
  first.join();                // pauses until first finishes
  second.join();               // pauses until second finishes
  return 0;
}

大量AB会输出。但是我在这里有一个问题:据我所知,printf有一个缓冲区,那么为什么buffer [pos ++]不会导致溢出?我想使用了一个圆缓冲区,但我不确定。

因此,当缓冲区不完整时,两个线程会同时写入缓冲区,我也编写另一个版本

#include <iostream>       // std::cout
#include <thread>         // std::thread
 
void foo() 
{
    while(1) printf("aaaaaa");
}

void bar()
{
  while(1) printf("bbbbbb");
}

int main() 
{
  std::thread first (foo);     // spawn new thread that calls foo()
  std::thread second (bar);  // spawn new thread that calls bar(0)
  first.join();                // pauses until first finishes
  second.join();               // pauses until second finishes
  return 0;
}

,但是输出不会像ababab一样,我想知道Printf如何同时处理写作?

I'm doing some experiments on printf(), my code is shown below:

#include <iostream>       // std::cout
#include <thread>         // std::thread
 
void foo() 
{
    while(1) printf("a");
}

void bar()
{
  while(1) printf("b");
}

int main() 
{
  std::thread first (foo);     // spawn new thread that calls foo()
  std::thread second (bar);  // spawn new thread that calls bar(0)
  first.join();                // pauses until first finishes
  second.join();               // pauses until second finishes
  return 0;
}

A lot of ab will output. But I have a question here: as far as I know, printf has a buffer, so why buffer[pos++] will cause no overflow? I guess a circle buffer is used, but I'm not sure.

So when the buffer is not full, two threads will write to the buffer concurrently, I also write another version

#include <iostream>       // std::cout
#include <thread>         // std::thread
 
void foo() 
{
    while(1) printf("aaaaaa");
}

void bar()
{
  while(1) printf("bbbbbb");
}

int main() 
{
  std::thread first (foo);     // spawn new thread that calls foo()
  std::thread second (bar);  // spawn new thread that calls bar(0)
  first.join();                // pauses until first finishes
  second.join();               // pauses until second finishes
  return 0;
}

But the output will not something like ababab, I wonder how printf deal with writing concurrently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2025-01-30 08:43:21

为什么缓冲区[POS ++]不会导致溢出?

printf的实现要注意,如果使用缓冲区,它从未在缓冲区之外写下。

我想使用了一个圆缓冲区

实现可能会选择这样做。

我想知道printf如何同时处理写作?

C标准说:

每个流都有一个关联的锁定,用于在多个执行访问流并限制多个线程执行的流操作的相互作用时,用于防止数据种族。一次只有一个线程可以容纳此锁。锁定是重入的:一个线程可能在给定时间将锁多次固定。

so why buffer[pos++] will cause no overflow?

The implementation of printf takes care that it never writes outside of its buffer in case that it uses a buffer.

I guess a circle buffer is used

The implementation may choose to do so.

I wonder how printf deal with writing concurrently?

The C standard says:

Each stream has an associated lock that is used to prevent data races when multiple threads of execution access a stream, and to restrict the interleaving of stream operations performed by multiple threads. Only one thread may hold this lock at a time. The lock is reentrant: a single thread may hold the lock multiple times at a given time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文