Gorgon Game Engine
Threading.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <functional>
6 #include <thread>
7 #include <vector>
8 
9 namespace Gorgon {
10 
13  namespace Threading {
14 
15 
19  inline void RunAsync(std::function<void()> fn) {
20  std::thread t(fn);
21  t.detach();
22  }
23 
40  inline void RunInParallel(std::function<void(int, int)> fn, unsigned threads=0) {
41  std::vector<std::thread> thrds;
42 
43  if(threads==0) threads=std::thread::hardware_concurrency();
44 
45  for(int id=0;id<threads;id++) {
46  thrds.emplace_back(fn, id, threads);
47  }
48 
49  for(int id=0;id<threads;id++) {
50  thrds[id].join();
51  }
52  }
53  }
54 
55 }
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Threading::RunAsync
void RunAsync(std::function< void()> fn)
Executes a function asynchronously.
Definition: Threading.h:19
Gorgon::Threading::RunInParallel
void RunInParallel(std::function< void(int, int)> fn, unsigned threads=0)
Runs a function specified amount of times in parallel.
Definition: Threading.h:40