├── NewPrintf- implement ├── README.md └── main.cpp ├── README.md └── ThreadPool ├── README.md ├── Syncqueue.h ├── ThreadPool.h ├── main.cpp └── 活动图.png /NewPrintf- implement/README.md: -------------------------------------------------------------------------------- 1 | ## 利用可变参数模拟实现`printf` 2 | - 利用`stdarg`宏来解决可变参数问题 3 | - 头文件:`` 4 | ### 函数: 5 | - `va_list`; 6 | - `type va_arg( va_list arg_ptr, type );` 7 | - `void va_end( va_list arg_ptr );` 8 | - `void va_start( va_list arg_ptr, prev_param ); (ANSI version)` 9 | -------------------------------------------------------------------------------- /NewPrintf- implement/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author: LzyRapx 3 | * @Date: 2018-06-24 13:10 4 | * @Email: LzyRapx@gmail.com 5 | * @Last modified by: LzyRapx 6 | * @Last modified time: 2018-06-25 16:44 7 | * @Copyright: (C) 2018 LzyRapx 8 | */ 9 | 10 | // 利用可变参数模拟实现 printf 11 | 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int new_printf(char * str, ...) 18 | { 19 | va_list arg; 20 | int cnt= 0; 21 | char * str_tmp = NULL; 22 | va_start(arg,str); //初始化 23 | while(*str != '\0') 24 | { 25 | switch (*str) { 26 | case 'c': 27 | // 打印下一个参数的字符 28 | putchar(va_arg(arg,int)); 29 | cnt++; 30 | break; 31 | case 's': 32 | // 取下一个参数的地址 33 | str_tmp = (char*)va_arg(arg,int); 34 | while(*str_tmp != '\0') // 利用解引用进行输出 35 | { 36 | putchar(*str_tmp); 37 | cnt++; 38 | str_tmp++; 39 | } 40 | break; 41 | default: //如果不是'c'或's',就直接将它打印 42 | putchar(*str); 43 | cnt++; 44 | break; 45 | } 46 | str++; 47 | } 48 | //arg指向空,防止野指针 49 | va_end(arg); 50 | return cnt; 51 | } 52 | int main(int argc, char const *argv[]) { 53 | new_printf("s \nc c c\n","how cruel the world !", 'l', 'z', 'y'); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 1. ThreadPool : c++11实现半同步半异步线程池 2 | - `main.cpp`: 主函数+测试例子. 3 | - `hreadPool.h`:线程池的实现:“半同步半异步线程池”,非“领导者追随者线程池”. 4 | - `Syncqueue.h`: 同步阻塞队列的实现. 5 | 6 | ## 2. NewPrintf-implement:c++利用可变参数模拟实现`printf` 7 | - `main.cpp`:主函数+测试例子 8 | -------------------------------------------------------------------------------- /ThreadPool/README.md: -------------------------------------------------------------------------------- 1 | ### 语言 : 2 | - c++ 11 or above. 3 | ### 涉及的c++11 特性 : 4 | - std::mutex 5 | - std::thread 6 | - std::list 7 | - std::vector 8 | - std::condition_variable 9 | - std::queue 10 | - std::atomic 11 | ### 介绍: 12 | - 线程池技术通过在系统中预先创建一定数量的线程,当任务请求到来时从线程池中分配一个预先创建的线程去处理任务,线程在处理完任务之后可以重用,不会销毁,而是等待下次任务的到来。这样,通过线程池能避免大量的线程创建和销毁动作,从而节省系统资源。对于多核处理器,由于线程会被分配到多个CPU,会提高并行处理的效率。并且每个线程独立阻塞,可以防止主线程被阻塞而是主流程被阻塞,导致其他请求得不到响应。 13 | 14 | ### 半同步半异步活动图 15 | ![活动图](活动图.png) 16 | -------------------------------------------------------------------------------- /ThreadPool/Syncqueue.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzyrapx/CPlusPlus-Projects/eecb7b9c7244c97c2275627bc8f14f7f5bde7926/ThreadPool/Syncqueue.h -------------------------------------------------------------------------------- /ThreadPool/ThreadPool.h: -------------------------------------------------------------------------------- 1 | #ifndef ThreadPool_h 2 | 3 | #define ThreadPool_h 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // 互斥算法避免多个线程同时访问共享资源。这会避免数据竞争,并提供线程间的同步支持。 12 | #include //条件变量是允许多个线程相互交流的同步原语。它允许一定量的线程等待(可以定时)另一线程的提醒,然后再继续。条件变量始终关联到一个互斥。 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | //using namespace std; 20 | 21 | #include "Syncqueue.h" 22 | 23 | using namespace std; 24 | 25 | const int max_task = 100; // 最大任务数 26 | 27 | class ThreadPool 28 | { 29 | 30 | 31 | private: 32 | // 线程中的函数对象 33 | using Task = function; 34 | // 处理任务的线程池,用list保存 35 | list < shared_ptr > m_thread_pool; 36 | // 同步队列 37 | Syncqueue m_queue; 38 | // 是否停止的标志 39 | atomic_bool m_running; 40 | // call_once的参数 41 | once_flag m_flag; 42 | 43 | private: 44 | //线程池开始,预先创建包含numThreads 个线程的线程池 45 | void Start(int num_thread) 46 | { 47 | m_running = true; 48 | 49 | // 创建线程池 50 | for (int i = 0; i < num_thread; i++) { 51 | //智能指针管理,给出线程函数&ThreadPool::RunInThread 和对应参数this 52 | m_thread_pool.push_back(make_shared(&ThreadPool::RunInThread, this)); 53 | } 54 | } 55 | void RunInThread() 56 | { 57 | while (m_running == true) 58 | { 59 | listlist; 60 | m_queue.Out(list); 61 | for (auto & Task : list) 62 | { 63 | if (m_running == true) return; 64 | Task(); 65 | } 66 | } 67 | } 68 | //终止线程池,销毁池中的所有线程 69 | void stop_thread_pool() 70 | { 71 | m_queue.Stop(); // 让同步队列中的线程终止 72 | m_running = false; 73 | for (auto thread : m_thread_pool) 74 | { 75 | if (thread) thread->join(); 76 | } 77 | m_thread_pool.clear(); 78 | } 79 | public: 80 | //任务类型,这里是无参数无返回值,可以修改为任何类型的范型函数模板 81 | 82 | 83 | // hardware_concurrency CPU核数 当默认线程数 84 | ThreadPool(int num_thread = thread::hardware_concurrency()) : m_queue(max_task) 85 | { 86 | Start(num_thread); // 开始 87 | } 88 | // 析构函数 89 | ~ThreadPool() 90 | { 91 | Stop(); // 如果没有停止时,则主动终止线程池 92 | } 93 | 94 | //终止线程池,销毁池中所有线程 95 | void Stop() 96 | { 97 | //保证多线程情况下只调用一次stop_Thread_pool 98 | call_once(m_flag, [this] { stop_thread_pool(); }); 99 | } 100 | 101 | // 同步服务层:往同步队列中添加任务,两个版本 102 | // 同步服务层:往同步队列中添加任务,重载一下 103 | // 左值引用 104 | void add_task(const Task& task) { 105 | m_queue.add(task); 106 | } 107 | void add_task(Task&& task) { 108 | m_queue.add(forward(task)); 109 | } 110 | 111 | }; 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /ThreadPool/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzyrapx/CPlusPlus-Projects/eecb7b9c7244c97c2275627bc8f14f7f5bde7926/ThreadPool/main.cpp -------------------------------------------------------------------------------- /ThreadPool/活动图.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzyrapx/CPlusPlus-Projects/eecb7b9c7244c97c2275627bc8f14f7f5bde7926/ThreadPool/活动图.png --------------------------------------------------------------------------------