多线程编程:C/C++如何使用线程函数?

2021年3月22日15:17:04 发表评论 662 次浏览

在一个Unix / Linux操作系统, C / C ++语言提供POSIX线程(pthread)所有线程相关功能的标准API(应用程序接口)。它允许我们为并发流程创建多个线程。它在多处理器或多核系统上最有效, 在多系统或多核系统中, 可以在内核级别上实现线程以实现执行速度。也可以通过利用IO或其他可能导致进程中断的系统功能中的延迟, 在单处理器系统中找到收益。

我们必须在脚本的开头包含pthread.h头文件, 才能使用pthreads库的所有功能。要执行c文件, 我们在编译文件时必须在命令行中使用-pthread或-lpthread。

cc -pthread file.c or
cc -lpthread file.c

函数在定义pthreads库包括:

pthread_create:

用于创建新线程

语法如下:

int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg);

参数:

  • 线:指向无符号整数值的指针, 该整数值返回创建的线程的线程ID。
  • 属性:指向用于定义线程属性(如分离状态, 调度策略, 堆栈地址等)的结构的指针。对于默认线程属性, 设置为NULL。
  • start_routine:指向线程执行的子例程的指针。子例程的返回类型和参数类型必须为void *。该函数具有单个属性, 但是如果需要将多个值传递给该函数, 则必须使用结构。
  • arg:指向包含先前参数中定义的函数的参数的void指针

pthread_exit:

用于终止线程

语法如下:

void pthread_exit(void *retval);

参数:此方法接受一个强制参数撤回这是指向一个整数的指针, 该整数存储终止的线程的返回状态。此变量的范围必须是全局的, 以便任何等待加入该线程的线程都可以读取返回状态。

pthread_join:

用于等待线程终止。

语法如下:

int pthread_join(pthread_t th, void **thread_return);

参数:此方法接受以下参数:

  • th:当前线程正在等待的线程的线程ID。
  • thread_return:指向th中提到的线程的退出状态存储位置的指针。

pthread_self:

用于获取当前线程的线程ID。

语法如下:

pthread_t pthread_self(void);

pthread_equal:

比较两个线程是否相同。如果两个线程相等, 则该函数返回一个非零值, 否则返回零。

语法如下:

int pthread_equal(pthread_t t1, pthread_t t2);

参数:此方法接受以下参数:

  • t1:第一个线程的线程ID
  • t2:第二个线程的线程ID

pthread_cancel:

用于向线程发送取消请求

语法如下:

int pthread_cancel(pthread_t thread);

参数:此方法接受一个强制参数线这是要向其发送取消请求的线程的线程ID。

pthread_detach:

用于分离线程。分离的线程不需要在终止时加入线程。如果线程是分离的, 则终止线程后, 线程的资源会自动释放。

语法如下:

int pthread_detach(pthread_t thread);

参数:此方法接受一个强制参数线这是必须分离的线程的线程ID。

例子:线程的简单实现如下:

// C program to show thread functions
  
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
  
void * func( void * arg)
{
     // detach the current thread
     // from the calling thread
     pthread_detach(pthread_self());
  
     printf ( "Inside the thread\n" );
  
     // exit the current thread
     pthread_exit(NULL);
}
  
void fun()
{
     pthread_t ptid;
  
     // Creating a new thread
     pthread_create(&ptid, NULL, &func, NULL);
     printf ( "This line may be printed"
            " before thread terminates\n" );
  
     // The following line terminates
     // the thread manually
     // pthread_cancel(ptid);
  
     // Compare the two threads created
     if (pthread_equal(ptid, pthread_self())
         printf ( "Threads are equal\n" );
     else
         printf ( "Threads are not equal\n" );
  
     // Waiting for the created thread to terminate
     pthread_join(ptid, NULL);
  
     printf ( "This line will be printed"
            " after thread ends\n" );
  
     pthread_exit(NULL);
}
  
// Driver code
int main()
{
     fun();
     return 0;
}

输出如下:

This line may be printed before thread terminates
Threads are not equal
Inside the thread
This line will be printed after thread ends

说明:在这里, 在代码中创建了两个执行线程。取决于先前处理的线程, 两个线程的输出线的顺序可以互换。主线程等待新创建的线程退出。因此, 仅在新线程退出后才输出输出的最后一行。通过不使用线程, 线程可以彼此独立终止。pthread_join功能。如果我们想手动终止新线程, 可以使用pthread_cancel去做吧。

注意:如果我们使用exit()代替pthread_exit()要结束一个线程, 即使其中一些线程可能仍在运行, 也会终止具有所有相关线程的整个过程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: