bind_processor_linux.cpp 704 B

123456789101112131415161718192021222324252627282930
  1. // Copyright Oliver Kowalke 2014.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include "bind_processor.hpp"
  6. extern "C"
  7. {
  8. #include <pthread.h>
  9. #include <sched.h>
  10. }
  11. #include <stdexcept>
  12. #include <boost/config/abi_prefix.hpp>
  13. void bind_to_processor( unsigned int n)
  14. {
  15. cpu_set_t cpuset;
  16. CPU_ZERO( & cpuset);
  17. CPU_SET( n, & cpuset);
  18. int errno_( ::pthread_setaffinity_np( ::pthread_self(), sizeof( cpuset), & cpuset) );
  19. if ( errno_ != 0)
  20. throw std::runtime_error("::pthread_setaffinity_np() failed");
  21. }
  22. #include <boost/config/abi_suffix.hpp>