<add>新增linux下线程池简单的启动
This commit is contained in:
		
							parent
							
								
									c2c4009ee1
								
							
						
					
					
						commit
						24ecfe29a0
					
				|  | @ -1,3 +1,6 @@ | ||||||
| /build | /build | ||||||
|  | /bin | ||||||
| *.so | *.so | ||||||
| *.exe | *.exe | ||||||
|  | /.vscode | ||||||
|  | /.cache | ||||||
|  | @ -1,8 +1,17 @@ | ||||||
| cmake_minimum_required(VERSION 3.16) | cmake_minimum_required(VERSION 3.16) | ||||||
| project(ProcessPool) | project(ProcessPool) | ||||||
|  | set(CMAKE_CXX_STANDARD 11) | ||||||
| set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin) | set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin) | ||||||
| 
 | 
 | ||||||
| add_library(${PROJECT_NAME} SHARED main.cpp) | file(GLOB SRC src/*.cpp) | ||||||
|  | 
 | ||||||
|  | if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") | ||||||
|  |     file(GLOB OSRC src/systemapi/unix/*.cpp) | ||||||
|  | else() | ||||||
|  |     file(GLOB OSRC src/systemapi/win/*.cpp) | ||||||
|  | endif() | ||||||
|  | 
 | ||||||
|  | add_library(${PROJECT_NAME} SHARED ${SRC} ${OSRC}) | ||||||
| 
 | 
 | ||||||
| target_include_directories(${PROJECT_NAME} PUBLIC  | target_include_directories(${PROJECT_NAME} PUBLIC  | ||||||
|     ${CMAKE_CURRENT_SOURCE_DIR}/include  |     ${CMAKE_CURRENT_SOURCE_DIR}/include  | ||||||
|  |  | ||||||
|  | @ -0,0 +1,13 @@ | ||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | #include "processpool.hpp" | ||||||
|  | #include "worker.hpp" | ||||||
|  | 
 | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | struct ProcessPool::systemapi { | ||||||
|  |      /* 子工作进程 */ | ||||||
|  |     std::vector<Worker> works; | ||||||
|  |     systemapi(int num); | ||||||
|  |     void start(const Worker&) const; | ||||||
|  | }; | ||||||
|  | @ -0,0 +1,12 @@ | ||||||
|  | #pragma once | ||||||
|  | #include "exportapi.hpp" | ||||||
|  | 
 | ||||||
|  | class Worker { | ||||||
|  |     /* 子进程的pid */ | ||||||
|  |     pid_t m_pid; | ||||||
|  |     bool m_busy; | ||||||
|  | 
 | ||||||
|  | public: | ||||||
|  |      | ||||||
|  |     void handle(pid_t pid) const; | ||||||
|  | }; | ||||||
|  | @ -0,0 +1,30 @@ | ||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | #if defined(_WIN32) || defined(_WIN64) | ||||||
|  |     #ifdef DYZEXPORT | ||||||
|  |         #define POOLAPI __declspec(dllexport) | ||||||
|  |     #else | ||||||
|  |         #define POOLAPI __declspec(dllimport) | ||||||
|  |     #endif | ||||||
|  | #else | ||||||
|  |     #define POOLAPI __attribute__((visibility("default"))) | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | #if defined(_WIN32) || defined(_WIN64) | ||||||
|  | 
 | ||||||
|  | #else | ||||||
|  | #include "sys/types.h" | ||||||
|  | #include "unistd.h" | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | #if defined(_WIN32) || defined(_WIN64) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | #else | ||||||
|  | using pid_t = pid_t; | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
|  | @ -0,0 +1,10 @@ | ||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | enum class Loglevel { | ||||||
|  |     OFF, DEBUG, INFO, WARN, ERROR | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | template <typename ...Args> | ||||||
|  | void log(Loglevel level, Args... arg) { | ||||||
|  |      | ||||||
|  | } | ||||||
|  | @ -0,0 +1,16 @@ | ||||||
|  | #pragma once | ||||||
|  | #include "exportapi.hpp" | ||||||
|  | 
 | ||||||
|  | #include <memory> | ||||||
|  | 
 | ||||||
|  | class POOLAPI ProcessPool { | ||||||
|  |     /* 进程数量 */ | ||||||
|  |     int m_num; | ||||||
|  |      | ||||||
|  |     struct systemapi; | ||||||
|  |     std::shared_ptr<systemapi> Impl; | ||||||
|  |      | ||||||
|  | public: | ||||||
|  |     ProcessPool(int num); | ||||||
|  |     void exec() const; | ||||||
|  | }; | ||||||
							
								
								
									
										6
									
								
								main.cpp
								
								
								
								
							
							
						
						
									
										6
									
								
								main.cpp
								
								
								
								
							|  | @ -1,6 +0,0 @@ | ||||||
| #include <iostream> |  | ||||||
| 
 |  | ||||||
| int main() { |  | ||||||
|     std::cout << "hello ProcessPoll" << '\n'; |  | ||||||
|     return 0; |  | ||||||
| } |  | ||||||
|  | @ -0,0 +1,17 @@ | ||||||
|  | #include "processpool.hpp" | ||||||
|  | #include "systempool.hpp" | ||||||
|  | 
 | ||||||
|  | ProcessPool::ProcessPool(int num):m_num(num) | ||||||
|  | { | ||||||
|  |     Impl = std::make_shared<systemapi>(m_num); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | void ProcessPool::exec() const { | ||||||
|  | 
 | ||||||
|  |     for (const auto& e : Impl->works)  | ||||||
|  |     { | ||||||
|  |         /* 创建子工作进程 */ | ||||||
|  |         Impl->start(e); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -0,0 +1,26 @@ | ||||||
|  | #include "processpool.hpp" | ||||||
|  | #include "systempool.hpp" | ||||||
|  | 
 | ||||||
|  | ProcessPool::systemapi::systemapi(int num) { | ||||||
|  |     for (int i = 0; i < num; ++i)  | ||||||
|  |     { | ||||||
|  |         Worker work; | ||||||
|  |         works.push_back(work); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /* 创建进程任务 */ | ||||||
|  | void ProcessPool::systemapi::start(const Worker& child) const { | ||||||
|  |     pid_t pid = fork(); | ||||||
|  |     if (pid > 0) { | ||||||
|  |         child.handle(pid); | ||||||
|  |     } | ||||||
|  |     else if (pid < 0) { | ||||||
|  |         /* 创建失败 */ | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  | 
 | ||||||
|  |         /* 父进程 */ | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  |   | ||||||
|  | @ -0,0 +1,12 @@ | ||||||
|  | #include "worker.hpp" | ||||||
|  | #include "iostream" | ||||||
|  | 
 | ||||||
|  | void Worker::handle(pid_t pid) const { | ||||||
|  |     for (int i = 0; i < 5; ++i)  | ||||||
|  |     { | ||||||
|  |         std::cerr << "子进程任务:" << pid << '\n'; | ||||||
|  |         sleep(1); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return; | ||||||
|  | } | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| cmake_minimum_required(VERSION 3.16) | cmake_minimum_required(VERSION 3.16) | ||||||
| 
 | set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../bin) | ||||||
| # Add test executable | # Add test executable | ||||||
| add_executable(${PROJECT_NAME}_test test.cpp) | add_executable(${PROJECT_NAME}_test test.cpp) | ||||||
| 
 | 
 | ||||||
|  | @ -7,3 +7,7 @@ add_executable(${PROJECT_NAME}_test test.cpp) | ||||||
| target_link_libraries(${PROJECT_NAME}_test PRIVATE  | target_link_libraries(${PROJECT_NAME}_test PRIVATE  | ||||||
|     ${PROJECT_NAME} |     ${PROJECT_NAME} | ||||||
| ) | ) | ||||||
|  | 
 | ||||||
|  | target_include_directories(${PROJECT_NAME}_test PUBLIC  | ||||||
|  |     ${CMAKE_CURRENT_SOURCE_DIR}/../include | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | @ -0,0 +1,9 @@ | ||||||
|  | #include "processpool.hpp" | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  | 
 | ||||||
|  |     ProcessPool pool(3); | ||||||
|  |     pool.exec(); | ||||||
|  | 
 | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue