Skip to content

Commit 91385d7

Browse files
committed
修正recv中断后counter无法下降的问题;添加新的示例
1 parent baf645e commit 91385d7

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

CMakeLists.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if(NOT MSVC)
1313
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
1414
endif()
1515

16-
if (MSVC AND LIBIPC_USE_STATIC_CRT)
16+
if (MSVC)
1717
set(CompilerFlags
1818
CMAKE_CXX_FLAGS
1919
CMAKE_CXX_FLAGS_DEBUG
@@ -22,9 +22,17 @@ if (MSVC AND LIBIPC_USE_STATIC_CRT)
2222
CMAKE_C_FLAGS_DEBUG
2323
CMAKE_C_FLAGS_RELEASE
2424
)
25-
foreach(CompilerFlag ${CompilerFlags})
26-
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
27-
endforeach()
25+
if (LIBIPC_USE_STATIC_CRT)
26+
foreach(CompilerFlag ${CompilerFlags})
27+
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
28+
string(REPLACE "/MDd" "/MTd" ${CompilerFlag} "${${CompilerFlag}}")
29+
endforeach()
30+
else()
31+
foreach(CompilerFlag ${CompilerFlags})
32+
string(REPLACE "/MT" "/MD" ${CompilerFlag} "${${CompilerFlag}}")
33+
string(REPLACE "/MTd" "/MDd" ${CompilerFlag} "${${CompilerFlag}}")
34+
endforeach()
35+
endif()
2836
endif()
2937

3038
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
@@ -50,6 +58,7 @@ endif()
5058
if (LIBIPC_BUILD_DEMOS)
5159
add_subdirectory(demo/chat)
5260
add_subdirectory(demo/msg_que)
61+
add_subdirectory(demo/send_recv)
5362
endif()
5463

5564
install(

demo/msg_que/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ int main(int argc, char ** argv) {
127127
::signal(SIGHUP , exit);
128128
#endif
129129

130-
if (std::string{ argv[1] } == mode_s__) {
130+
std::string mode {argv[1]};
131+
if (mode == mode_s__) {
131132
do_send();
132-
}
133-
else if (std::string{ argv[1] } == mode_r__) {
133+
} else if (mode == mode_r__) {
134134
do_recv();
135135
}
136136
return 0;

src/libipc/waiter_helper.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ struct waiter_helper {
3434
counter.waiting_.fetch_add(1, std::memory_order_release);
3535
flags.is_waiting_.store(true, std::memory_order_relaxed);
3636
auto finally = ipc::guard([&counter, &flags] {
37-
counter.waiting_.fetch_sub(1, std::memory_order_release);
37+
for (auto curr_wait = counter.waiting_.load(std::memory_order_acquire); curr_wait > 0;) {
38+
if (counter.waiting_.compare_exchange_weak(curr_wait, curr_wait - 1, std::memory_order_release)) {
39+
break;
40+
}
41+
}
3842
flags.is_waiting_.store(false, std::memory_order_relaxed);
3943
});
4044
{
@@ -107,6 +111,7 @@ struct waiter_helper {
107111
counter.counter_ -= 1;
108112
ret = ret && ctrl.handshake_wait(tm);
109113
} while (counter.counter_ > 0);
114+
counter.waiting_.store(0, std::memory_order_release);
110115
}
111116
return ret;
112117
}

0 commit comments

Comments
 (0)