sprintf的是线程安全的? [英] is sprintf thread safe?

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

问题描述

的sprintf 线程安全吗?

//Global log buffer 
char logBuffer[20];

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

这个函数的线程安全性完全取决于的snprintf / 的sprintf 的线程安全。

更新:
感谢乌拉圭回合的答案。
我不介意,如果实际内容GTS搞砸了。但要确认的sprintf不会造成内存损坏/缓冲区溢出超越20个字节在这种情况下,当多个线程试图写日志缓冲区

推荐答案

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

There is no problem using snprintf() in multiple threads. But here you are writing to a shared string buffer, which I assume is shared across threads.

所以的您使用此功能的会的的线程安全的。

So your use of this function would not be thread safe.

这篇关于sprintf的是线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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