-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbox.cpp
162 lines (122 loc) · 3.84 KB
/
mailbox.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <syncstream>
#include <assert.h>
#include "core/utils.h"
#include "mailbox.h"
class mbx_data
{
public:
mbx_data()
: _value(0)
{
std::osyncstream(std::cout) << "create mbx_data " << std::endl;
}
mbx_data( const mbx_data& md )
: _value(md._value)
{
std::osyncstream(std::cout) << "create mbx_data from copy constructor" << std::endl;
}
mbx_data( mbx_data&& md )
: _value(md._value)
{
std::osyncstream(std::cout) << "create mbx_data from move constructor" << std::endl;
}
~mbx_data()
{
std::osyncstream(std::cout) << "destroy mbx_data " << std::endl;
}
mbx_data& operator=( const mbx_data& md )
{
std::osyncstream(std::cout) << "assigned value by copy" << std::endl;
_value = md._value;
return *this;
}
mbx_data& operator=( mbx_data&& md )
{
std::osyncstream(std::cout) << "assigned value by move" << std::endl;
_value = md._value;
return *this;
}
void set_value( uint32_t value )
{ _value = value; }
uint32_t get_value() const
{ return _value; }
uint32_t _value;
};
using mailbox_type = lock_free::mailbox<mbx_data, core::ds_impl_t::lockfree, 0>;
//using mailbox_type = lock_free::mailbox<mbx_data, core::ds_impl_t::mutex, 0>;
void th_main_write( mailbox_type* mbx, [[maybe_unused]] uint32_t run_time_ms )
{
auto th_start_ms = core::utils::now<std::chrono::milliseconds>();
mbx_data md;
uint32_t cnt = 0;
while (true)
{
std::this_thread::sleep_for(500ms);
md.set_value( cnt++ );
mbx->write( md );
auto th_end_ms = core::utils::now<std::chrono::milliseconds>();
if (double(th_end_ms-th_start_ms) >= run_time_ms)
break;
}
}
void th_main_read ( mailbox_type* mbx, [[maybe_unused]] uint32_t run_time_ms )
{
auto th_start_ms = core::utils::now<std::chrono::milliseconds>();
mbx_data data;
while (true)
{
core::result_t res = mbx->read( data, 100 );
switch (res)
{
case core::result_t::eEmpty :
std::osyncstream(std::cout) << "core::result_t::eEmpty" << std::endl;
break;
case core::result_t::eSuccess :
std::osyncstream(std::cout) << "core::result_t::eSuccess - got [" << data.get_value() << "]" << std::endl;
break;
case core::result_t::eTimeout :
std::osyncstream(std::cout) << "core::result_t::eTimeout" << std::endl;
break;
case core::result_t::eNullPointer :
std::osyncstream(std::cout) << "core::result_t::eNullPointer" << std::endl;
break;
case core::result_t::eDoubleFree :
std::osyncstream(std::cout) << "core::result_t::eDoubleFree" << std::endl;
break;
default:
std::osyncstream(std::cout) << "? handled by default: ? [" << (uint32_t)res << "]" << std::endl;
break;
}
auto th_end_ms = core::utils::now<std::chrono::milliseconds>();
if (double(th_end_ms-th_start_ms) >= run_time_ms)
break;
}
}
int main()
{
core::node_t<mbx_data,false,true,false> nodo;
mailbox_type mbx( "test mbx" );
std::cout << "MBX NAME [" << mbx.name() << "]" << std::endl;
std::cout << " - Initial size = " << mbx.size() << std::endl;
std::cout << " - Is Empty() = " << (mbx.empty()?"true":"false") << std::endl;
uint32_t writers = 1;
uint32_t readers = 1;
uint32_t run_time_ms = 60000; // milliseconds
std::vector<std::thread> vec_writers;
std::vector<std::thread> vec_readers;
for ( uint32_t th_num = 0; th_num < writers; th_num++ )
{
vec_writers.emplace_back( std::thread( th_main_write, &mbx, run_time_ms ) );
}
for ( uint32_t th_num = 0; th_num < readers; th_num++ )
{
vec_readers.emplace_back( std::thread( th_main_read , &mbx, run_time_ms ) );
}
for ( auto& th : vec_writers )
th.join();
for ( auto& th : vec_readers )
th.join();
std::this_thread::sleep_for( 30s );
return 0;
}