no message

master
zcy 2021-11-29 09:39:20 +08:00
parent 9a96adcd07
commit d425550735
4 changed files with 93 additions and 42 deletions

View File

@ -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 "")

View File

@ -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: <EFBFBD><EFBFBD>koroFileHeader<EFBFBD><EFBFBD><EFBFBD> 𥡝??: 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 <type_traits> // 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<T_ref(Args...)>
>::value
>::type* = nullptr
>
int test2(R& ret_param, Args... args)
{
static_assert( std::is_same<
R,
std::result_of_t<T_ref(Args...)>
>::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<T_ref(Args...)>
>::value
>::type* = nullptr
>
int test2(Args... args)
{
T* test_call = test;
test_call(args...);
return 0;
}
TEST(ResultOfTest,HandleZeroInput)
{
test2<decltype(test)>( 1 );
int result;
test2<decltype(foo)>( result );
}
int Foo(int a,int b) {
if(a==0||b==0)
{

View File

@ -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,13 +82,18 @@ 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();
@ -238,28 +242,21 @@ int TestFuntionParadim() {
return 0;
}
// 一个异步线程的封装
template<typename T>
template<class Function,class Ret,class ... Args>
class ASyncProcess {
public:
ASyncProcess(T t){
ASyncProcess(Function t,Ret &ret,Args... args){
std::cout<<"ASyncProcess construct"<<std::endl;
mThread = new std::thread([&](){
this->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()"<<std::endl;
};
virtual void Done(T t) {
std::cout<<"ASyncProcess::Done()"<<std::endl;
};
virtual ~ASyncProcess(){
virtual ~ASyncProcess() {
mThread->detach();
}
private:
@ -267,31 +264,35 @@ class ASyncProcess {
std::thread *mThread;
};
class TestProcess : public ASyncProcess<int>{
public:
TestProcess(int x )
:ASyncProcess<int>(x)
{
}
void Run(){
Sleep(5000);
std::cout<<"ASyncProcess\r\n";
}
void Done(int i){
std::cout<<"Done\r\n";
i = 4;
}
};
template<typename R, typename... Args>
int test2(R(&f)(Args...), R& ret_param, Args... args) {
ret_param = f(args...);
return 0;
}
template<typename... Args>
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<std::function<int(int)>,int,int>([](int p) -> int{
std::cout<<p;
return 100;
},result,22);
getchar();
std::cout<<x<<std::endl;
std::cout<<result;
}

View File

@ -1,7 +1,7 @@
/*
* @Author: your name
* @Date: 2021-10-09 10:03:45
* @LastEditTime: 2021-11-25 16:43:43
* @LastEditTime: 2021-11-26 10:40:22
* @LastEditors: Please set LastEditors
* @Description: koroFileHeader : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: \cpp11\thread_usage.h
@ -28,6 +28,8 @@
#include <objbase.h>
#include <functional>
#include <tuple>
#include <type_traits>
using namespace std;