I am using QBuffer in ReadWrite
mode. One worker QThread
pushes data in the buffer and another QThread
reads from it.
我在读写模式中使用QBuffer。一个worker QThread在缓冲区中推送数据,另一个QThread从它读取数据。
Does QBuffer
guarantee thread-safety or do I need to derive from QBuffer
and add mutex stuff?
QBuffer是否保证线程安全,或者是否需要从QBuffer派生并添加互斥对象?
4 个解决方案
#1
8
To quote Mark Summerfield's book C++ GUI Programming with Qt 4:
引用马克·萨默菲尔德的《c++ GUI编程》
Qt's thread-safe classes include QMutex, QMutexLocker, QReadWriteLock, QReadLocker, QWriteLocker, QSemaphore, QThreadStorage, and QWaitCondition. In addition, parts of the QThread API and several other functions are thread-safe, notably QObject::connect(), QObject::disconnect(), QCoreApplication::postEvent(), and QCoreApplication::removePostedEvents().
Qt的线程安全类包括QMutex、QMutexLocker、QReadWriteLock、QReadLocker、QWriteLocker、QSemaphore、QThreadStorage和QWaitCondition。此外,QThread API和其他几个函数的部分是线程安全的,特别是QObject::connect()、QObject::disconnect()、QCoreApplication::postEvent()和QCoreApplication::removePostedEvents()。
Qt expects that you will use locking mechanisms around most of it's classes. The docs will say "All functions are thread-safe" if they are, and the individual functions will also specify "is thread-safe".
Qt期望您将在大部分的类中使用锁定机制。如果是的话,文档会说“所有的函数都是线程安全的”,并且单个函数也会指定“是线程安全的”。
笔记Qt类
Many Qt classes are reentrant, but they are not made thread-safe, because making them thread-safe would incur the extra overhead of repeatedly locking and unlocking a QMutex. For example, QString is reentrant but not thread-safe. You can safely access different instances of QString from multiple threads simultaneously, but you can't safely access the same instance of QString from multiple threads simultaneously (unless you protect the accesses yourself with a QMutex).
许多Qt类都是可重入的,但是它们并不是线程安全的,因为使它们线程安全会导致重复锁定和解锁QMutex的额外开销。例如,QString是可重入的,但不是线程安全的。您可以安全地从多个线程同时访问QString的不同实例,但是您不能同时安全地从多个线程访问相同的QString实例(除非您使用QMutex保护访问权限)。
Some Qt classes and functions are thread-safe. These are mainly the thread-related classes (e.g. QMutex) and fundamental functions (e.g. QCoreApplication::postEvent()).
一些Qt类和函数是线程安全的。这些主要是与线程相关的类(例如QMutex)和基本函数(如QCoreApplication: postEvent())。
Because QBuffer
is a direct subclass of QIODevice
I would especially expect it not to be thread-safe, but there are container classes that are thread-safe for read-access, but would require locking for write access:
因为QBuffer是QIODevice的直接子类,所以我特别希望它不是线程安全的,但是有一些容器类是线程安全的,用于读取访问,但是需要锁定写入访问:
容器类
The container classes are implicitly shared, they are reentrant, and they are optimized for speed, low memory consumption, and minimal inline code expansion, resulting in smaller executables. In addition, they are thread-safe in situations where they are used as read-only containers by all threads used to access them.
容器类是隐式共享的,它们是可重入的,它们是为速度、低内存消耗和最小内联代码扩展而优化的,从而导致了更小的可执行文件。此外,它们是线程安全的,因为它们被用于访问它们的所有线程用作只读容器。
#2
2
QBuffer
isn't the best way to communicate between threads as writing to it makes the buffer grows, but reading from it doesn't delete the data at the beginning.
QBuffer并不是在线程间通信的最好方式,因为它可以使缓冲区增长,但是从它开始读取并不会在开始时删除数据。
You could instead use signal/slot with a QByteArray
parameter, use QLocalSocket
or write a thread-safe ring buffer class derived from QIODevice
yourself.
您可以使用QByteArray参数使用signal/slot,使用QLocalSocket或编写从QIODevice自己派生的线程安全的环形缓冲类。
#3
1
That extends QIODevice, and the documentation there states that all methods on QIODevice are reentrant, but doesn't specify any thread safety on top of that. Given that QBuffer doesn't mention anything more I expect that QBuffer is not thread safe.
这扩展了QIODevice,并且文档说明了QIODevice上的所有方法都是可重入的,但是没有指定任何线程安全。考虑到QBuffer没有提到任何东西,我希望QBuffer不是线程安全的。
http://qt-project.org/doc/qt-4.8/qiodevice.html
http://qt project.org/doc/qt - 4.8 - / - qiodevice.html
#4
0
The simplest way of communicating between threads in Qt is by posting events to the other thread's event queue. This assumes that the other thread spins an event loop. It only needs to spin it periodically where you'd ordinarily check for new data etc.
在Qt中,线程间通信的最简单方式是将事件发送到其他线程的事件队列。这假设另一个线程旋转一个事件循环。它只需要周期性地旋转它,你通常会检查新的数据等等。
#1
8
To quote Mark Summerfield's book C++ GUI Programming with Qt 4:
引用马克·萨默菲尔德的《c++ GUI编程》
Qt's thread-safe classes include QMutex, QMutexLocker, QReadWriteLock, QReadLocker, QWriteLocker, QSemaphore, QThreadStorage, and QWaitCondition. In addition, parts of the QThread API and several other functions are thread-safe, notably QObject::connect(), QObject::disconnect(), QCoreApplication::postEvent(), and QCoreApplication::removePostedEvents().
Qt的线程安全类包括QMutex、QMutexLocker、QReadWriteLock、QReadLocker、QWriteLocker、QSemaphore、QThreadStorage和QWaitCondition。此外,QThread API和其他几个函数的部分是线程安全的,特别是QObject::connect()、QObject::disconnect()、QCoreApplication::postEvent()和QCoreApplication::removePostedEvents()。
Qt expects that you will use locking mechanisms around most of it's classes. The docs will say "All functions are thread-safe" if they are, and the individual functions will also specify "is thread-safe".
Qt期望您将在大部分的类中使用锁定机制。如果是的话,文档会说“所有的函数都是线程安全的”,并且单个函数也会指定“是线程安全的”。
笔记Qt类
Many Qt classes are reentrant, but they are not made thread-safe, because making them thread-safe would incur the extra overhead of repeatedly locking and unlocking a QMutex. For example, QString is reentrant but not thread-safe. You can safely access different instances of QString from multiple threads simultaneously, but you can't safely access the same instance of QString from multiple threads simultaneously (unless you protect the accesses yourself with a QMutex).
许多Qt类都是可重入的,但是它们并不是线程安全的,因为使它们线程安全会导致重复锁定和解锁QMutex的额外开销。例如,QString是可重入的,但不是线程安全的。您可以安全地从多个线程同时访问QString的不同实例,但是您不能同时安全地从多个线程访问相同的QString实例(除非您使用QMutex保护访问权限)。
Some Qt classes and functions are thread-safe. These are mainly the thread-related classes (e.g. QMutex) and fundamental functions (e.g. QCoreApplication::postEvent()).
一些Qt类和函数是线程安全的。这些主要是与线程相关的类(例如QMutex)和基本函数(如QCoreApplication: postEvent())。
Because QBuffer
is a direct subclass of QIODevice
I would especially expect it not to be thread-safe, but there are container classes that are thread-safe for read-access, but would require locking for write access:
因为QBuffer是QIODevice的直接子类,所以我特别希望它不是线程安全的,但是有一些容器类是线程安全的,用于读取访问,但是需要锁定写入访问:
容器类
The container classes are implicitly shared, they are reentrant, and they are optimized for speed, low memory consumption, and minimal inline code expansion, resulting in smaller executables. In addition, they are thread-safe in situations where they are used as read-only containers by all threads used to access them.
容器类是隐式共享的,它们是可重入的,它们是为速度、低内存消耗和最小内联代码扩展而优化的,从而导致了更小的可执行文件。此外,它们是线程安全的,因为它们被用于访问它们的所有线程用作只读容器。
#2
2
QBuffer
isn't the best way to communicate between threads as writing to it makes the buffer grows, but reading from it doesn't delete the data at the beginning.
QBuffer并不是在线程间通信的最好方式,因为它可以使缓冲区增长,但是从它开始读取并不会在开始时删除数据。
You could instead use signal/slot with a QByteArray
parameter, use QLocalSocket
or write a thread-safe ring buffer class derived from QIODevice
yourself.
您可以使用QByteArray参数使用signal/slot,使用QLocalSocket或编写从QIODevice自己派生的线程安全的环形缓冲类。
#3
1
That extends QIODevice, and the documentation there states that all methods on QIODevice are reentrant, but doesn't specify any thread safety on top of that. Given that QBuffer doesn't mention anything more I expect that QBuffer is not thread safe.
这扩展了QIODevice,并且文档说明了QIODevice上的所有方法都是可重入的,但是没有指定任何线程安全。考虑到QBuffer没有提到任何东西,我希望QBuffer不是线程安全的。
http://qt-project.org/doc/qt-4.8/qiodevice.html
http://qt project.org/doc/qt - 4.8 - / - qiodevice.html
#4
0
The simplest way of communicating between threads in Qt is by posting events to the other thread's event queue. This assumes that the other thread spins an event loop. It only needs to spin it periodically where you'd ordinarily check for new data etc.
在Qt中,线程间通信的最简单方式是将事件发送到其他线程的事件队列。这假设另一个线程旋转一个事件循环。它只需要周期性地旋转它,你通常会检查新的数据等等。