Skip to content

Commit 959a6d6

Browse files
committed
replace std::jthread with std::thread
VS2019 では std::jthread::native_handle() が実装されていない。 microsoft/STL#1966 そのため、std::threadで代替する。
1 parent 5037dc0 commit 959a6d6

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

example/reader.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ int main()
1313
using std::this_thread::sleep_for;
1414
using namespace std::literals::chrono_literals;
1515

16-
//::SetPriorityClass(::GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
17-
//::SetThreadPriority(hMonitorThread, THREAD_PRIORITY_TIME_CRITICAL);
18-
1916
debugio::Monitor monitor;
20-
2117
monitor.start([](debugio::Buffer* buf) -> int {
2218
fprintf(stderr, "[READ] pid=%d msg=%s\n", buf->processID, buf->data);
2319
return 0;
2420
});
2521

22+
#ifdef _MSC_VER
23+
SetPriorityClass(::GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
24+
SetThreadPriority(monitor.native_handle(), THREAD_PRIORITY_TIME_CRITICAL);
25+
#else
26+
struct sched_param param = {
27+
.sched_priority = 99,
28+
};
29+
pthread_setschedparam(monitor.native_handle(), SCHED_FIFO, &param);
30+
#endif
31+
2632
sleep_for(2000ms);
2733
monitor.stop();
2834

example/writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int main()
3131
debugio::write_string(msg.c_str());
3232
fprintf(stderr, "[WRITE] %s\n", msg.c_str());
3333

34-
sleep_for(100ms);
34+
sleep_for(50ms);
3535
}
3636

3737
sleep_for(100ms);

include/debugio.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef DEBUGIO_H_
22
#define DEBUGIO_H_
33

4+
#include <atomic>
45
#include <functional>
56
#include <thread>
67

@@ -44,6 +45,7 @@ namespace debugio {
4445
shared_memory<Buffer> buffer;
4546
event buffer_ready;
4647
event data_ready;
48+
std::atomic<bool> stop_required;
4749

4850
int open();
4951
int close();
@@ -57,6 +59,7 @@ namespace debugio {
5759
: buffer(DATA_BUFFER_ID)
5860
, buffer_ready(EVENTS_BUFFER_READY_ID)
5961
, data_ready(EVENTS_DATA_READY_ID)
62+
, stop_required(false)
6063
{
6164
}
6265

@@ -103,14 +106,16 @@ namespace debugio {
103106
*/
104107
class Monitor : DebugIoBase {
105108
private:
106-
std::jthread monitor;
109+
std::thread monitor;
107110

108111
public:
109112
Monitor();
110113
virtual ~Monitor();
111114

112115
int start(std::function<int(Buffer*)> callback);
113116
int stop();
117+
118+
std::thread::native_handle_type native_handle();
114119
};
115120

116121
inline Monitor::Monitor()
@@ -134,9 +139,10 @@ namespace debugio {
134139
return -1;
135140
}
136141

137-
monitor = std::jthread([this, callback](std::stop_token st) mutable {
142+
stop_required.store(false);
143+
monitor = std::thread([this, callback]() mutable {
138144
this->buffer_ready.notify();
139-
while (!st.stop_requested()) {
145+
while (!this->stop_required.load()) {
140146
if (this->data_ready.wait(100) == 0) {
141147
callback(&buffer);
142148
this->buffer_ready.notify();
@@ -152,12 +158,17 @@ namespace debugio {
152158
return 0;
153159
}
154160

155-
monitor.request_stop();
161+
stop_required.store(true);
156162
monitor.join();
157163

158164
return close();
159165
}
160166

167+
inline std::thread::native_handle_type Monitor::native_handle()
168+
{
169+
return monitor.native_handle();
170+
}
171+
161172
/***************************************************************************
162173
* DebugIO Buffer Writer
163174
*/

0 commit comments

Comments
 (0)