QuantumLibrary
quantum_yielding_thread.h
1 /*
2 ** Copyright 2018 Bloomberg Finance L.P.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 #ifndef BLOOMBERG_QUANTUM_YIELDING_THREAD_H
17 #define BLOOMBERG_QUANTUM_YIELDING_THREAD_H
18 
19 #include <thread>
20 #include <chrono>
21 #include <quantum/quantum_thread_traits.h>
22 
23 namespace Bloomberg {
24 namespace quantum {
25 
26 //==============================================================================================
27 // struct YieldingThreadDuration
28 //==============================================================================================
32 template <class DURATION>
34 {
38  void operator()(DURATION time = defaultDuration())
39  {
40  if (time == DURATION(0)) {
41  //Busy wait
42  std::this_thread::yield();
43  }
44  else {
45  //Sleep wait
46  std::this_thread::sleep_for(time);
47  }
48  }
49 
50  static DURATION defaultDuration()
51  {
52  return std::chrono::duration_cast<DURATION>(ThreadTraits::yieldSleepIntervalMs()) +
53  std::chrono::duration_cast<DURATION>(ThreadTraits::yieldSleepIntervalUs());
54  }
55 };
56 
58 
59 }}
60 
61 #endif //BLOOMBERG_QUANTUM_YIELDING_THREAD_H
Definition: quantum_buffer_impl.h:22
static std::chrono::microseconds & yieldSleepIntervalUs()
Definition: quantum_thread_traits.h:42
static std::chrono::milliseconds & yieldSleepIntervalMs()
Dictates how long any thread should sleep on blocking calls when interacting with coroutines (e....
Definition: quantum_thread_traits.h:37
static DURATION defaultDuration()
Definition: quantum_yielding_thread.h:50
void operator()(DURATION time=defaultDuration())
Yields the current thread either via a busy wait loop or by sleeping it. Behavior is determined at co...
Definition: quantum_yielding_thread.h:38
This class provides the same functionality as a coroutine yield when called from a thread context.
Definition: quantum_yielding_thread.h:33