Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit 17e2eb7

Browse files
committed
RaceExample.cpp
1 parent 8a5bcbf commit 17e2eb7

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
| 27 | Fib.cpp | P Fib | 776 |
176176
| 27 | MatVec.cpp | Mat Vec | 785 |
177177
| 27 | MatVec.cpp | Mat Vec Main Loop | 785 |
178+
| 27 | RaceExample.cpp | Race Example | 788 |
178179

179180
# Supplementary Files
180181
* `utils.h`: Utils

RaceExample.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// algorithm - some algorithms in "Introduction to Algorithms", third edition
3+
// Copyright (C) 2018 lxylxy123456
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as
7+
// published by the Free Software Foundation, either version 3 of the
8+
// License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
19+
#ifndef MAIN
20+
#define MAIN
21+
#define MAIN_RaceExample
22+
#endif
23+
24+
#ifndef FUNC_RaceExample
25+
#define FUNC_RaceExample
26+
27+
#include <thread>
28+
#include "utils.h"
29+
30+
template <typename T>
31+
void RaceExampleLoop(T* x) {
32+
(*x) = (*x) + 1;
33+
}
34+
35+
template <typename T>
36+
T RaceExample() {
37+
T x = 0;
38+
std::thread t1(RaceExampleLoop<T>, &x);
39+
x = x + 1;
40+
t1.join();
41+
return x;
42+
}
43+
#endif
44+
45+
#ifdef MAIN_RaceExample
46+
int main(int argc, char *argv[]) {
47+
size_t n = get_argv(argc, argv, 1, 100);
48+
for (size_t i = 0; i < n; i++)
49+
std::cout << RaceExample<size_t>() << std::endl;
50+
return 0;
51+
}
52+
#endif
53+

0 commit comments

Comments
 (0)