#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <errno.h> /*ESRCH*/
#define MINI_STACK_SIZE (0x20000)
#define MINI_PRIORITY (66)
#define RET_OK (0)
#define RET_ERR (-1)
/*线程管理结构 -- ID与名字映射,方便从名字找到对应线程*/
#define THREAD_NAME_LEN (10)
typedef struct
{
char thread_name[THREAD_NAME_LEN];
pthread_t thread_id;
}THREAD_MGR_s;
typedef void (*THREAD_FUNC)(void *param);
/**********************************************************************************
局部函数声明
**********************************************************************************/
int add_thread(const char *name, const pthread_t id);
int thread_alive(const pthread_t thread_id);
int thread_destroy(pthread_t thread_id);
/**********************************************************************************
局部函数定义
*********************************************************************************/
/*
*创建线程,
*成功返回 > 0 值
*线程属性主要设置:
- 调度策略,
- 是否绑定,
- 是否分离,
- 堆栈大小,
- 线程优先级
*/
pthread_t thread_create(int stack_size, int priority, int detach, THREAD_FUNC func, const char *name)
{
pthread_t thread_id;
pthread_attr_t attr;
struct sched_param schedparam;
int ret;
/*线程初始化 -- 0为成功*/
ret = pthread_attr_init(&attr);
/* 设置调度策略 -- 0为成功
SCHED_OTHER 默认
SCHED_FIFO 先进先出
SCHED_RR 轮转
*/
ret |= pthread_attr_setschedpolicy(&attr, SCHED_RR);
/* 设置线程绑定状态 -- 0为成功
绑定 PTHREAD_SCOPE_SYSTEM 默认 -- 高效线程切换方式
非绑定 PTHREAD_SCOPE_PROCESS 无法设置
线程的调度是面向轻量级线程的,非绑定时,一个轻量线线程对应用户态下
多个线程;绑定状态下,一个用户线程绑定于一个轻量级线程,CPU切换线程
时无需寻找空闲轻量级线程绑定,直接切换,相对于非绑定线程,切换时间减
少
*/
ret |= pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/*设置线程是否分离 -- 0为成功
分离线程 PTHREAD_CREATE_DETACHED
- 如果创建分离线程 (PTHREAD_CREATE_DETACHED),则该线程一退出,便可重用
- 其线程 ID 和其他资源。
非分离线程 PTHREAD_CREATE_JOINABLE
- 非分离线程在终止后,必须要有一个线程用 join 来等待它。否则,不会释放
- 该线程的资源以供新线程使用,而这通常会导致内存泄漏。因此,如果不希望
- 线程被等待,请将该线程作为分离线程来创建。
- 需要调用pthread_join(pthread_t thread_id, (void *)ret);等待后线程退出
后的线程ID和其他资源才被释放(如:fork -- wait 相同)
*/
#if 1
if (0 != detach)
{
ret |= pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
}
else
{
ret |= pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
}
#endif
if (MINI_STACK_SIZE > stack_size) {
stack_size = MINI_STACK_SIZE;
}
/*设置线程堆栈大小 -- 0为成功*/
ret |= pthread_attr_setstacksize(&attr, stack_size);
/*设置线程优先级 -- 0为成功*/
schedparam.__sched_priority = priority + MINI_PRIORITY;
ret |= pthread_attr_setschedparam(&attr, &schedparam);
if (0 != ret)
{
printf("0 != ret\n");
goto LAB_ERR;
}
/*线程创建*/
ret = pthread_create(&thread_id, &attr, (void *)func, NULL);
if (0 != ret)
{
printf("pthread_create error\n");
goto LAB_ERR;
}
#if 0
if (0 != detach)
{
/*调用者调用pthread_join(pthread_id)后,如果该线程没有运行结束,调用者会被阻塞*/
ret |= pthread_detach(thread_id);
}
#endif
ret |= add_thread(name, thread_id);
if (0 != ret)
{
thread_destroy(thread_id);
goto LAB_ERR;
}
printf("thread create ok, id = [%lu]\n", (unsigned long int)thread_id);
LAB_OK:
return (thread_id);
LAB_ERR:
return (RET_ERR);
}
/*查看线程是否还存在*/
int thread_alive(const pthread_t thread_id)
{
int ret;
ret = pthread_kill(thread_id, 0);
if (ESRCH == ret)
{
printf("thread(%lu) not exit\n", thread_id);
goto LAB_ERR;
}
else if (EINVAL == ret)
{
printf("signal not legal\n");
goto LAB_ERR;
}
else
{
printf("thread alive\n");
}
return (RET_OK);
LAB_ERR:
return (RET_ERR);
}
int thread_destroy(pthread_t thread_id)
{
int ret;
/*
注意:
pthread_kill是向一个线程发送signal信号,如果线程不存在或不安装signal处理handle,
那这个signal将会转向进程进行处理,如果发送pthread_kill(thread_id, SIGKILL),线程
也没有安装signal处理handle,那么整个程序就会退出.发送pthread_kill(thread_id, 0),
signal = 0为保留值,只是用于测试线程是否退出.
0:成功
ESRCH:不存在或退出
EINVAL:信号不合法
*/
if (RET_OK == thread_alive(thread_id))
{
/*线程还在运行之中*/
printf("thread(%lu) is alive\n", thread_id);
/*终止线程运行,pthread_cancel函数的功能是给线程发送取消信号,使线程从取消点退出.
0:成功,成功并不代表thread会终止
*/
ret = pthread_cancel(thread_id);
if (0 == ret)
{
printf("send thread cancel ok\n");
}
else
{
printf("send thread cancel error\n");
goto LAB_ERR;
}
}
printf("thread destroy ok\n");
return RET_OK;
LAB_ERR:
return RET_ERR;
}
/*线程执行函数*/
static void thread_func(void *param)
{
int sleep_tm_count = 0;
printf("start func now!\n");
while (10 >= sleep_tm_count++)
{
sleep(1);
}
printf("thread fun exit\n");
}
int main(int argc, char *argv[])
{
const char thread_name[] = "one";
pthread_t thread_id;
unsigned char detach_flag = 0;
detach_flag = 1;
if (2 == argc) {
detach_flag = atoi(argv[0]);
}
thread_id = thread_create(0x20000, 10, detach_flag, (THREAD_FUNC)thread_func, thread_name);
if (RET_ERR == thread_id)
{
printf("thread create error\n");
}
else
{
printf("pthread join now\n");
pthread_join(thread_id, NULL);
printf("pthread join out\n");
}
#if 0
printf("go to sleep now\n");
sleep(-1);
#else
printf("exit process now\n");
#endif
return 0;
}
/***************************** 线程管理函数 start**********************************
*设计思想:
****************************** 线程管理函数 end **********************************/
/*添加新线程到线程管理结构*/
int add_thread(const char *name, const pthread_t id)
{
return (RET_OK);
LAB_ERR:
return (RET_ERR);
}
gcc ./test.c -lpthread -g -o test.elf
root@ubuntu:/home/libz/share/project/gdb-test# gdb ./test.elf -q
Reading symbols from /home/libz/share/project/gdb-test/test.elf...done.(gdb) set args 0
(gdb) r
Starting program: /home/libz/share/project/gdb-test/test.elf 0
[Thread debugging using libthread_db enabled]
[New Thread 0xb7feab70 (LWP 5174)]
thread create ok, id = [3086920560]
pthread join now
start func now!