|
| 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