多线程
线程是轻量级的线程(LWP:light weight process)
线程是最小执行单位,进程是最小分配资源单位。一个进程可以有多个线程,一个进程可以理解为只有一个线程的进程。
每个线程都有自己独立的pcb,它们有独立的线程id和线程号,线程id是程序员使用,线程号是系统使用,同一个进程的线程,它们的进程id是一样的,共享进程空间。
可以使用命令 ps -Lf pid
来查看线程号。
不管是fork创建子进程还是pthreadcreate 创建线程,底层实现都是调用同一个内核函数clone。区别就是复制原始进程的地址空间,那么就会创建一个子进程。如果共享原始进程的地址空间,那么就会创建一个线程。系统内核是不区分进程和线程的,因为它们都有自己独立的pcb,只有在用户层面的概念上区分。因此可以看出线程相关的 pthread 系列函数是库函数而不是系统调用。
一、线程创建和回收
循环创建线程
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
void *threadMethod(void *num)
{
printf("this thread num is [%d], pid is [%d],tid is [%ld]\n", *(int *)num, getpid(), pthread_self());
sleep(30);
}
int main()
{
int arr[5];
pthread_t thread[5];
int i;
int ret;
for (i = 0; i < 5; i++)
{
arr[i] = i;
ret = pthread_create(&thread[i], NULL, threadMethod, &arr[i]);
if (ret != 0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
}
printf("main thread, pid is [%d], tid is [%ld]\n", getpid(), pthread_self());
sleep(30);
return 0;
}
多线程使用pthread_exit退出线程,并用pthread_join接收退出状态
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
int * nums;
void* thread_function(void* arg) {
// 线程执行的代码
int thread_id = *(int *)arg;
nums[thread_id] = thread_id + 5;
printf("Thread %d exiting,pid is [%d],tid is [%ld]\n", thread_id,getpid(),pthread_self());
pthread_exit(&nums[thread_id]);
}
int main() {
nums = (int *)malloc(sizeof(int)*5);
pthread_t threads[5];
int indexes[5] = {
1, 2, 3, 4, 5}; // 线程索引,用于打印
for(int i = 0; i < 5; i++) {
int ret = pthread_create(&threads[i], NULL, thread_function, &indexes[i]);
if (ret) {
printf("Thread creation error: %d\n", ret);
return -1;
}
}
void* exit_status;
for(int i = 0; i < 5; i++) {
int ret = pthread_join(threads[i], &exit_status);
if (ret) {
printf("Thread join error: %d\n", ret);
return -1;
}
printf("Thread %d exited with status: %d\n", i, *(int *)exit_status);
}
return 0;
}
//输出
Thread 1 exiting,pid is [1913],tid is [140703346046720]
Thread 2 exiting,pid is [1913],tid is [140703337654016]
Thread 3 exiting,pid is [1913],tid is [140703329261312]
Thread 4 exiting,pid is [1913],tid is [140703320868608]
Thread 5 exiting,pid is [1913],tid is [140703312475904]
Thread 0 exited with status: 6
Thread 1 exited with status: 7
Thread 2 exited with status: 8
Thread 3 exited with status: 9
Thread 4 exited with status: 10
二、线程属性
pthread_create函数中的线程属性参数设置步骤
//1.定义线程属性变量
pthread_attr_t attr;
//2.线程属性初始化,成功返回0,失败返回失败编号
int pthread_attr_init (pthread_attr_t* attr);
//3.线程属性变量添加分离属性,其中detachstate可以设置为
//PTHREAD_CREATE_DETACHED(分离)和PTHREAD_CREATE_JOINABLE(非分离)
//成功返回0,失败返回失败编号
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
//4.创建线程后释放变量,成功返回0,失败返回失败编号
int pthread_attr_destroy(pthread_attr_t *attr);
三、线程分离
使用pthread_detach函数实现线程分离,线程的退出后不需要手动去回收资源,设置线程分离有两种方法,一个是使用pthread_detach函数,另一个是在pthread_create函数中添加分离属性。
方式一:使用pthread_detach函数实现线程分离
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
void *threadMethod(void *num)
{
printf("this thread pid is [%d],tid is [%ld]\n", getpid(), pthread_self());
}
int main()
{
pthread_t thread;
int ret = pthread_create(&thread, NULL, threadMethod, NULL);
if (ret != 0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
ret = pthread_detach(thread);
if (ret != 0)
{
printf("pthread_detach error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid is [%d], tid is [%ld]\n", getpid(), pthread_self());
sleep(1);
return 0;
}
方式二:pthread_create函数中添加分离属性
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
void *threadMethod(void *num)
{
printf("this thread pid is [%d],tid is [%ld]\n", getpid(), pthread_self());
}
int main()
{
int ret;
pthread_attr_t attr;
ret = pthread_attr_init(&attr);
if (ret != 0)
{
printf("pthread_attr_init error, [%s]\n", strerror(ret));
return -1;
}
ret = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if (ret != 0)
{
printf("pthread_attr_setdetachstate error, [%s]\n", strerror(ret));
return -1;
}
pthread_t thread;
ret = pthread_create(&thread, &attr, threadMethod, NULL);
pthread_attr_destroy(&attr);
if (ret != 0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid is [%d], tid is [%ld]\n", getpid(), pthread_self());
sleep(1);
return 0;
}
//输出
main thread, pid is [2063], tid is [139936574068480]
this thread pid is [2063],tid is [139936565679872]