提升线程抛出异常"thread_resource_error:资源暂时不可用"

dea*_*sin 4 c++ resources multithreading boost exception

我有类似于以下代码的代码

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);
  }     
}
Run Code Online (Sandbox Code Playgroud)

myFunction从缓冲区读取并写入另一个缓冲区.它永远不会写入写缓冲区中的相同位置.我在这里做过线程的根本错误吗?使用相同的线程名称循环创建线程是不是很糟糕?它运行平稳一段时间然后我得到以下异常.

在抛出'boost :: exception_detail :: clone_impl>'的实例后调用终止what():boost :: thread_resource_error:资源暂时不可用Aborted

这个例外是什么意思?任何想法都会有所帮助.

seh*_*ehe 8

每个进程可以创建的线程数量有限制.

例如,在linux上,

cat /proc/sys/kernel/threads-max
Run Code Online (Sandbox Code Playgroud)

告诉你当前的最大值.默认值是内存页数/ 4,因此在我的系统上它是513785,但在另一个盒子上可能要低得多.例如,在我的邮件服务器盒(512mb RAM)上它只有7295.

你可以限制.但事实上,这将是无用的,因为操作系统无法有效地安排它们.所以,请尝试使用线程池.

哦.PS. detach()- 他的线程有助于(很多)节约资源.pthreads可能会在达到操作系统限制之前阻止线程创建,因为它需要分配跟踪活动线程的开销.detach释放它们(并删除在程序退出之前不加入所有线程的错误).

更新疯狂星期五奖金:一个自动扩展到系统核心数量的线程池:

#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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @StackedCrooked嗯.这个比喻被打破了,因为孩子们实际上***会有相同的名字.线程没有这样的共同特征.它们碰巧被分配给相同的`thread`变量一次(但从不同时).所以,这更像是_"如果你杀了一个男孩(你在一个特定的房子里遇到过),所有和那个男孩住在同一栋房子里的男孩都会被杀死"_? (2认同)