Boost thread resource error как исправить

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();...

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 <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 << "donen";
    }

  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;
        }
    }
}

Содержание

  1. Boost C++ Libraries
  2. Boost.Threads
  3. Header
  4. Contents
  5. Introduction
  6. Classes
  7. Class lock_error
  8. Class lock_error synopsis
  9. Class lock_error constructors and destructor
  10. Class thread_resource_error
  11. Class thread_resource_error synopsis
  12. Class thread_resource_error constructors and destructor
  13. Boost C++ Libraries
  14. Thread Management
  15. Synopsis
  16. Launching threads
  17. Exceptions in thread functions
  18. Joining and detaching
  19. Interruption
  20. Predefined Interruption Points
  21. Thread IDs
  22. Boost C++ Libraries
  23. Thread Management
  24. Synopsis
  25. Launching threads
  26. Exceptions in thread functions
  27. Joining and detaching
  28. Interruption
  29. Predefined Interruption Points
  30. Thread IDs

Boost C++ Libraries

. one of the most highly regarded and expertly designed C++ library projects in the world. — Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Boost.Threads

Contents

Introduction

Include the header to define the exception types that may be thrown by Boost.Threads classes.

Classes

Class lock_error

The lock_error class defines an exception type thrown to indicate a locking related error has been detected. Examples of such errors include a lock operation which can be determined to result in a deadlock, or unlock operations attempted by a thread that does not own the lock.

Class lock_error synopsis

Class lock_error constructors and destructor

Class thread_resource_error

The thread_resource_error class defines an exception type that is thrown by constructors in the Boost.Threads library when thread related resources can not be acquired. This does not include memory allocation failures which instead throw std::bad_alloc.

Class thread_resource_error synopsis

Class thread_resource_error constructors and destructor

Revised 05 November, 2001

© Copyright William E. Kempf 2001-2002. All Rights Reserved.

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. William E. Kempf makes no representations about the suitability of this software for any purpose. It is provided «as is» without express or implied warranty.

Источник

Boost C++ Libraries

. one of the most highly regarded and expertly designed C++ library projects in the world. — Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Thread Management

Synopsis

The boost :: thread class is responsible for launching and managing threads. Each boost :: thread object represents a single thread of execution, or Not-a-Thread , and at most one boost :: thread object represents a given thread of execution: objects of type boost :: thread are not copyable.

Objects of type boost :: thread are movable, however, so they can be stored in move-aware containers, and returned from functions. This allows the details of thread creation to be wrapped in a function.

[Note: On compilers that support rvalue references, boost :: thread provides a proper move constructor and move-assignment operator, and therefore meets the C++0x MoveConstructible and MoveAssignable concepts. With such compilers, boost :: thread can therefore be used with containers that support those concepts.

For other compilers, move support is provided with a move emulation layer, so containers must explicitly detect that move emulation layer. See for details.]

Launching threads

A new thread is launched by passing an object of a callable type that can be invoked with no parameters to the constructor. The object is then copied into internal storage, and invoked on the newly-created thread of execution. If the object must not (or cannot) be copied, then boost :: ref can be used to pass in a reference to the function object. In this case, the user of Boost.Thread must ensure that the referred-to object outlives the newly-created thread of execution.

If you wish to construct an instance of boost :: thread with a function or callable object that requires arguments to be supplied, this can be done by passing additional arguments to the boost :: thread constructor:

The arguments are copied into the internal thread structure: if a reference is required, use boost :: ref , just as for references to callable functions.

There is an unspecified limit on the number of additional arguments that can be passed.

Exceptions in thread functions

If the function or callable object passed to the boost :: thread constructor propagates an exception when invoked that is not of type boost :: thread_interrupted , std :: terminate () is called.

Joining and detaching

When the boost :: thread object that represents a thread of execution is destroyed the thread becomes detached . Once a thread is detached, it will continue executing until the invocation of the function or callable object supplied on construction has completed, or the program is terminated. A thread can also be detached by explicitly invoking the detach () member function on the boost :: thread object. In this case, the boost :: thread object ceases to represent the now-detached thread, and instead represents Not-a-Thread .

In order to wait for a thread of execution to finish, the join () or timed_join () member functions of the boost :: thread object must be used. join () will block the calling thread until the thread represented by the boost :: thread object has completed. If the thread of execution represented by the boost :: thread object has already completed, or the boost :: thread object represents Not-a-Thread , then join () returns immediately. timed_join () is similar, except that a call to timed_join () will also return if the thread being waited for does not complete when the specified time has elapsed.

Interruption

