【Linux操作系统】Linux系统编程中线程操作,pthread_exit、pthread_join、pthread_cancel、pthread_detach......

在现代计算机系统中,多线程编程已经成为一种常见的技术。多线程编程可以充分利用计算机的多核处理器,提高程序的并发性和性能。在Linux系统中,提供了一系列的线程操作函数,用于创建、管理和控制线程。本文将介绍Linux系统编程中常用的线程操作函数,包括pthread_exit、pthread_join、pthread_cancel和pthread_detach。通过学习这些函数的用法,我们可以更好地理解和掌握多线程编程的技巧,提高程序的效率和可靠性。

在这里插入图片描述


介绍

在Linux系统编程中,线程是一种轻量级的执行单元,与进程共享同一地址空间,可以并发地执行任务。线程操作是多线程编程中常用的一些函数,包括pthread_exitpthread_joinpthread_cancelpthread_detach等。本文将详细介绍这些函数的用法和特点。


1. pthread_exit函数

作用:

pthread_exit函数用于线程的退出,它可以在任何地方调用,使得线程提前结束执行。

原型:

void pthread_exit(void *retval);

参数和返回值:

其中,retval是线程的返回值,可以是任意类型的指针。线程通过调用pthread_exit()函数来终止自身的执行,并将retval作为返回值传递给父线程或者等待该线程的线程。

使用示例:

#include <stdio.h>
#include <pthread.h>

void *thread_func(void *arg) {
    
    
    int value = *(int *)arg;
    printf("Thread value: %d\n", value);
    pthread_exit(NULL);
}

int main() {
    
    
    pthread_t tid;
    int value = 10;
    pthread_create(&tid, NULL, thread_func, &value);
    pthread_join(tid, NULL);
    printf("Main thread exit\n");
    return 0;
}

在上述示例中,通过pthread_create函数创建一个新的线程,传递了一个整数值给线程函数thread_func。线程函数中打印了这个整数值,并通过pthread_exit函数退出线程。在主线程中,通过pthread_join函数等待子线程结束。


2. pthread_join函数

作用:

pthread_join()函数用于等待一个线程的结束,并获取线程的返回值。它可以阻塞调用线程,直到被等待的线程结束。

原型:

int pthread_join(pthread_t thread, void **retval);

参数:

  • thread: 要等待的线程的标识符。
  • retval: 一个指向指针的指针,用于接收线程的返回值。

返回值:

  • 成功时,返回0。
  • 失败时,返回错误码。

示例:

#include <stdio.h>
#include <pthread.h>

void *thread_func(void *arg) {
    
    
    int *value = (int *)arg;
    printf("Thread value: %d\n", *value);
    *value = *value * 2;
    pthread_exit(NULL);
}

int main() {
    
    
    pthread_t tid;
    int value = 10;
    pthread_create(&tid, NULL, thread_func, (void *)&value);
    pthread_join(tid, NULL);
    printf("Main value: %d\n", value);
    return 0;
}

在上述示例中,主线程创建了一个新的线程,并传递了一个整数值10作为参数。新线程的线程函数thread_func将打印接收到的参数值,并将其乘以2后退出。主线程调用pthread_join()函数等待新线程的结束,并获取其返回值。最后,主线程打印接收到的参数值,并输出乘以2后的值。

解释:

  • 主线程创建了一个新线程,并传递了一个整数值10作为参数。
  • 新线程的线程函数thread_func接收到参数值10,并打印出来。
  • 线程函数将参数值乘以2,并通过pthread_exit()函数退出。
  • 主线程调用pthread_join()函数等待新线程的结束。
  • 主线程获取新线程的返回值,但此处没有使用,因此传递了NULL。
  • 主线程打印接收到的参数值10,并输出乘以2后的值20。
  • 程序结束。

通过pthread_join()函数,主线程可以等待子线程的结束,并获取其返回值,从而实现线程间的同步和数据交换。


3. pthread_cancel函数

作用:

pthread_cancel()函数用于取消一个线程的执行。它向指定的线程发送一个取消请求,但并不保证线程会立即终止。

原型:

int pthread_cancel(pthread_t thread);

参数:

  • thread: 要取消的线程的标识符。

返回值:

  • 成功时,返回0。
  • 失败时,返回错误码。

示例:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *thread_func(void *arg) {
    
    
    int i;
    for (i = 0; i < 5; i++) {
    
    
        printf("Thread: %d\n", i);
        sleep(1);
    }
    pthread_exit(NULL);
}

int main() {
    
    
    pthread_t tid;
    pthread_create(&tid, NULL, thread_func, NULL);
    sleep(3);
    pthread_cancel(tid);
    pthread_join(tid, NULL);
    printf("Thread cancelled\n");
    return 0;
}

在上述示例中,主线程创建了一个新的线程,新线程的线程函数thread_func会每隔1秒打印一个数字,总共打印5次。主线程在等待3秒后,调用pthread_cancel()函数取消新线程的执行。然后,主线程调用pthread_join()等待新线程的结束,并打印出线程被取消的消息。

解释:

  • 主线程创建了一个新线程,并调用pthread_create()函数。
  • 新线程的线程函数thread_func会每隔1秒打印一个数字,总共打印5次。
  • 主线程等待3秒后,调用pthread_cancel()函数取消新线程的执行。
  • 主线程调用pthread_join()函数等待新线程的结束。
  • 新线程被取消后,主线程打印出线程被取消的消息。
  • 程序结束。

通过pthread_cancel()函数,可以向指定的线程发送取消请求,但并不保证线程会立即终止。被取消的线程需要在适当的地方检查取消请求,并根据需要进行清理操作。


4. pthread_detach函数

作用:

pthread_detach()函数用于将一个线程设置为分离状态,使其在终止时自动释放资源,无需其他线程调用pthread_join()函数来等待其结束。

原型:

int pthread_detach(pthread_t thread);

参数:

  • thread: 要设置为分离状态的线程的标识符。

返回值:

  • 成功时,返回0。
  • 失败时,返回错误码。

示例:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *thread_func(void *arg) {
    
    
    printf("Thread started\n");
    sleep(2);
    printf("Thread finished\n");
    pthread_exit(NULL);
}

int main() {
    
    
    pthread_t tid;
    pthread_create(&tid, NULL, thread_func, NULL);
    pthread_detach(tid);
    sleep(3);
    printf("Main thread finished\n");
    return 0;
}

在上述示例中,主线程创建了一个新的线程,并调用pthread_create()函数。新线程的线程函数thread_func会在开始时打印一条消息,然后睡眠2秒后再打印一条消息。主线程在创建新线程后,调用pthread_detach()函数将其设置为分离状态。然后,主线程睡眠3秒后打印一条消息,并结束。

解释:

  • 主线程创建了一个新线程,并调用pthread_create()函数。
  • 新线程的线程函数thread_func在开始时打印一条消息。
  • 主线程调用pthread_detach()函数将新线程设置为分离状态。
  • 新线程睡眠2秒后打印一条消息。
  • 主线程睡眠3秒后打印一条消息。
  • 程序结束。

通过pthread_detach()函数,可以将一个线程设置为分离状态,使其在终止时自动释放资源,无需其他线程调用pthread_join()函数来等待其结束。这对于不需要等待线程结束的情况非常有用。


总结

本文介绍了Linux系统编程中的线程操作函数,包括pthread_exitpthread_joinpthread_cancelpthread_detach。这些函数提供了对线程的控制和管理,能够实现线程的退出、等待、取消和分离等功能。熟练掌握这些函数的用法,对于多线程编程非常重要。

猜你喜欢

转载自blog.csdn.net/Goforyouqp/article/details/132393938