修正recv中断后counter无法下降的问题;添加新的示例

This commit is contained in:
mutouyun 2021-09-17 22:01:34 +08:00
parent baf645eea1
commit 91385d727a
3 changed files with 22 additions and 8 deletions

View File

@ -13,7 +13,7 @@ if(NOT MSVC)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
endif() endif()
if (MSVC AND LIBIPC_USE_STATIC_CRT) if (MSVC)
set(CompilerFlags set(CompilerFlags
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG
@ -22,9 +22,17 @@ if (MSVC AND LIBIPC_USE_STATIC_CRT)
CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE
) )
if (LIBIPC_USE_STATIC_CRT)
foreach(CompilerFlag ${CompilerFlags}) foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}") string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
string(REPLACE "/MDd" "/MTd" ${CompilerFlag} "${${CompilerFlag}}")
endforeach() endforeach()
else()
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MT" "/MD" ${CompilerFlag} "${${CompilerFlag}}")
string(REPLACE "/MTd" "/MDd" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
endif()
endif() endif()
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
@ -50,6 +58,7 @@ endif()
if (LIBIPC_BUILD_DEMOS) if (LIBIPC_BUILD_DEMOS)
add_subdirectory(demo/chat) add_subdirectory(demo/chat)
add_subdirectory(demo/msg_que) add_subdirectory(demo/msg_que)
add_subdirectory(demo/send_recv)
endif() endif()
install( install(

View File

@ -127,10 +127,10 @@ int main(int argc, char ** argv) {
::signal(SIGHUP , exit); ::signal(SIGHUP , exit);
#endif #endif
if (std::string{ argv[1] } == mode_s__) { std::string mode {argv[1]};
if (mode == mode_s__) {
do_send(); do_send();
} } else if (mode == mode_r__) {
else if (std::string{ argv[1] } == mode_r__) {
do_recv(); do_recv();
} }
return 0; return 0;

View File

@ -34,7 +34,11 @@ struct waiter_helper {
counter.waiting_.fetch_add(1, std::memory_order_release); counter.waiting_.fetch_add(1, std::memory_order_release);
flags.is_waiting_.store(true, std::memory_order_relaxed); flags.is_waiting_.store(true, std::memory_order_relaxed);
auto finally = ipc::guard([&counter, &flags] { auto finally = ipc::guard([&counter, &flags] {
counter.waiting_.fetch_sub(1, std::memory_order_release); for (auto curr_wait = counter.waiting_.load(std::memory_order_acquire); curr_wait > 0;) {
if (counter.waiting_.compare_exchange_weak(curr_wait, curr_wait - 1, std::memory_order_release)) {
break;
}
}
flags.is_waiting_.store(false, std::memory_order_relaxed); flags.is_waiting_.store(false, std::memory_order_relaxed);
}); });
{ {
@ -107,6 +111,7 @@ struct waiter_helper {
counter.counter_ -= 1; counter.counter_ -= 1;
ret = ret && ctrl.handshake_wait(tm); ret = ret && ctrl.handshake_wait(tm);
} while (counter.counter_ > 0); } while (counter.counter_ > 0);
counter.waiting_.store(0, std::memory_order_release);
} }
return ret; return ret;
} }