C++ 中的多线程

原文:https://www.studytonight.com/cpp/multithreading-in-cpp.php

多线程意味着两个或多个线程同时运行,每个线程处理不同的任务。当你登录你的脸书个人资料时,在你的新闻提要上,你可以看到实时视频,你可以评论或点击一个喜欢的按钮,一切都同时进行。这是多线程的最好例子。多线程环境允许您同时运行许多活动;其中不同的线程负责不同的活动。

多线程有多种用途,其中一些是:

  • 更好的资源利用。
  • 更简单的程序设计。
  • 反应更灵敏的程序。

什么是线?

线程通常被称为轻量进程。每个线程执行程序的不同部分。每个线程共享内存、文件描述符和其他系统资源。在 Linux 中,所有线程函数都是在 < pthread.h > 头文件中声明的。但是它在标准的 C++ 库中不可用。


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

  1. pthread_create(): It creates a new thread. Below is the syntax:

    pthread_create(threadID, attr, start_routine, arg)
    

    在上面的代码中:

    线程号:是每个线程的唯一标识符。使用pthread_equal()功能比较线程的线程 ID。

    属性:可以用来设置各种线程属性的属性对象。它控制线程与程序其余部分的交互。

    start_routine :线程一旦创建就要执行的 C++ 例程。

    arg :单个参数必须作为 void 类型的指针通过引用传递。如果不传递参数,可以使用 null。

  2. pthread_exit():用于终止任意线程。

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

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

char* str = "Child thread";

void* func(void *str)
{
    cout << "Child thread Created: " << (char*)str;
}
// main function
int main()
{
    s = ctime(&Time);
    // Step 1: Declaring thread
    pthread_t t;    
    // Step 2: Calling create thread function
    pthread_create(&t, NULL, &func, (void*)str); 
    /*
        Syntax for pthread_create is:
        pthread_create(threadID,attr,start_routine,arg)
        Here,
        threadID = t, arg = (void*)str, atrr = Null, start_routine = func
    */
    cout << "Main thread created" << endl;
    pthread_join(t, NULL);
    //Exiting after completion
    exit(EXIT_SUCCESS); 
    return 0;
}

主线程已创建子线程已创建:子线程


连接和分离螺纹

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

join()功能

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

语法:

threadname.join();

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

语法:

/* 
    It checks whether a thread is joinable. 
    It returns bool value.
*/
threadname.joinable();

detach()功能

detach()函数从父线程中分离一个线程。它允许主线程和子线程独立执行。

语法:

threadname.detach();

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

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

#include <iostream>
#include <unistd.h>   // To include sleep function
#include<ctime>   // To get system time
#include <pthread.h>
using namespace std;

string s;
time_t Time = time(0);

void* func(void*)
{
    s = ctime(&Time);
    sleep(1);   //C alls sleep function
    cout << "Child thread Created " << s << endl;
}

// main function
int main()
{
    s = ctime(&Time);
    //Step 1: Declaring thread
    pthread_t t1[5];
    for(int i=0; i<5; i++)
    {
        cout << "Thread T[" << i << "] is Created " << s << endl;
        // Step 2: calling create thread function
        pthread_create(&t1[i], NULL, &func, NULL); 
        // Joining threads, main thread waits for child thread to complete
        pthread_join(t1[i], NULL); 
}
//Exiting after completion
exit(EXIT_SUCCESS); 
return 0;
}

线程 T[0]于 2017 年 11 月 1 日星期三 02:30:57 创建子线程于 2017 年 11 月 1 日星期三 02:30:57 创建线程 T[1]于 2017 年 11 月 1 日星期三 02:30:57 创建子线程于 2017 年 11 月 1 日星期三 02:30:57 创建线程 T[2]于 2017 年 11 月 1 日星期三 02:30:57 创建子线程 T[3]于 2017 年星期三创建


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

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

#include <iostream>
#include <unistd.h>   // To include sleep function
#include<ctime>   // To get system time
#include <pthread.h>
using namespace std;

string s;
time_t Time = time(0);

void* func(void*)
{
    s = ctime(&Time);
    sleep(1);   // Calls sleep function
    cout << "Child thread Created " << s << endl;
}

// main function
int main()
{
    s = ctime(&Time);
    // Step 1: Declaring thread
    pthread_t t1[5]; 
    for(int i=0; i<5; i++)
    {
        cout << "Thread T[" << i << "] is Created " << s << endl;
        // Step 2: Calling create thread function
        pthread_create(&t1[i], NULL, &func, NULL); 
        // Step 3: main thread doesn't waits for child thread to complete
        pthread_detach(t1[i]); 
}
// Exiting after completion
exit(EXIT_SUCCESS); 
return 0;
}

线程 T[0]于 2017 年 11 月 1 日星期三 02:38:14 创建线程 T[1]于 2017 年 11 月 1 日 02:38:14 创建线程 T[2]于 2017 年 11 月 1 日 02:38:14 创建线程 T[3]于 2017 年 11 月 1 日 02:38:14 创建线程 T[4]于 2017 年 11 月 1 日 02:38:14 创建

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