A running thread can be interrupted by invoking the interrupt () member function of the corresponding boost :: thread object. When the interrupted thread next executes one of the specified interruption points (or if it is currently blocked whilst executing one) with interruption enabled, then a boost :: thread_interrupted exception will be thrown in the interrupted thread. If not caught, this will cause the execution of the interrupted thread to terminate. As with any other exception, the stack will be unwound, and destructors for objects of automatic storage duration will be executed.

If a thread wishes to avoid being interrupted, it can create an instance of boost :: this_thread :: disable_interruption . Objects of this class disable interruption for the thread that created them on construction, and restore the interruption state to whatever it was before on destruction:

The effects of an instance of boost :: this_thread :: disable_interruption can be temporarily reversed by constructing an instance of boost :: this_thread :: restore_interruption , passing in the boost :: this_thread :: disable_interruption object in question. This will restore the interruption state to what it was when the boost :: this_thread :: disable_interruption object was constructed, and then disable interruption again when the boost :: this_thread :: restore_interruption object is destroyed.

At any point, the interruption state for the current thread can be queried by calling boost :: this_thread :: interruption_enabled () .

Predefined Interruption Points

The following functions are interruption points , which will throw boost :: thread_interrupted if interruption is enabled for the current thread, and interruption is requested for the current thread:

Thread IDs

Objects of class boost :: thread :: id can be used to identify threads. Each running thread of execution has a unique ID obtainable from the corresponding boost :: thread by calling the get_id () member function, or by calling boost :: this_thread :: get_id () from within the thread. Objects of class boost :: thread :: id can be copied, and used as keys in associative containers: the full range of comparison operators is provided. Thread IDs can also be written to an output stream using the stream insertion operator, though the output format is unspecified.

Each instance of boost :: thread :: id either refers to some thread, or Not-a-Thread . Instances that refer to Not-a-Thread compare equal to each other, but not equal to any instances that refer to an actual thread of execution. The comparison operators on boost :: thread :: id yield a total order for every non-equal thread ID.

Источник

Boost C++ Libraries

. one of the most highly regarded and expertly designed C++ library projects in the world. — Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Thread Management

Synopsis

The boost :: thread class is responsible for launching and managing threads. Each boost :: thread object represents a single thread of execution, or Not-a-Thread , and at most one boost :: thread object represents a given thread of execution: objects of type boost :: thread are not copyable.

Objects of type boost :: thread are movable, however, so they can be stored in move-aware containers, and returned from functions. This allows the details of thread creation to be wrapped in a function.

[Note: On compilers that support rvalue references, boost :: thread provides a proper move constructor and move-assignment operator, and therefore meets the C++0x MoveConstructible and MoveAssignable concepts. With such compilers, boost :: thread can therefore be used with containers that support those concepts.

For other compilers, move support is provided with a move emulation layer, so containers must explicitly detect that move emulation layer. See for details.]

Launching threads

A new thread is launched by passing an object of a callable type that can be invoked with no parameters to the constructor. The object is then copied into internal storage, and invoked on the newly-created thread of execution. If the object must not (or cannot) be copied, then boost :: ref can be used to pass in a reference to the function object. In this case, the user of Boost.Thread must ensure that the referred-to object outlives the newly-created thread of execution.

If you wish to construct an instance of boost :: thread with a function or callable object that requires arguments to be supplied, this can be done by passing additional arguments to the boost :: thread constructor:

The arguments are copied into the internal thread structure: if a reference is required, use boost :: ref , just as for references to callable functions.

There is an unspecified limit on the number of additional arguments that can be passed.

Exceptions in thread functions

If the function or callable object passed to the boost :: thread constructor propagates an exception when invoked that is not of type boost :: thread_interrupted , std :: terminate () is called.

Joining and detaching

When the boost :: thread object that represents a thread of execution is destroyed the thread becomes detached . Once a thread is detached, it will continue executing until the invocation of the function or callable object supplied on construction has completed, or the program is terminated. A thread can also be detached by explicitly invoking the detach () member function on the boost :: thread object. In this case, the boost :: thread object ceases to represent the now-detached thread, and instead represents Not-a-Thread .

In order to wait for a thread of execution to finish, the join () or timed_join () member functions of the boost :: thread object must be used. join () will block the calling thread until the thread represented by the boost :: thread object has completed. If the thread of execution represented by the boost :: thread object has already completed, or the boost :: thread object represents Not-a-Thread , then join () returns immediately. timed_join () is similar, except that a call to timed_join () will also return if the thread being waited for does not complete when the specified time has elapsed.

Interruption

A running thread can be interrupted by invoking the interrupt () member function of the corresponding boost :: thread object. When the interrupted thread next executes one of the specified interruption points (or if it is currently blocked whilst executing one) with interruption enabled, then a boost :: thread_interrupted exception will be thrown in the interrupted thread. If not caught, this will cause the execution of the interrupted thread to terminate. As with any other exception, the stack will be unwound, and destructors for objects of automatic storage duration will be executed.

