2个回答
展开全部
#ifndef THREAD_H_
#define THREAD_H_
#include <unistd.h>
#include <pthread.h>
class Runnable
{
public:
//运行实体
virtual void run() = 0;
};
//线程类
class Thread: public Runnable
{
private:
//线程初始化号
static int thread_init_number;
//当前线程初始化序号
int current_thread_init_number;
//线程体
Runnable *target;
//当前线程的线程ID
pthread_t tid;
//线程的状态
int thread_status;
//线程属性
pthread_attr_t attr;
//线咐唯弯程优先级
sched_param param;
//获取执行方法的指针
static void* run0(void* pVoid);
//内部执行方法
void* run1();
//获取线程序号
static int get_next_thread_num();
public:
//线程的状态-新建
static const int THREAD_STATUS_NEW = 0;
//线程的状态-正在运行
static const int THREAD_STATUS_RUNNING = 1;
//线程的状态-运行结束
static const int THREAD_STATUS_EXIT = -1;
//构造函数
Thread();
//构造函数
Thread(Runnable *target);
//析构
~Thread();
//线程的运行体
void run();
//开始执行线程
bool start();
//获取线程状态
int get_state();
//等待线程直至退出
void join();
//等待线程退出或者超时
void join(unsigned long millis_time);
//比较两个线程时候相同,通过current_thread_init_number判断
bool operator ==(const Thread* other_pthread);
//获取this线程ID
pthread_t get_thread_id();
//获取当前线程ID
static pthread_t get_current_thread_id();
//当前线程是否和某个线程相等,通过tid判断
static bool is_equals(Thread* iTarget);
//设置线程的类型:绑定/非绑山型定
void set_thread_scope(bool isSystem);
//获取线程的类型:绑定/非绑定
bool get_thread_scope();
//设置线程的优先级,1-99,其中99为实时,意外的为普通
void set_thread_priority(int priority);
//获取线程的优先级
int get_thread_priority();
};
int Thread::thread_init_number = 1;
inline int Thread::get_next_thread_num()
{
return thread_init_number++;
}
void* Thread::run0(void* pVoid)
{
Thread* p = (Thread*) pVoid;
p->run1();
return p;
}
void* Thread::run1()
{
thread_status = THREAD_STATUS_RUNNING;
tid = pthread_self();
run();
thread_status = THREAD_STATUS_EXIT;
tid = 0;
pthread_exit(NULL);
}
void Thread::run()
{
if (target != NULL)
{
(*target).run();
}
} 衡闷
Thread::Thread()
{
tid = 0;
thread_status = THREAD_STATUS_NEW;
current_thread_init_number = get_next_thread_num();
pthread_attr_init(&attr);
}
Thread::Thread(Runnable *iTarget)
{
target = iTarget;
tid = 0;
thread_status = THREAD_STATUS_NEW;
current_thread_init_number = get_next_thread_num();
pthread_attr_init(&attr);
}
Thread::~Thread()
{
pthread_attr_destroy(&attr);
}
bool Thread::start()
{
return pthread_create(&tid, &attr, run0, this);
}
inline pthread_t Thread::get_current_thread_id()
{
return pthread_self();
}
inline pthread_t Thread::get_thread_id()
{
return tid;
}
inline int Thread::get_state()
{
return thread_status;
}
void Thread::join()
{
if (tid > 0)
{
pthread_join(tid,NULL);
}
}
void Thread::join(unsigned long millis_time)
{
if (tid == 0)
{
return;
}
if (millis_time == 0)
{
join();
}
else
{
unsigned long k = 0;
while (thread_status != THREAD_STATUS_EXIT && k <= millis_time)
{
usleep(100);
k++;
}
}
}
bool Thread::operator ==(const Thread* other_pthread)
{
if(other_pthread==NULL)
{
return false;
}if(current_thread_init_number==(*other_pthread).current_thread_init_number)
{
return true;
}
return false;
}
bool Thread::is_equals(Thread* iTarget)
{
if (iTarget == NULL)
{
return false;
}
return pthread_self() == iTarget->tid;
}
void Thread::set_thread_scope(bool isSystem)
{
if (isSystem)
{
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
}
else
{
pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
}
}
void Thread::set_thread_priority(int priority)
{
pthread_attr_getschedparam(&attr,¶m);
param.__sched_priority = priority;
pthread_attr_setschedparam(&attr,¶m);
}
int Thread::get_thread_priority(){
pthread_attr_getschedparam(&attr,¶m);
return param.__sched_priority;
}
#endif /* THREAD_H_ */
展开全部
与c语言一样,使用线猛册举程库,pthread线程,例如
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
struct member
{
int num;
char *name;
};
//结构体后的分号勿漏
void *create(void *arg)
//有void* 型参数传入,不能直接void
{
struct member *temp;
temp=(struct member *)arg;
//结构体变量之间不能直接赋值,但可以通过指针赋地址
printf("member->枝碧num:%d\n",temp->num);
printf("member->name:%s\n",temp->name);
sleep(1);
return (void *)8;
//这个很有特色,返回一个指向void的数据类型的值,这个值作为后面的exit code
}
int main(int agrc,char* argv[])
{
pthread_t tidp;
struct member *b;
void* a;
b=(struct member *)malloc(sizeof(struct member));
//先分配内存空间撒~
b->num=1;
b->name="mlq";
//字符串赋值,其他好用简便的方法有: char *p = NULL; p = new char [256];
if((pthread_create(&tidp,NULL,create,(void*)b))==-1) /
//
void *
为“无类型指针”,void * 可以指向任何类型的数据
{
printf("create error!\n");
return 1;
}
if(pthread_join(tidp,&a))
//调用
pthread_join函数,等待线程结束再继续往下执行,要不然主姿猜进程和下面的线程并行执行
{
printf("thread is not exit...\n");
return -2;
}
printf("thread is exit ,code is %d\n",(int)a); //不知为啥这里是(int)a,,a不是指针来的么
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
struct member
{
int num;
char *name;
};
//结构体后的分号勿漏
void *create(void *arg)
//有void* 型参数传入,不能直接void
{
struct member *temp;
temp=(struct member *)arg;
//结构体变量之间不能直接赋值,但可以通过指针赋地址
printf("member->枝碧num:%d\n",temp->num);
printf("member->name:%s\n",temp->name);
sleep(1);
return (void *)8;
//这个很有特色,返回一个指向void的数据类型的值,这个值作为后面的exit code
}
int main(int agrc,char* argv[])
{
pthread_t tidp;
struct member *b;
void* a;
b=(struct member *)malloc(sizeof(struct member));
//先分配内存空间撒~
b->num=1;
b->name="mlq";
//字符串赋值,其他好用简便的方法有: char *p = NULL; p = new char [256];
if((pthread_create(&tidp,NULL,create,(void*)b))==-1) /
//
void *
为“无类型指针”,void * 可以指向任何类型的数据
{
printf("create error!\n");
return 1;
}
if(pthread_join(tidp,&a))
//调用
pthread_join函数,等待线程结束再继续往下执行,要不然主姿猜进程和下面的线程并行执行
{
printf("thread is not exit...\n");
return -2;
}
printf("thread is exit ,code is %d\n",(int)a); //不知为啥这里是(int)a,,a不是指针来的么
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询