fb11e647
vrobic
reseau statique a...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
/*
* Copyright (C) 2015 Hamburg University of Applied Sciences (HAW)
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief test mutex replacement header
*
* @author Raphael Hiesgen <raphael.hiesgen@haw-hamburg.de>
*
* @}
*/
#include <string>
#include <cstdio>
#include <cassert>
#include <system_error>
#include "riot/mutex.hpp"
#include "riot/chrono.hpp"
#include "riot/thread.hpp"
#include "riot/condition_variable.hpp"
using namespace std;
using namespace riot;
/* http://en.cppreference.com/w/cpp/thread/mutex */
int main() {
puts("\n************ C++ mutex test ***********");
puts("Lock and unlock ... ");
{
mutex m;
int resource = 0;
auto f = [&m, &resource] {
for (int i = 0; i < 3; ++i) {
m.lock();
++resource;
this_thread::sleep_for(chrono::milliseconds(100));
m.unlock();
}
};
assert(resource == 0);
auto start = std::chrono::system_clock::now();
thread t1(f);
thread t2(f);
t1.join();
t2.join();
assert(resource == 6);
auto duration = std::chrono::duration_cast
<chrono::milliseconds>(std::chrono::system_clock::now() - start);
assert(duration.count() >= 600);
}
puts("Done\n");
puts("Try_lock ...");
{
mutex m;
m.lock();
thread([&m] {
auto res = m.try_lock();
assert(res == false);
}).detach();
m.unlock();
}
{
mutex m;
thread([&m] {
auto res = m.try_lock();
assert(res == true);
m.unlock();
}).detach();
}
puts("Done\n");
puts("Bye, bye.");
puts("*****************************************\n");
return 0;
}
|