If a thread wishes to avoid being interrupted, it can create an instance of boost :: this_thread :: disable_interruption . Objects of this class disable interruption for the thread that created them on construction, and restore the interruption state to whatever it was before on destruction:

The effects of an instance of boost :: this_thread :: disable_interruption can be temporarily reversed by constructing an instance of boost :: this_thread :: restore_interruption , passing in the boost :: this_thread :: disable_interruption object in question. This will restore the interruption state to what it was when the boost :: this_thread :: disable_interruption object was constructed, and then disable interruption again when the boost :: this_thread :: restore_interruption object is destroyed.

At any point, the interruption state for the current thread can be queried by calling boost :: this_thread :: interruption_enabled () .

Predefined Interruption Points

The following functions are interruption points , which will throw boost :: thread_interrupted if interruption is enabled for the current thread, and interruption is requested for the current thread:

Thread IDs

Objects of class boost :: thread :: id can be used to identify threads. Each running thread of execution has a unique ID obtainable from the corresponding boost :: thread by calling the get_id () member function, or by calling boost :: this_thread :: get_id () from within the thread. Objects of class boost :: thread :: id can be copied, and used as keys in associative containers: the full range of comparison operators is provided. Thread IDs can also be written to an output stream using the stream insertion operator, though the output format is unspecified.

Each instance of boost :: thread :: id either refers to some thread, or Not-a-Thread . Instances that refer to Not-a-Thread compare equal to each other, but not equal to any instances that refer to an actual thread of execution. The comparison operators on boost :: thread :: id yield a total order for every non-equal thread ID.

Источник

C++ Boost

Header <boost/thread/exceptions.hpp>


Contents

Introduction
Classes
Class lock_error
Class lock_error synopsis
Class lock_error constructors
and destructor
Class thread_resource_error
Class thread_resource_error
synopsis
Class thread_resource_error
constructors and destructor

Introduction

Include the header <boost/thread/exceptions.hpp>
to define the exception types that may be thrown by Boost.Threads classes.

Classes

Class lock_error

The lock_error class defines an exception type thrown to indicate a locking
related error has been detected. Examples of such errors include a lock operation
which can be determined to result in a deadlock, or unlock operations attempted
by a thread that does not own the lock.

Class lock_error synopsis

namespace boost
{
    class lock_error : public std::logical_error
    {
    public:
        lock_error();
    };
};

Class lock_error constructors
and destructor

lock_error();
Effects: Constructs a lock_error object.

Class thread_resource_error

The thread_resource_error class defines an exception type that is thrown by
constructors in the Boost.Threads library when thread related resources can
not be acquired. This does not include memory allocation failures which instead
throw std::bad_alloc.

Class thread_resource_error
synopsis

namespace boost
{
    class thread_resource_error : public std::runtime_error
    {
    public:
        thread_resource_error();
    };
};

Class thread_resource_error
constructors and destructor

thread_resource_error();
Effects: Constructs a thread_resource_error object.

Revised

05 November, 2001

© Copyright William E. Kempf 2001-2002.
All Rights Reserved.

Permission to use, copy, modify, distribute and sell this software and its
documentation for any purpose is hereby granted without fee, provided that the
above copyright notice appear in all copies and that both that copyright notice
and this permission notice appear in supporting documentation. William E. Kempf
makes no representations about the suitability of this software for any purpose.
It is provided «as is» without express or implied warranty.

  • Home
  • Forum
  • The Ubuntu Forum Community
  • Ubuntu Specialised Support
  • Development & Programming
  • Programming Talk
  • boost::thread_resource_error

  1. boost::thread_resource_error

    Hello,

    I am encountering the following error:

    **********************************
    terminate called after throwing an instance of ‘boost::thread_resource_error’
    what(): boost::thread_resource_error
    Abandon
    **********************************

    trying to run a threadgroup of 500 threads as follows:

    thread_group * thgrp = new thread_group();
    for (int i = 0; i < NUMTHREADS; i++) {
    thgrp->create_thread(bind(&simulate, i, 100, 0.05, 0.2, 1.0, 100, 100, endPrices));
    }
    thgrp->join_all();

    Can anyone please help. I don’t understand why I get this error as I have no resource problems with my machine.

    Julien.


  2. Re: boost::thread_resource_error

    I thought you might find the information here to be interesting, and related to your question.

    As for your program, you should consider placing the call to boost::thread_group::create_thread() within try/catch blocks.

    Code:

    try {
       for (int i = 0; i < NUMTHREADS; i++) {
          thgrp->create_thread(bind(&simulate, i, 100, 0.05, 0.2, 1.0, 100, 100, endPrices));
       }
    }
    catch (std::exception& e) {
       std::cerr << "Caught exception: " << e.what() << std::endl;
    }
    thgrp->join_all();

    P.S. There’s no need to allocate the boost::thread_group object. IIRC, it maintains an STL list of allocated boost::thread objects.


  3. Re: boost::thread_resource_error

    thanks a lot for this reply,
    I have too many threads anyway. As suggested by someone, I am going to alter the design of my app.
    Julien.


