boost thread throwing exception “thread_resource_error: resource temporarily unavailable”

前端 未结 2 1436
旧巷少年郎
旧巷少年郎 2020-11-28 15:36

I have code similar to the following code

boost::thread myThread
unsigned char readbuffer[bignumber];
unsigned char writebuffer[bignumber];

for(int i=0; i&l         


        
2条回答
  •  心在旅途
    2020-11-28 16:02

    There's a limit on the number of threads you can create per process.

    On linux, for example,

    cat /proc/sys/kernel/threads-max
    

    tells you the current maximum. The default is the number of memory pages/4, so on my system it's 513785, but it may be much much lower on another box. E.g. on my mail server box (512mb RAM) it's only 7295.

    You could the limit. But in fact that will be useless because the OS can't schedule them effectively. So, instead, try using a thread pool.

    Oh. PS. detach()-ing he threads will help (a lot) with conserving resources. pthreads might be blocking thread creation well before the OS limit is reached because it needs to allocate overhead tracking the active threads. detach frees those up (and removes the error of not joining all threads before program exit).

    UPDATE Crazy friday bonus: a thread pool that auto-scales to the number of cores your system has:

    #include 
    #include 
    #include 
    
    using namespace boost;
    using namespace boost::phoenix::arg_names;
    
    boost::atomic_size_t counter(0ul);
    
    class thread_pool
    {
      private:
          mutex mx;
          condition_variable cv;
    
          typedef function job_t;
          std::deque _queue;
    
          thread_group pool;
    
          boost::atomic_bool shutdown;
          static void worker_thread(thread_pool& q)
          {
              while (auto job = q.dequeue())
                  (*job)();
          }
    
      public:
          thread_pool() : shutdown(false) {
              for (unsigned i = 0; i < boost::thread::hardware_concurrency(); ++i)
                  pool.create_thread(bind(worker_thread, ref(*this)));
          }
    
          void enqueue(job_t job) 
          {
              lock_guard lk(mx);
              _queue.push_back(std::move(job));
    
              cv.notify_one();
          }
    
          optional dequeue() 
          {
              unique_lock lk(mx);
              namespace phx = boost::phoenix;
    
              cv.wait(lk, phx::ref(shutdown) || !phx::empty(phx::ref(_queue)));
    
              if (_queue.empty())
                  return none;
    
              auto job = std::move(_queue.front());
              _queue.pop_front();
    
              return std::move(job);
          }
    
          ~thread_pool()
          {
              shutdown = true;
              {
                  lock_guard lk(mx);
                  cv.notify_all();
              }
    
              pool.join_all();
          }
    };
    
    static constexpr size_t bignumber = 1 << 20;
    
    class myClass 
    {
        //unsigned char readbuffer[bignumber];
        //unsigned char writebuffer[bignumber];
        void functiondostuff() { }
        void functiondomorestuff() { }
    
        thread_pool pool; // uses 1 thread per core
    
      public:
        void wreak_havoc()
        {
            std::cout << "enqueuing jobs... " << std::flush;
            for(size_t i=0; i> 4u) > last)
            {
                std::cout << "Progress: " << counter << "/" << (bignumber*2) << "\n";
                last = counter >> 4u;
            }
        }
    }
    

提交回复
热议问题