commit 8f77b5511b46d9ec8c105a412be702101421556f Author: impressionyang Date: Sun Jan 24 12:03:31 2021 +0800 first initial update diff --git a/README.md b/README.md new file mode 100644 index 0000000..7c228e1 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# linux cpp pthread package class + +it is use to make the thread create more easyer in linux c plus plus diff --git a/pthread_test/Makefile b/pthread_test/Makefile new file mode 100644 index 0000000..cdea1ee --- /dev/null +++ b/pthread_test/Makefile @@ -0,0 +1,14 @@ +object = main.o thread.o thread_test.o +run_target = thread_test +LFLAG = -lpthread + +main : $(object) + g++ -o $(run_target) $(object) $(LFLAG) + +main.o : main.cpp +thread.o : thread.h thread.cpp +thread_test.o: thread_test.h thread_test.cpp + +.PHONY : clean +clean: + rm $(object) \ No newline at end of file diff --git a/pthread_test/main.cpp b/pthread_test/main.cpp new file mode 100644 index 0000000..e9fa992 --- /dev/null +++ b/pthread_test/main.cpp @@ -0,0 +1,22 @@ +#include +#include "thread_test.h" + +using namespace std; + +int main(int argc, char **argv) +{ + Thread_Test test(10); + int ret = test.start(); + if (!ret) { + cout<<"thread add succeed!"<run(); + return 0; //线程的实体是run +} + +pthread_t Thread::get_thread_id() +{ + return this->pid; +} \ No newline at end of file diff --git a/pthread_test/thread.h b/pthread_test/thread.h new file mode 100644 index 0000000..25b2ffd --- /dev/null +++ b/pthread_test/thread.h @@ -0,0 +1,19 @@ +#ifndef _THREAD_H_ +#define _THREAD_H_ + +#include + +class Thread +{ +private: + pthread_t pid; + +private: + static void *start_thread(void *arg); //静态成员函数 +public: + int start(); + virtual void run() = 0; //基类中的虚函数要么实现,要么是纯虚函数(绝对不允许声明不实现,也不纯虚) + pthread_t get_thread_id(); +}; + +#endif // _THREAD_H_ \ No newline at end of file diff --git a/pthread_test/thread_test b/pthread_test/thread_test new file mode 100755 index 0000000..b5e3101 Binary files /dev/null and b/pthread_test/thread_test differ diff --git a/pthread_test/thread_test.cpp b/pthread_test/thread_test.cpp new file mode 100644 index 0000000..ad62a36 --- /dev/null +++ b/pthread_test/thread_test.cpp @@ -0,0 +1,31 @@ +#include "thread_test.h" + + +Thread_Test::Thread_Test(int cnt) +{ + this->cnt = cnt; + this->alive = true; +} + +void Thread_Test::run() +{ + while(this->alive) { + cout<<"test: "<alive<alive = false; +} + +int Thread_Test::get_cnt() +{ + return this->cnt; +} + +bool Thread_Test::get_running() +{ + return this->alive; +} \ No newline at end of file diff --git a/pthread_test/thread_test.h b/pthread_test/thread_test.h new file mode 100644 index 0000000..82da4ca --- /dev/null +++ b/pthread_test/thread_test.h @@ -0,0 +1,25 @@ +#ifndef _THREAD_TEST_CPP_ +#define _THREAD_TEST_CPP_ + +#include +#include +#include "thread.h" + +using namespace std; + +class Thread_Test: public Thread +{ +public: + Thread_Test(int cnt); + void stop(); + int get_cnt(); + bool get_running(); + +protected: + void run(); + +private: + int cnt; + bool alive; +}; +#endif // _THREAD_TEST_CPP_ \ No newline at end of file