C++ 多线程



C++中的多线程

多线程是指两个或多个线程同时运行,其中每个线程处理不同的任务,当你用视频软件播放视频的时候,你可以一边观看视频,一边提交评论,这就是多线程运行,多线程环境允许你同时运行许多任务,不同的线程负责不同的任务。



什么是线程?

线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。

线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源。



在 Linux(C++) 中创建线程

  1. pthread_create():创建一个新线程,下面是语法:
pthread_create(threadID, attr, start_routine, arg)

在上面的代码中:

  • threadID:是每个线程的唯一标识符,线程的 ThreadID 使用 pthread_equal() 函数进行比较。
  • attr:属性,可用于设置各种线程属性。
  • start_routine:线程创建后将执行的 C++ 例程。
  • arg:单个参数必须通过引用作为 void 类型的指针传递,如果不传递任何参数,则可以使用 null。
  1. pthread_exit():用于终止线程。

下面是一个在 C++ 中创建线程的简单程序:

#include <iostream>
#include <pthread.h>
using namespace std;

char* str = "Child thread";

void* func(void *str)
{
    cout << "创建子线程: " << (char*)str;
}

int main()
{
    s = ctime(&Time);
    // 第一步:声明线程
    pthread_t t;    
    // 第二步: 创建线程函数
    pthread_create(&t, NULL, &func, (void*)str); 
    /*
        创建线程的语法是:
        pthread_create(threadID,attr,start_routine,arg)
        在这里:
        threadID = t, arg = (void*)str, atrr = Null, start_routine = func
    */
    cout << "主线程被创建" << endl;
    pthread_join(t, NULL);
    // 退出程序
    exit(EXIT_SUCCESS); 
    return 0;
}

以上实例输出结果:

主线程被创建
创建子线程: Child thread


加入和分离线程

我们可以使用两种方法来加入或分离线程:

join() 函数

线程的加入是通过使用线程类的join() 函数来完成的,它使主线程和子线程相互依赖,主线程仅在子线程终止后终止,即主线程等待子线程完成执行。 语法:

threadname.join();

一旦所有功能完成,它就会返回。当一个线程被分配给另一个线程或调用 join()detach() 时,它是不可连接的。 语法:

/* 
    返回一个线程是否可加入. 
    返回值类型为bool值.
*/
threadname.joinable(); 

detach() 函数

detach() 函数将线程与父线程分离,它允许主线程和子线程独立执行。 语法:

threadname.detach();

使用 join() 方法的程序示例

让我们用一个简单的例子来演示使用 join() 函数来连接两个线程:

#include <iostream>
#include <unistd.h>   // 为了导入sleep函数
#include<ctime>   // 为了获得系统时间
#include <pthread.h>
using namespace std;

string s;
time_t Time = time(0);

void* func(void*)
{
    s = ctime(&Time);
    sleep(1);   //
    cout << "创建子线程 " << s << endl;
}

int main()
{
    s = ctime(&Time);
    //第一步:声明线程
    pthread_t t1[5];
    for(int i=0; i<5; i++)
    {
        cout << "Thread T[" << i << "] 被创建 " << s << endl;
        // 第二步:调用创建线程的函数
        pthread_create(&t1[i], NULL, &func, NULL); 
        // 加入线程,主函数等待子线程完成
        pthread_join(t1[i], NULL); 
}

exit(EXIT_SUCCESS); 
return 0;
}

以上实例输出结果:

Thread T[0] 被创建 Wed May 1 02:30:57 2022
创建子线程 Wed 1 02:30:57 2022]
Thread T[1] 被创建 Wed May 1 02:30:57 2022
创建子线程 Wed 1 02:30:57 2022
Thread T[2] 被创建 Wed May 1 02:30:57 2022
创建子线程 Wed 1 02:30:57 2022
Thread T[3] 被创建 Wed May 1 02:30:57 2022
创建子线程 Wed 1 02:30:57 2022
Thread T[4] 被创建 Wed May 1 02:30:57 2022
创建子线程 Wed 1 02:30:57 2022

使用 detach() 方法的程序示例

让我们用一个简单的例子来演示使用 detach() 函数来分离两个线程:

#include <iostream>
#include <unistd.h>   // 为了导入sleep函数
#include<ctime>   // 为了获得系统时间
#include <pthread.h>
using namespace std;

string s;
time_t Time = time(0);

void* func(void*)
{
    s = ctime(&Time);
    sleep(1);   
    cout << "创建子线程 " << s << endl;
}

// main function
int main()
{
    s = ctime(&Time);
    // 第一步:声明线程
    pthread_t t1[5]; 
    for(int i=0; i<5; i++)
    {
        cout << "Thread T[" << i << "] 被创建 " << s << endl;
        // 第二步:调用创建线程的函数
        pthread_create(&t1[i], NULL, &func, NULL); 
        // Step 3: 主函数不会等待子线程结束而结束
        pthread_detach(t1[i]); 
}

exit(EXIT_SUCCESS); 
return 0;
}

以上实例输出结果:

Thread T[0] 被创建 Wed May 1 02:38:14 2022
Thread T[1] 被创建 Wed May 1 02:38:14 2022
Thread T[2] 被创建 Wed May 1 02:38:14 2022
Thread T[3] 被创建 Wed May 1 02:38:14 2022
Thread T[4] 被创建 Wed May 1 02:38:14 2022

希望你已经了解 C++ 中线程创建的概念。