开发者

How to change boost threads limitation?

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_loc开发者_Go百科k<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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