我有一个 C++ 库,它应该在多个线程上进行一些计算。我制作了独立的线程代码(即它们之间没有共享变量),除了一个数组。问题是,我不知道如何让它成为线程安全的。
我查看了互斥锁锁定/解锁(QMutex
,因为我正在使用 Qt),但它不适合我的任务 - 虽然一个线程将锁定互斥锁,但其他线程将等待!
然后我阅读了有关 std::atomic
的内容,它看起来正是我所需要的。不过,我尝试通过以下方式使用它:
std::vector<std::atomic<uint64_t>> *myVector;
它产生了编译器错误(use of deleted function 'std::atomic::atomic(const std::atomic&)')。然后我找到the solution - 为 std::atomic
使用特殊包装器。我试过这个:
struct AtomicUInt64
{
std::atomic<uint64_t> atomic;
AtomicUInt64() : atomic() {}
AtomicUInt64 ( std::atomic<uint64_t> a ) : atomic ( atomic.load() ) {}
AtomicUInt64 ( AtomicUInt64 &auint64 ) : atomic ( auint64.atomic.load() ) {}
AtomicUInt64 &operator= ( AtomicUInt64 &auint64 )
{
atomic.store ( auint64.atomic.load() );
}
};
std::vector<AtomicUInt64> *myVector;
这个东西编译成功,但是当我无法填充 vector 时:
myVector = new std::vector<AtomicUInt64>();
for ( int x = 0; x < 100; ++x )
{
/* This approach produces compiler error:
* use of deleted function 'std::atomic<long long unsigned int>::atomic(const std::atomic<long long unsigned int>&)'
*/
AtomicUInt64 value( std::atomic<uint64_t>( 0 ) ) ;
myVector->push_back ( value );
/* And this one produces the same error: */
std::atomic<uint64_t> value1 ( 0 );
myVector->push_back ( value1 );
}
我做错了什么?我假设我尝试了所有方法(也许没有)但没有任何帮助。在 C++ 中是否有任何其他线程安全数组共享方法?
顺便说一句,我在 Windows 上使用 MinGW 32 位 4.7 编译器。
最佳答案
这是您的 AtomicUInt64
类型的清理版本:
template<typename T>
struct MobileAtomic
{
std::atomic<T> atomic;
MobileAtomic() : atomic(T()) {}
explicit MobileAtomic ( T const& v ) : atomic ( v ) {}
explicit MobileAtomic ( std::atomic<T> const& a ) : atomic ( a.load() ) {}
MobileAtomic ( MobileAtomic const&other ) : atomic( other.atomic.load() ) {}
MobileAtomic& operator=( MobileAtomic const &other )
{
atomic.store( other.atomic.load() );
return *this;
}
};
typedef MobileAtomic<uint64_t> AtomicUInt64;
并使用:
AtomicUInt64 value;
myVector->push_back ( value );
或:
AtomicUInt64 value(x);
myVector->push_back ( value );
您的问题是您按值获取了 std::atomic
,这会导致复制被阻止。哦,您未能从 operator=
返回。我还使一些构造函数显式化,这可能是不必要的。我将 const
添加到您的复制构造函数中。
我也想将 store
和 load
方法添加到转发到 atomic.store
的 MobileAtomic
和 atomic.load
。
关于c++ - 线程安全无锁数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16919858/