项目作者: sslnjz

项目描述 :
C++ concurrency
高级语言: C++
项目地址: git://github.com/sslnjz/concurrency.git
创建时间: 2019-05-15T12:41:17Z
项目社区:https://github.com/sslnjz/concurrency

开源协议:GNU General Public License v3.0

下载


C++1x Multiple thread usage

thread_pool

A class which will create a pool of thread, you can determine how much threads to create or determined by the hardware performance itself. The thread_pool class used new c++1x features and make it easy to use.

  1. thread_pool pool(4);
  2. auto result = pool.thread_pool([](int process) { return process; }, 42);
  3. std::cout << result.get() << std::endl;

thread_safe_queue
A thread safe queue which used condition variables to try or wait to pop element of the queue.

thread_safe_stack

A thread safe stack which used condition variables to try or wait to pop element of the stack.

thread_sequence

Aim to solve the problem of multiple threads printing natural numbers in order.

  1. thread_sequence seq;
  2. seq.start(10, 3);

After running, the console will print like following:

  1. Thread 1: 1
  2. Thread 2: 2
  3. Thread 3: 3
  4. Thread 1: 4
  5. Thread 2: 5
  6. Thread 3: 6
  7. Thread 1: 7
  8. Thread 2: 8
  9. Thread 3: 9
  10. Thread 1: 10

high_resolution_timer
High resolution timer without errors.
```c++
auto tp = std::chrono::high_resolution_clock::now();
high_resolution_timer t;
t.setInterval(& {
printf(“Frame:%d\n”, std::chrono::duration_cast:milliseconds>(std::chrono::high_resolution_clock::now() - tp1).count());
tp = std::chrono::high_resolution_clock::now();
}, 10);

  1. t.sleep_for(1010);
  2. t.stop();
  1. >Console print:
  2. ```text
  3. ---100 ms
  4. Total: 1010 ms