From d42555073584586daea8916b5d5f62128ae5b8e9 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Mon, 29 Nov 2021 09:39:20 +0800 Subject: [PATCH] no message --- test/src/cpp11/CMakeLists.txt | 5 +-- test/src/cpp11/gtest.cpp | 51 +++++++++++++++++++++- test/src/cpp11/thread_usage.cpp | 75 +++++++++++++++++---------------- test/src/cpp11/thread_usage.h | 4 +- 4 files changed, 93 insertions(+), 42 deletions(-) diff --git a/test/src/cpp11/CMakeLists.txt b/test/src/cpp11/CMakeLists.txt index f1ca2cf..bc4e00f 100644 --- a/test/src/cpp11/CMakeLists.txt +++ b/test/src/cpp11/CMakeLists.txt @@ -1,11 +1,10 @@ cmake_minimum_required(VERSION 3.12) project(cpp11) -add_definitions(-std=c++11) set(CMAKE_BUILD_TYPE DEBUG) -set(CMAKE_CXX_FLAGS " /MDd ") -set(CMAKE_CXX_FLAGS_DEBUG "/DEBUG") +set(CMAKE_CXX_FLAGS " /MTd /std:c++11 /EHsc") +set(CMAKE_CXX_FLAGS_DEBUG "/DEBUG /std:c++11 /EHsc") set(CMAKE_CXX_FLAGS_RELEASE "") diff --git a/test/src/cpp11/gtest.cpp b/test/src/cpp11/gtest.cpp index 7d2c4ac..c3a2d34 100644 --- a/test/src/cpp11/gtest.cpp +++ b/test/src/cpp11/gtest.cpp @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-11-18 14:07:21 - * @LastEditTime: 2021-11-18 14:43:21 + * @LastEditTime: 2021-11-26 11:27:50 * @LastEditors: Please set LastEditors * @Description: 打开koroFileHeader查看配置 进??置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @FilePath: \cpp11\gtest.cpp @@ -9,6 +9,55 @@ #include "gtest/gtest.h" +#include // std::(enable_if, result_of) + +void test(int) {} +int foo() { return 0; } + +template< + class T, class R, class... Args, + class T_ref = T&, + typename std::enable_if< + !std::is_void< + std::result_of_t + >::value + >::type* = nullptr + > +int test2(R& ret_param, Args... args) +{ + static_assert( std::is_same< + R, + std::result_of_t + >::value, "!" ); + T* test_call = foo; + ret_param = test_call(args...); + return 0; +} + +template< + class T, class... Args, + class T_ref = T&, + typename std::enable_if< + std::is_void< + std::result_of_t + >::value + >::type* = nullptr + > +int test2(Args... args) +{ + T* test_call = test; + test_call(args...); + return 0; +} + +TEST(ResultOfTest,HandleZeroInput) +{ + test2( 1 ); + + int result; + test2( result ); +} + int Foo(int a,int b) { if(a==0||b==0) { diff --git a/test/src/cpp11/thread_usage.cpp b/test/src/cpp11/thread_usage.cpp index f0b6438..22a2530 100644 --- a/test/src/cpp11/thread_usage.cpp +++ b/test/src/cpp11/thread_usage.cpp @@ -27,8 +27,7 @@ void independentThread() { std::cout << "Exiting concurrent thread.\n"; } -void TestThreadDetach() -{ +void TestThreadDetach() { std::cout << "Starting thread caller.\n"; std::thread t(independentThread); // t.detach(); @@ -83,14 +82,19 @@ void go(){ gCv.notify_all(); // 唤醒所有线程. } +void go_once(){ + gReady = true; + gCv.notify_one(); // 唤醒所有线程. +} + int TestConditionVariable() { std::thread threads[10]; // spawn 10 threads: for (int i = 0; i < 10; ++i) threads[i] = std::thread(do_print_id, i); std::cout << "10 threads ready to race...\n"; - go(); - + go_once(); + for (auto & th:threads) th.join(); return 0; @@ -238,28 +242,21 @@ int TestFuntionParadim() { return 0; } - // 一个异步线程的封装 -template +template class ASyncProcess { public: - ASyncProcess(T t){ + ASyncProcess(Function t,Ret &ret,Args... args){ std::cout<<"ASyncProcess construct"<Run(); - this->Done(t); + mThread = new std::thread([t,&ret,args...](){ + ret = t(args...); }); } bool Finish(){ return m_finish; } - virtual void Run() { - std::cout<<"ASyncProcess::Run()"<detach(); } private: @@ -267,31 +264,35 @@ class ASyncProcess { std::thread *mThread; }; -class TestProcess : public ASyncProcess{ -public: - TestProcess(int x ) - :ASyncProcess(x) - { - } - void Run(){ - Sleep(5000); - std::cout<<"ASyncProcess\r\n"; - } - void Done(int i){ - std::cout<<"Done\r\n"; - i = 4; - } -}; +template +int test2(R(&f)(Args...), R& ret_param, Args... args) { + ret_param = f(args...); + return 0; +} + +template +int test2(void(&f)(Args...), Args... args) +{ + f(args...); + return 0; +} + + +int ss(int p,int w){ + return 1; +} int main() { - TestFuntionParadim(); - int j =111; - int &x = j; - TestProcess p1(x); + TestConditionVariable(); + int result =200; + ASyncProcess,int,int>([](int p) -> int{ + std::cout< #include #include +#include + using namespace std;