我遇到了这个问题并阅读它最终让我看到了boost::detail::spinlock_pool.
其目的boost::detail::spinlock_pool是spinlock通过在shared_ptrs地址上进行散列选择s 数组来减少对全局自旋锁的潜在争用.这似乎是一个合理的解决方案,但目前(Boost v1.49)版本的实现似乎存在问题.
spinlock_pool管理静态分配的41个spinlock实例的数组.看来,sizeof(spinlock)==4对于我所看到的平台 - 这意味着,例如x64与64字节spinlock的高速缓存行,每个高速缓存行将有16 秒.
即整个阵列跨越所有2 1/2缓存线.
也就是说,有一个随机螺旋锁错误共享的可能性为40%.
......首先几乎完全打败了游泳池的目的.
我的分析是正确的还是我遗漏了一些重要的东西?
更新: 我终于写了一个小的基准程序:
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/timer.hpp>
#include <iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
enum { BufferSize = 1<<24, SLsPerCacheLine = 1 };
int ibuffer[BufferSize];
using boost::detail::spinlock;
size_t nslp = 41;
spinlock* pslp = 0;
spinlock& getSpinlock(size_t h)
{
return pslp[ (h%nslp) * SLsPerCacheLine ];
}
void …Run Code Online (Sandbox Code Playgroud) 使用Visual Studio 2010 SP1:
#include <vector>
//namespace XXX {
struct Test
{
bool operator==(const Test& r) const { return true; }
};
//}
//typedef XXX::Test Test;
template <typename T> inline bool operator!=(const T& l,const T& r)
{ return !(l==r); }
int main()
{
std::vector<Test> vt;
std::vector<Test> vt2 = std::move(vt);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我按原样编译上面的代码,它会失败并显示以下错误:
1>C:\apps\MVS10\VC\include\vector(609): error C2593: 'operator !=' is ambiguous
1> C:\apps\MVS10\VC\include\xmemory(268): could be 'bool std::operator !=<_Ty,_Ty>(const std::allocator<_Ty> &,const std::allocator<_Ty> &) throw()'
1> with
1> [
1> _Ty=Test
1> ] …Run Code Online (Sandbox Code Playgroud)