Qt库-静态成员函数的线程安全性
Qt library - thread safety of static members functions
Qt的文档指出,所有QDateTime函数都是可重入的,用Qt的术语来说,这意味着如果你在另一个线程中创建新的QDateTime对象,你可以安全地使用它。但是以下静态成员是线程安全的吗:QDateTime::currentDateTime和QDateTime::fromTime_t?
辅助线程中的代码:
// Is line below thread safe?
QDateTime tDateTimeNow1 = QDateTime::currentDateTime();
// The below code should be no different then the one above..
QDateTime tDateTimeNow2;
tDateTimeNow2 = tDateTimeNow2.currentDateTime();
我对这篇文章中的以下说法感到困惑http://doc.qt.nokia.com/4.7-snapshot/thread-basics.html:"在Qt文档中,QDateTime::currentDateTime()没有被标记为线程安全的,但是我们可以在这个小例子中使用它,因为我们知道QDateTime::currentDateTime()静态方法没有在任何其他线程中使用。">
如果QDateTime::currentDateTime()不能在辅助线程中使用,那么我们如何以线程安全的方式创建具有当前日期时间的QDateTime对象?
以下是其他类似于上面的静态成员函数,我不知道它们是否可以在线程中安全使用:1) Q定时器::singleShot2) Q字符串::fromUtf83) Q字符串:数字
如果您需要一种线程安全的方式来获取具有当前时间的QDateTime对象,请创建一个保护不安全调用的函数。
QDateTime getCurrentTime()
{
static QMutex mutex;
QMutexLocker locker(&mutex);
return QDateTime::currentDateTime();
}
相关文章:
- 类与私有变量的其他类之间的线程安全性
- 调用socket.remote_endpoint(提升 asio 库)线程安全性
- std::lock_guard 似乎提供了线程安全性,尽管作用域块
- C++中向量和列表的非写入成员函数的线程安全性
- 线程安全性和静态变量/成员功能
- 是仅使用get或toplown的原始类型的线程安全性的威胁
- 提高 ASIO stream_descriptor和事件 FD 线程安全性
- OpenMP中树结构的线程安全性
- 静态变量初始化的线程安全性
- 从另一个(非 qt)线程调用 QObject 方法的线程安全性?
- C 共享_ptr如何确保线程安全性
- 编写std::vector与普通数组的线程安全性
- C++标准库容器相对于所包含对象的线程安全性
- 读取和写入操作的线程安全性C++
- Qt库-静态成员函数的线程安全性
- QObject可重入性和线程安全性
- std::map中的线程安全性
- boost::asio io_service 和 std::containers 的线程安全性
- C++11 std:原子<T>复制构造函数的线程安全性
- Clang 线程安全性分析和线程角色