如何改变boost线程限制?
所以我尝试使用 boost 1.47.0 创建简单的速度台。但如果我尝试创建超过 1450 个线程,则会抛出异常。如何摆脱这种 boost::tread 限制?
我的代码示例:
#include <iostream>
#include <boost/thread.hpp>
#include <map>
#include <boost/thread.hpp>
#include <boost/thread/locks.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random.hpp>
#include <boost/timer.hpp>
class TestDs
{
public:
virtual bool containsKey(int key)=0;
virtual int get(int key)=0;
virtual int put(int key, int value)=0;
virtual int remove(int key)=0;
virtual int size()=0;
virtual const char* name()=0;
virtual void print()=0;
virtual void shutdown()=0;
};
class GeneralMap: public TestDs
{
private:
std::map<int,int> _ds;
mutable boost::mutex mut_;
public:
GeneralMap() {}
bool containsKey(int key) {
boost::mutex::scoped_lock lock(mut_);
if ( _ds.find(key) != _ds.end())
{
return true;
}
else
{
return false;
}
}
int get(int key) {
boost::mutex::scoped_lock lock(mut_);
return _ds[key];
}
int put(int key, int value) {
boost::mutex::scoped_lock lock(mut_);
_ds.insert(std::pair<int, int>(key,value));
return key;
}
int remove(int key) {
boost::mutex::scoped_lock lock(mut_);
return _ds.erase(key);
}
int size() {
boost::mutex::scoped_lock lock(mut_);
return _ds.size();
}
const char* name() {
return "StdMap";
}
void print() {}
void shutdown() {}
};
int n;
boost::shared_mutex tests;
boost::shared_mutex results;
boost::timer timerForCaptureFame;
GeneralMap Ds;
void test( int i)
{
boost::shared_lock<boost::shared_mutex> lock_r(results);
boost::shared_lock<boost::shared_mutex> lock(tests);
Ds.put(i, 0);
if (Ds.containsKey(i))
{
Ds.get(i);
}
Ds.remove(i);
}
void result()
{
boost::upgrade_lock<boost::shared_mutex> lock(results);
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
std::cout << std::endl << "test of " << Ds.name() << " complite;" << std::endl << "test performed on " << n << " items" << std::endl << "test duration: " << timerForCaptureFame.elapsed() << std::endl;
}
void create_tests( int n)
{
boost::upgrade_lock<boost::shared_mutex> lock(tests);
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
boost::shared_lock<boost::shared_mutex> lock_r(results);
for(int i=0; i<n; i++)
{
boost::thread worker(test, i);
}
boost::thread worker_r(result);
timerForCaptureFame.restart();
return;
}
int main()
{
n = 1000;// if n == 1600 crushes.
create_tests(n);
std::cin.get();
return 0;
}
So I try to create simple speed bench with boost 1.47.0. But if I try to create more than 1450 threads it throws exeption. How to get rid of such boost::tread limitation?
My code sample:
#include <iostream>
#include <boost/thread.hpp>
#include <map>
#include <boost/thread.hpp>
#include <boost/thread/locks.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random.hpp>
#include <boost/timer.hpp>
class TestDs
{
public:
virtual bool containsKey(int key)=0;
virtual int get(int key)=0;
virtual int put(int key, int value)=0;
virtual int remove(int key)=0;
virtual int size()=0;
virtual const char* name()=0;
virtual void print()=0;
virtual void shutdown()=0;
};
class GeneralMap: public TestDs
{
private:
std::map<int,int> _ds;
mutable boost::mutex mut_;
public:
GeneralMap() {}
bool containsKey(int key) {
boost::mutex::scoped_lock lock(mut_);
if ( _ds.find(key) != _ds.end())
{
return true;
}
else
{
return false;
}
}
int get(int key) {
boost::mutex::scoped_lock lock(mut_);
return _ds[key];
}
int put(int key, int value) {
boost::mutex::scoped_lock lock(mut_);
_ds.insert(std::pair<int, int>(key,value));
return key;
}
int remove(int key) {
boost::mutex::scoped_lock lock(mut_);
return _ds.erase(key);
}
int size() {
boost::mutex::scoped_lock lock(mut_);
return _ds.size();
}
const char* name() {
return "StdMap";
}
void print() {}
void shutdown() {}
};
int n;
boost::shared_mutex tests;
boost::shared_mutex results;
boost::timer timerForCaptureFame;
GeneralMap Ds;
void test( int i)
{
boost::shared_lock<boost::shared_mutex> lock_r(results);
boost::shared_lock<boost::shared_mutex> lock(tests);
Ds.put(i, 0);
if (Ds.containsKey(i))
{
Ds.get(i);
}
Ds.remove(i);
}
void result()
{
boost::upgrade_lock<boost::shared_mutex> lock(results);
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
std::cout << std::endl << "test of " << Ds.name() << " complite;" << std::endl << "test performed on " << n << " items" << std::endl << "test duration: " << timerForCaptureFame.elapsed() << std::endl;
}
void create_tests( int n)
{
boost::upgrade_lock<boost::shared_mutex> lock(tests);
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
boost::shared_lock<boost::shared_mutex> lock_r(results);
for(int i=0; i<n; i++)
{
boost::thread worker(test, i);
}
boost::thread worker_r(result);
timerForCaptureFame.restart();
return;
}
int main()
{
n = 1000;// if n == 1600 crushes.
create_tests(n);
std::cin.get();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定这是 Boost 的限制,而是 Windows 的限制。根据 Mark Russinovich 的最大线程数32 位操作系统是 2048,这意味着您已经分配了大约 3/4 的最大允许线程数。如果您有其他进程正在运行,则会减少可供您使用的线程数量。
I am not so sure it is a limitation of Boost, but a limitation of Windows. According to Mark Russinovich the maximum threads on a 32 bit OS is 2048 which means you have allocated approximatly 3/4's of the maximum allowed threads. If you have other processes running it will decrease the amount of threads available for you to use.