no message

master
zcy 2021-03-08 17:09:45 +08:00
parent f190a8d0c5
commit 5854d9bcc4
6 changed files with 116 additions and 9 deletions

View File

@ -8,7 +8,7 @@
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/cpp11.exe",
"program": "cpp11.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -1,5 +1,55 @@
{
"files.associations": {
"ostream": "cpp"
"ostream": "cpp",
"cstdint": "cpp",
"future": "cpp",
"thread": "cpp",
"iostream": "cpp",
"algorithm": "cpp",
"atomic": "cpp",
"chrono": "cpp",
"cmath": "cpp",
"condition_variable": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"resumable": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"list": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"ratio": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"xthread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"utility": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xhash": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xmemory0": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp",
"ctime": "cpp"
}
}

View File

@ -3,5 +3,6 @@ project(cpp11)
message("current dir" ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(cpp11 cpp11_test.cpp )
aux_source_directory(. SOURCE)
message(info ${SOURCE})
add_executable(cpp11 ${SOURCE} )

View File

@ -1,10 +1,14 @@
#include<iostream>
using namespace std;
#include<stdio.h>
#include "thread_usage.h"
int main(){
int x;
cout<<"hello world"<<endl;
printf("shit");
getchar();
std::cout<<"test start"<<endl;
// TestPromiseFutureBefore();
try{
TestThreadDetch();
}catch( std::exception e){
std::cout<<"exception"<<e.what();
}
}

View File

@ -0,0 +1,41 @@
#include "thread_usage.h"
void thread_set_promise(std::promise<int>& promiseObj) {
std::cout << "In a thread, making data. wait for 10 seconds"<<std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(800));
promiseObj.set_value(9);
std::cout<<"end"<<std::endl;
}
void TestPromiseFutureBefore(){
std::promise<int> promiseObj;
std::future<int> futureObj = promiseObj.get_future();
std::thread t(&thread_set_promise, std::ref(promiseObj));
while (std::future_status::timeout == futureObj.wait_for(std::chrono::milliseconds(1000))){
static uint32_t times = 0;
times ++;
std::cout<<"time out "<< times <<std::endl;
}
int ret = futureObj.get();
std::cout<<"get value "<<ret<<std::endl;
t.join();
}
void independentThread()
{
std::cout << "Starting concurrent thread.\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Exiting concurrent thread.\n";
}
void TestThreadDetch()
{
std::cout << "Starting thread caller.\n";
std::thread t(independentThread);
// t.detach();
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "Exiting thread caller.\n";
std::cout << "done.\n";
}

View File

@ -0,0 +1,11 @@
#include <thread>
#include <cstdint>
#include <iostream>
#include <functional>
#include <future>
#include <thread>
#include <chrono>
#include <cstdlib>
void TestPromiseFutureBefore();
void TestThreadDetch();