2011-08-27 39 views
0

所以我嘗試用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; 
} 
+0

(Windows測試過) – Rella

+4

你爲什麼要創建這麼多的線程? – Maz

+2

我同意Maz,在線程數量多於處理內核之後,線程逐漸變得不那麼有用。一個理智的應用程序應該永遠不會有接近1450個線程,更不用說100個線程,更不用說100個。 – Mranz

回答

2

我不敢肯定它是加速的限制,但Windows的限制。根據Mark Russinovich,32位操作系統上的最大線程數是2048,這意味着您已經分配了最大允許線程數的大約3/4。如果您有其他進程在運行,它會減少可供您使用的線程數量。

+0

我的窗口是x64 btw .. – Rella

+0

@Kabumbus:你的應用程序是64位應用程序嗎? –