File: threadUtil.hh

package info (click to toggle)
last-align 1609-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 14,656 kB
  • sloc: cpp: 44,297; python: 5,192; ansic: 1,937; sh: 651; makefile: 457
file content (38 lines) | stat: -rw-r--r-- 778 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright 2016 Martin C. Frith

#ifndef THREAD_UTIL_HH
#define THREAD_UTIL_HH

#include <iostream>
#include <stdexcept>

#ifdef HAS_CXX_THREADS
#include <thread>
#endif

namespace cbrc {

inline unsigned decideNumberOfThreads(unsigned request,
				      const char *programName,
				      bool isVerbose) {
#ifdef HAS_CXX_THREADS
  if (request) return request;
  unsigned x = std::thread::hardware_concurrency();
  if (x) {
    if (isVerbose) std::cerr << programName << ": threads=" << x << '\n';
    return x;
  }
  std::cerr
    << programName
    << ": can't determine how many threads to use: falling back to 1 thread\n";
#else
  if (request != 1)
    throw
      std::runtime_error("I was installed here with multi-threading disabled");
#endif
  return 1;
}

}

#endif