线程相关概念
察看指定线程的LWP号
线程号和线程ID是有区别的
线程号是给内核看的
查看方式:
找到程序的进程ID
ps Lf pid
线程相关函数
创建线程 – pthread_create
int pthread_create(
pthread_t *thread, // 线程ID=无符号长整型
const pthread_attr_t *attr, // 线程属性, NULL
void *(*start_routine) (void *), // 线程处理函数
void *arg // 线程处理函数参数
);
参数:
thread: 传出参数, 线程创建成功之后,会被设置为一个合适的值
attr: 默认传NULL
start_routine: 子线程的处理函数
arg: 回调函数的参数
主线程先退出, 子线程会被强制结束,线程之间共享全局变量
返回值:
如果成功0,失败返回错误号
单个线程退出 – pthread_exit
exit(0);
函数原型: void pthread_exit(void *retval);
retval指针:必须指向全局,堆
阻塞等待线程退出, 获取线程退出状态 – pthread_join
int pthread_join(pthread_t thread, void **retval);
thread:要回收的子线程的线程id
retval:读取线程退出的时候携带的状态信息
eg.void* ptr;
pthread_join(pthid, &ptr);
指向的内存和pthread_exit参数指向同一块内存地址
测试源码
1、线程创建测试源码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
void* myfunc(void* arg)
{
printf("child pthread id: %lu\n", pthread_self());
return NULL;
}
int main(int argc, const char* argv[])
{
// 创建子线程
pthread_t thid;
// 返回错误号
int ret = pthread_create(&thid, NULL, myfunc, NULL);
if(ret != 0)
{
printf("error number: %d\n", ret);
// 根据错误号打印错误信息
printf("error information: %s\n", strerror(ret));
}
printf("parent pthread id: %lu\n", pthread_self());
sleep(1);
return 0;
}
测试结果
关键代码分析
pthread_create(&thid, NULL, myfunc, NULL);函数指针,回调函数。
2、线程退出测试源码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
void* myfunc(void* arg)
{
printf("child pthread id: %lu\n", pthread_self());
printf("child thread .....\n");
for(int i=0; i<5; ++i)
{
printf("child i = %d\n", i);
}
return NULL;
}
int main(int argc, const char* argv[])
{
// 创建子线程
pthread_t thid;
// 返回错误号
int ret = pthread_create(&thid, NULL, myfunc, NULL);
if(ret != 0)
{
printf("error number: %d\n", ret);
// 根据错误号打印错误信息
printf("error information: %s\n", strerror(ret));
}
printf("parent pthread id: %lu\n", pthread_self());
// 退出主线程,子线程不受影响
pthread_exit(NULL);
printf("parent thread .....\n");
for(int i=0; i<3; ++i)
{
printf("i = %d\n", i);
}
return 0;
}
测试结果
关键代码分析
pthread_exit(NULL);退出当前线程
3、线程阻塞退出测试源码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
int number = 100;
void* myfunc(void* arg)
{
printf("child pthread id: %lu\n", pthread_self());
printf("child thread .....\n");
for(int i=0; i<5; ++i)
{
printf("child i = %d\n", i);
}
return &number;
//pthread_exit(&number);
}
int main(int argc, const char* argv[])
{
// 创建子线程
pthread_t thid;
// 返回错误号
int ret = pthread_create(&thid, NULL, myfunc, NULL);
if(ret != 0)
{
printf("error number: %d\n", ret);
// 根据错误号打印错误信息
printf("error information: %s\n", strerror(ret));
}
printf("parent pthread id: %lu\n", pthread_self());
int *ptr;
pthread_join(thid, (void**)&ptr);
printf("++++++++++ number = %d\n", *ptr);
printf("parent thread .....\n");
for(int i=0; i<3; ++i)
{
printf("i = %d\n", i);
}
return 0;
}