c - sprintf 线程安全吗?

标签 c linux multithreading

sprintf 线程安全吗?

//Global log buffer 
char logBuffer[20];

logStatus (char * status, int length)
{
  snprintf(logBuffer, 19, status);
  printf ("%s\n", logBuffer);
}

该函数的线程安全完全依赖于snprintf/sprintf的线程安全。

更新: 谢谢你的回答。 我不介意,如果实际内容 gts 搞砸了。但是想确认在这种情况下,当多个线程试图写入 logBu​​ffer 时,sprintf 不会导致超过 20 个字节的内存损坏/缓冲区溢出?

最佳答案

多线程使用snprintf()没有问题。但是这里您正在写入一个共享字符串缓冲区,我假设它是跨线程共享的。

因此您对此函数的使用不是线程安全的。

关于c - sprintf 线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13386352/

相关文章:

c - 如何在 C 中进行引用计数 - 参数传递

c - 欧拉项目'8 : solution works for example but can't find answer

c++ - 在 pthread 中阻塞当前线程

linux - 用于 RHEL 的 gdb-multiarch

c++ - 在多线程 C++11 程序中未处理异常时会发生什么?

Java多线程: threads shall access list

返回数组的 C 函数

linux - gcc make 安装错误

linux - 使用 shell 脚本从出现在网页顶部的弹出窗口中提取 urls 链接?

c# - 无法终止 Silverlight 中的工作线程