14
14
#include < vector>
15
15
16
16
#include " mamba/core/error_handling.hpp"
17
+ #include " mamba/util/synchronized_value.hpp"
17
18
18
19
namespace mamba
19
20
{
@@ -71,10 +72,10 @@ namespace mamba
71
72
return ;
72
73
}
73
74
74
- std::scoped_lock lock{ threads_mutex } ;
75
+ auto synched_threads = threads. synchronize () ;
75
76
if (is_open) // Double check necessary for correctness
76
77
{
77
- threads. emplace_back (std::forward<Task>(task), std::forward<Args>(args)...);
78
+ synched_threads-> emplace_back (std::forward<Task>(task), std::forward<Args>(args)...);
78
79
}
79
80
}
80
81
@@ -92,10 +93,10 @@ namespace mamba
92
93
return ;
93
94
}
94
95
95
- std::scoped_lock lock{ threads_mutex } ;
96
+ auto synched_threads = threads. synchronize () ;
96
97
if (is_open) // Double check necessary for correctness
97
98
{
98
- threads. push_back (std::move (thread));
99
+ synched_threads-> push_back (std::move (thread));
99
100
}
100
101
}
101
102
@@ -116,12 +117,12 @@ namespace mamba
116
117
117
118
invoke_close_handlers ();
118
119
119
- std::scoped_lock lock{ threads_mutex } ;
120
- for (auto && t : threads )
120
+ auto synched_threads = threads. synchronize () ;
121
+ for (auto && t : *synched_threads )
121
122
{
122
123
t.join ();
123
124
}
124
- threads. clear ();
125
+ synched_threads-> clear ();
125
126
}
126
127
127
128
using on_close_handler = std::function<void ()>;
@@ -133,21 +134,18 @@ namespace mamba
133
134
return ;
134
135
}
135
136
136
- std::scoped_lock lock{ handlers_mutex } ;
137
+ auto handlers = close_handlers. synchronize () ;
137
138
if (is_open) // Double check needed to avoid adding new handles while closing.
138
139
{
139
- close_handlers. push_back (std::move (handler));
140
+ handlers-> push_back (std::move (handler));
140
141
}
141
142
}
142
143
143
144
private:
144
145
145
146
std::atomic<bool > is_open{ true };
146
- std::vector<std::thread> threads;
147
- std::recursive_mutex threads_mutex; // TODO: replace by synchronized_value once available
148
-
149
- std::vector<on_close_handler> close_handlers;
150
- std::recursive_mutex handlers_mutex; // TODO: replace by synchronized_value once available
147
+ util::synchronized_value<std::vector<std::thread>> threads;
148
+ util::synchronized_value<std::vector<on_close_handler>> close_handlers;
151
149
152
150
void invoke_close_handlers ();
153
151
};
0 commit comments