Tags for this Thread

Bookmarks

Bookmarks


Posting Permissions

$ sudo bitcoind --conf=/home/user/.bitcoin/bitcoin.testnet.conf
************************
EXCEPTION: N5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEEE
boost::thread_resource_error: Resource temporarily unavailable
bitcoin in AppInit()

Bitcoin debug.log:

2015-08-27 16:57:12 Bitcoin version v0.11.0.0-g4a1bc9f (Fri, 10 Jul 2015 19:05:23 +0100)
2015-08-27 16:57:12 Using OpenSSL version OpenSSL 1.0.1k 8 Jan 2015
2015-08-27 16:57:12 Using BerkeleyDB version Berkeley DB 4.8.30: (April  9, 2010)
2015-08-27 16:57:12 Default data directory /home/user/.bitcoin
2015-08-27 16:57:12 Using data directory /home/user/.bitcoin/testnet3
2015-08-27 16:57:12 Using config file /home/user/.bitcoin/bitcoin.testnet.conf
2015-08-27 16:57:12 Using at most 125 connections (1024 file descriptors available)
2015-08-27 16:57:12 Using 4 threads for script verification
2015-08-27 16:57:12 scheduler thread start
2015-08-27 16:57:12 Binding RPC on address ::1 port 18332 (IPv4+IPv6 bind any: 0)
2015-08-27 16:57:12 ERROR: Binding RPC on address ::1 port 18332 failed: open: Address family not supported by protocol
2015-08-27 16:57:12 Binding RPC on address 127.0.0.1 port 18332 (IPv4+IPv6 bind any: 0)
2015-08-27 16:57:12
************************
EXCEPTION: N5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEEE

Libraries linked by bitcoind:

 linux-vdso.so.1 (0x7efcb000)
        /usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0x7697f000)
        libboost_system.so.1.55.0 => /usr/lib/arm-linux-gnueabihf/libboost_system.so.1.55.0 (0x7695a000)
        libboost_filesystem.so.1.55.0 => /usr/lib/arm-linux-gnueabihf/libboost_filesystem.so.1.55.0 (0x76935000)
        libboost_program_options.so.1.55.0 => /usr/lib/arm-linux-gnueabihf/libboost_program_options.so.1.55.0 (0x768c0000)
        libboost_thread.so.1.55.0 => /usr/lib/arm-linux-gnueabihf/libboost_thread.so.1.55.0 (0x7689c000)
        libboost_chrono.so.1.55.0 => /usr/lib/arm-linux-gnueabihf/libboost_chrono.so.1.55.0 (0x76885000)
        libssl.so.1.0.0 => /usr/lib/arm-linux-gnueabihf/libssl.so.1.0.0 (0x7682b000)
        libminiupnpc.so.5 => /usr/lib/libminiupnpc.so.5 (0x7681c000)
        libcrypto.so.1.0.0 => /usr/lib/arm-linux-gnueabihf/libcrypto.so.1.0.0 (0x766af000)
        libanl.so.1 => /lib/arm-linux-gnueabihf/libanl.so.1 (0x7669c000)
        libstdc++.so.6 => /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0x765c0000)
        libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0x76544000)
        libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0x76517000)
        libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0x764ef000)
        libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0x763b2000)
        /lib/ld-linux-armhf.so.3 (0x54b75000)
        librt.so.1 => /lib/arm-linux-gnueabihf/librt.so.1 (0x7639b000)
        libboost_atomic.so.1.55.0 => /usr/lib/arm-linux-gnueabihf/libboost_atomic.so.1.55.0 (0x76388000)
        libdl.so.2 => /lib/arm-linux-gnueabihf/libdl.so.2 (0x76375000)

Other info:

$ uname -a
Linux raspberrypi 4.1.6-v7+ #810 SMP PREEMPT Tue Aug 18 15:32:12 BST 2015 armv7l GNU/Linux
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.9/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Raspbian 4.9.2-10' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 4.9.2 (Raspbian 4.9.2-10)

This error is similar to #639, but it appears at the very beginning.

Понравилась статья? Поделить с друзьями:
  • Bmw f10 ошибка управляемость
  • Bmw f10 ошибка регулировка устойчивости
  • Bmw f10 ошибка 801c20
  • Boost calibration low ошибка
  • Bmw f10 ошибка 26cf00