first initial update
This commit is contained in:
commit
8f77b5511b
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# linux cpp pthread package class
|
||||||
|
|
||||||
|
it is use to make the thread create more easyer in linux c plus plus
|
||||||
14
pthread_test/Makefile
Normal file
14
pthread_test/Makefile
Normal file
@ -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)
|
||||||
22
pthread_test/main.cpp
Normal file
22
pthread_test/main.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#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!"<<endl;
|
||||||
|
}else {
|
||||||
|
cout<<"thread add faild!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(test.get_running()) {
|
||||||
|
if (20 <= test.get_cnt()) {
|
||||||
|
test.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
24
pthread_test/thread.cpp
Normal file
24
pthread_test/thread.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include "thread.h"
|
||||||
|
|
||||||
|
int Thread::start()
|
||||||
|
{
|
||||||
|
//创建一个线程(必须是全局函数)
|
||||||
|
if (pthread_create(&pid, NULL, start_thread, (void *)this) != 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// pthread_join(pid, NULL); //使用这个会导致线程不响应
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *Thread::start_thread(void *arg) //静态成员函数只能访问静态变量或静态函数,通过传递this指针进行调用
|
||||||
|
{
|
||||||
|
Thread *ptr = (Thread *)arg;
|
||||||
|
ptr->run();
|
||||||
|
return 0; //线程的实体是run
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_t Thread::get_thread_id()
|
||||||
|
{
|
||||||
|
return this->pid;
|
||||||
|
}
|
||||||
19
pthread_test/thread.h
Normal file
19
pthread_test/thread.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef _THREAD_H_
|
||||||
|
#define _THREAD_H_
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
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_
|
||||||
BIN
pthread_test/thread_test
Executable file
BIN
pthread_test/thread_test
Executable file
Binary file not shown.
31
pthread_test/thread_test.cpp
Normal file
31
pthread_test/thread_test.cpp
Normal file
@ -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: "<<cnt++<<" status: "<<this->alive<<endl;
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Thread_Test::stop()
|
||||||
|
{
|
||||||
|
this->alive = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Thread_Test::get_cnt()
|
||||||
|
{
|
||||||
|
return this->cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Thread_Test::get_running()
|
||||||
|
{
|
||||||
|
return this->alive;
|
||||||
|
}
|
||||||
25
pthread_test/thread_test.h
Normal file
25
pthread_test/thread_test.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef _THREAD_TEST_CPP_
|
||||||
|
#define _THREAD_TEST_CPP_
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <unistd.h>
|
||||||
|
#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_
|
||||||
Loading…
Reference in New Issue
Block a user