no message

master
zcy 2021-11-25 16:56:43 +08:00
parent 88ce90857a
commit 9a96adcd07
7 changed files with 211 additions and 45 deletions

View File

@ -4,10 +4,11 @@ add_definitions(-std=c++11)
set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_CXX_FLAGS " /MTd ")
set(CMAKE_CXX_FLAGS " /MDd ")
set(CMAKE_CXX_FLAGS_DEBUG "/DEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "")
message("current dir" ${CMAKE_CURRENT_SOURCE_DIR})
message(info ${SOURCE})
link_directories("./third/jsoncpp/lib/")
@ -16,11 +17,13 @@ link_directories("./third/gtest/lib")
link_libraries(generallib)
link_libraries(gtestd)
link_libraries(jsoncpp)
add_executable(cpp11 cpp11_test.cpp )
add_executable(gtest gtest.cpp )
add_executable(thread_test thread_usage.cpp threadpool.cpp)
include_directories("./third/jsoncpp/include/pkgsrc/include/json")
include_directories("./third/jsoncpp/include/json")
include_directories("../../../obj/inc/")
include_directories("./third/gtest/include")

View File

@ -1,7 +1,7 @@
/*
* @Author: your name
* @Date: 2021-03-15 23:07:25
* @LastEditTime: 2021-11-18 11:14:15
* @LastEditTime: 2021-11-23 11:29:15
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \cpp11\cpp11_test.cpp

35
test/src/cpp11/gtest.cpp Normal file
View File

@ -0,0 +1,35 @@
/*
* @Author: your name
* @Date: 2021-11-18 14:07:21
* @LastEditTime: 2021-11-18 14:43:21
* @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
*/
#include "gtest/gtest.h"
int Foo(int a,int b) {
if(a==0||b==0)
{
throw"don'tdothat";
}
int c = a % b;
if(c == 0)
return b;
return Foo(b,c);
}
TEST(FooTest,HandleZeroInput)
{
EXPECT_ANY_THROW(Foo(10,0));
EXPECT_THROW(Foo(0,5),char*);
}
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -7,7 +7,7 @@ void thread_set_promise(std::promise<int>& promiseObj) {
std::cout<<"end"<<std::endl;
}
void TestPromiseFutureBefore(){
void TestPromiseFutureBefore() {
std::promise<int> promiseObj;
std::future<int> futureObj = promiseObj.get_future();
std::thread t(&thread_set_promise, std::ref(promiseObj));
@ -21,8 +21,7 @@ void TestPromiseFutureBefore(){
t.join();
}
void independentThread()
{
void independentThread() {
std::cout << "Starting concurrent thread.\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Exiting concurrent thread.\n";
@ -56,7 +55,7 @@ void print_thread_id (int id) {
}
}
void TestLockGuard(){
void TestLockGuard() {
std::thread threads[10];
// spawn 10 threads:
for (int i=0; i<10; ++i)
@ -71,7 +70,7 @@ std::mutex cMtx; // 全局互斥锁.
std::condition_variable gCv; // 全局条件变量.
bool gReady = false; // 全局标志位.
void do_print_id(int id){
void do_print_id(int id) {
std::unique_lock <std::mutex> lck(cMtx);
while (!gReady)
gCv.wait(lck); // 当前线程被阻塞, 当全局标志位变为 true 之后,
@ -82,7 +81,6 @@ void do_print_id(int id){
void go(){
gReady = true;
gCv.notify_all(); // 唤醒所有线程.
}
int TestConditionVariable() {
@ -91,7 +89,6 @@ int TestConditionVariable() {
for (int i = 0; i < 10; ++i)
threads[i] = std::thread(do_print_id, i);
std::cout << "10 threads ready to race...\n";
go();
for (auto & th:threads)
@ -99,8 +96,7 @@ int TestConditionVariable() {
return 0;
}
class TestClass
{
class TestClass {
public:
TestClass() : num(new int(0))
{
@ -119,17 +115,18 @@ public:
private:
int *num;
};
TestClass get_demo()
{
TestClass get_demo() {
return TestClass();
}
int TestOptiomization()
{
int TestOptiomization() {
TestClass a = get_demo();
return 0;
}
int TestRValue()
{
int TestRValue() {
std::string str = "Hello";
std::vector<std::string> v;
//调用常规的拷贝构造函数,新建字符数组,拷贝数据
@ -143,8 +140,7 @@ int TestRValue()
}
int global = 0;
class TestTask : public general::Task
{
class TestTask : public general::Task {
public:
void Run()
{
@ -163,17 +159,22 @@ public:
reader.parse(strValue, root);
reader1.parse(strValue, root1);
reader2.parse(strValue, root2);
}
auto t2 = std::chrono::steady_clock::now();
double dr_ms = std::chrono::duration<double,std::milli>(t2-t1).count();
std::cout<<"expired is "<<dr_ms<<std::endl;
}
void OnDone(){
std::cout<<"the work is done"<<std::endl;
}
};
// 42973.6
// 42942.3
int TestThreadPool()
{
int TestThreadPool() {
//run code
TestTask *t = new TestTask;
general::ThreadPool pool(3);
@ -195,3 +196,102 @@ int TestThreadPool()
return 0;
}
atomic<int> a{0};
atomic<int> b{0};
void ValueSet(int)
{
a.store(0,memory_order_relaxed);
b.store(0,memory_order_relaxed);
for(int i = 0;i < 100;i++){
int t=1;
a.store(t,memory_order_relaxed);
b.store(2,memory_order_relaxed);
}
}
void Observer(int)
{
while(b.load(memory_order_relaxed)!=2);//自旋等待
cout<<a.load(memory_order_relaxed)<<endl;
cout<<"("<<a<<","<<b<<")"<<endl;//可能有多种输出
}
using namespace std;
std::atomic_llong total{ 0 };//原子数据类型
//int total{ 0 };
void func(int)
{
for (long long i = 0; i<1000000000LL; ++i)
{
total += 1;
}
}
void test_function(std::function<int(int)> t1) {
t1(1);
}
int TestFuntionParadim() {
return 0;
}
// 一个异步线程的封装
template<typename T>
class ASyncProcess {
public:
ASyncProcess(T t){
std::cout<<"ASyncProcess construct"<<std::endl;
mThread = new std::thread([&](){
this->Run();
this->Done(t);
});
}
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(){
mThread->detach();
}
private:
bool m_finish;
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;
}
};
int main()
{
TestFuntionParadim();
int j =111;
int &x = j;
TestProcess p1(x);
getchar();
std::cout<<x<<std::endl;
}

View File

@ -1,3 +1,11 @@
/*
* @Author: your name
* @Date: 2021-10-09 10:03:45
* @LastEditTime: 2021-11-25 16:43:43
* @LastEditors: Please set LastEditors
* @Description: koroFileHeader : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: \cpp11\thread_usage.h
*/
#pragma once
#include <cstdint>
@ -9,7 +17,19 @@
#include <cstdlib>
#include <vector>
#include "threadpool.h"
#include "third\jsoncpp\include\pkgsrc\include\json\json.h"
#include "json.h"
#include<atomic>
#include<thread>
#include<iostream>
#include <Windows.h>
#include <thread>
#include <atomic>
#include <iostream>
#include <objbase.h>
#include <functional>
#include <tuple>
using namespace std;
void TestPromiseFutureBefore();
void TestThreadDetach();
@ -18,3 +38,4 @@ int TestConditionVariable();
int TestOptiomization();
int TestRValue();
int TestThreadPool();
int TestFuntionParadim();

View File

@ -16,14 +16,13 @@ unsigned int CoreCount()
namespace general{
CThreadPool::CThreadPool(int num)
{
CThreadPool::CThreadPool(int num) {
this->mThreadCnt = num;
mStarted = false;
}
int CThreadPool::AddTask(Task *t){
int CThreadPool::AddTask(Task *t) {
if ( nullptr == t){
return -1;
}
@ -39,7 +38,7 @@ namespace general{
}
}
void CThreadPool::Start(){
void CThreadPool::Start() {
mStarted = true;
mThreads.reserve(this->mThreadCnt);
for (int i = 0; i < mThreadCnt; i++)
@ -48,7 +47,7 @@ namespace general{
}
}
Task *CThreadPool::PopTask(){
Task *CThreadPool::PopTask() {
std::unique_lock<std::mutex> lk(this->mMutex);
while (mTasks.empty() && mStarted)
{
@ -62,8 +61,7 @@ namespace general{
return ret;
}
void CThreadPool::Process(int id)
{
void CThreadPool::Process(int id) {
// std::cout << "thread id " << id << " started " << std::endl;
while (mStarted)
{
@ -75,12 +73,12 @@ namespace general{
else
{
task->Run();
task->OnDone();
}
}
}
void CThreadPool::Stop()
{
void CThreadPool::Stop() {
{
std::lock_guard<std::mutex> lk(mMutex);
mStarted = false;
@ -93,8 +91,7 @@ namespace general{
}
mStarted = false;
}
void CThreadPool::StopAll()
{
void CThreadPool::StopAll() {
{
while(this->mTasks.size() > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(100));

View File

@ -1,3 +1,11 @@
/*
* @Author: your name
* @Date: 2021-10-09 10:03:45
* @LastEditTime: 2021-11-25 15:26:02
* @LastEditors: Please set LastEditors
* @Description: koroFileHeader : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: \cpp11\threadpool.h
*/
#pragma once
#include <thread>
#include <mutex>
@ -22,13 +30,13 @@ extern "C"{
}
namespace general{
class CThreadPool;
enum PRIORITY
{
enum PRIORITY {
MIN = 1, NORMAL = 25, MAX = 50
};
typedef class CTask{
class Task{
public:
CTask() {
Task() {
}
void SetPriority(int priority) {
if (priority>(PRIORITY::MAX))
@ -41,9 +49,10 @@ namespace general{
}
}
virtual void Run() = 0;
virtual void OnDone() = 0;
protected:
int priority_;
}Task;
};
class Worker{
public:
@ -83,3 +92,4 @@ namespace general{
std::condition_variable mQueueAvaliableCondition;
}ThreadPool;
}