提高线程引发异常" thread_resource_error:资源暂时不可用" [英] boost thread throwing exception "thread_resource_error: resource temporarily unavailable"

查看:2904
本文介绍了提高线程引发异常" thread_resource_error:资源暂时不可用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似以下code

I have code similar to the following code

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

for(int i=0; i<bignumber; ++i){
  functiondostuff();
  for(int j=0; j<2; ++j){
    functiondomorestuff();
    myThread = boost::thread(&myClass::myFunction, this, j, i);
  }     
}

myFunction的从缓冲器读取和写入到另一个。它永远不会写入在写入缓冲器中的同一位置。我在做某种根本性错误在这里的主题?它是坏的遍历线程的创建与同一个线程的名字?它运行平稳一段时间,然后我得到下面的异常。

myFunction reads from a buffer and writes to another. It will never write to the same location in the write buffer. Am I doing something fundamentally wrong with threads here? Is it bad to loop over a thread creation with the same thread name? It runs smooth for a while and then I get the following exception.

抛'的boost :: exception_detail :: clone_impl>'一个实例后终止叫
  什么()抛出boost :: thread_resource_error:资源暂时不可用
中止

terminate called after throwing an instance of 'boost::exception_detail::clone_impl >' what(): boost::thread_resource_error: Resource temporarily unavailable Aborted

这是什么意思例外?任何意见将是有益的。

What does this exception mean? Any ideas would be helpful.

推荐答案

有是你可以按进程创建的线程数量的限制。

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

在Linux上,例如,

On linux, for example,

cat /proc/sys/kernel/threads-max

告诉你当前最大。默认为内存页/ 4的数,所以我的系统中这是513785,但它可以是在另一箱得多低得多。例如。在我的邮件服务器箱(512MB RAM),它只有7295。

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.

哦。 PS。 分离() -ing他线程的帮助(很多)与节约资源。 的pthreads 可能会阻塞线程的创建达到OS限制之前好,因为它需要分配的开销跟踪活动线程。 分离释放这些了(并删除不加入程序退出之前的所有线程的错误)。

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 <boost/thread.hpp>
#include <boost/phoenix.hpp>
#include <boost/optional.hpp>

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<void()> job_t;
      std::deque<job_t> _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<mutex> lk(mx);
          _queue.push_back(std::move(job));

          cv.notify_one();
      }

      optional<job_t> dequeue() 
      {
          unique_lock<mutex> 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<mutex> 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<bignumber; ++i)
        {
            functiondostuff();
            for(int j=0; j<2; ++j) {
                functiondomorestuff();
                pool.enqueue(bind(&myClass::myFunction, this, j, i));
            }     
        }
        std::cout << "done\n";
    }

  private:
    void myFunction(int i, int j)
    {
        boost::this_thread::sleep_for(boost::chrono::milliseconds(1));
        counter += 1;
    }
};

int main()
{
    myClass instance;
    instance.wreak_havoc();

    size_t last = 0;
    while (counter < (2*bignumber))
    {
        boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
        if ((counter >> 4u) > last)
        {
            std::cout << "Progress: " << counter << "/" << (bignumber*2) << "\n";
            last = counter >> 4u;
        }
    }
}

这篇关于提高线程引发异常&QUOT; thread_resource_error:资源暂时不可用&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