forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
threadExample.cpp
1 #include<thread>
2 #include "Misc.h"
3 
4 int lockCount = 0;
5 std::mutex clok;
6 
7 using namespace std;
8 using namespace forest;
9 
10 int f(int start) {
11  int i = start;
12  while (i < 10000) {
13  clok.lock();
14  if (lockCount != i) { clok.unlock(); continue; }
15  lockCount++; i += 2;
16  clok.unlock();
17  }
18 }
19 
20 int main() {
21  int startTime = Misc::getTime();
22 
23  std::thread t0{bind(f,0)};
24  std::thread t1{bind(f,1)};
25 
26  t0.join(); t1.join();
27  cout << Misc::getTime() - startTime;
28 }