亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ 多线程

多线程是多任务的一种特殊形式,多任务是允许您的计算机同时运行两个或多个程序的功能。一般来说,多任务有两种类型:基于进程的和基于线程的。

基于进程的多任务处理程序的并发执行。基于线程的多任务处理处理同一程序的多个片段的并发执行。

多线程程序包含两个或多个可以同时运行的部分。这样一个程序的每个部分都称为一个线程,每个线程定义了一个单独的执行路径。

C++ 不包含对多线程应用程序的任何内置支持。相反,它完全依赖于操作系统来提供此功能。

本教程假设您在 Linux 操作系统上工作,我们将使用 POSIX 编写多线程 C++ 程序。 POSIX Threads 或 Pthreads 提供的 API 可用于许多类 Unix POSIX 系统,例如 FreeBSD、NetBSD、GNU/Linux、Mac OS X 和 Solaris。

创建线程

以下例程用于创建 POSIX 线程 -

#include <pthread.h>
pthread_create (thread, attr, start_routine, arg) 

这里,pthread_create 创建一个新线程并使其可执行。此例程可以在代码中的任何位置调用任意次数。这是参数的说明 -

Sr.No 参数及说明
1

线程

子例程返回的新线程的不透明唯一标识符。

2

属性

可用于设置线程属性的不透明属性对象。您可以指定一个线程属性对象,或者为默认值指定 NULL。

3

开始例程

线程创建后将执行的 C++ 例程。

4

参数

可以传递给 start_routine 的单个参数。它必须作为 void 类型的指针转​​换通过引用传递。如果不传递任何参数,则可以使用 NULL。

进程可以创建的最大线程数取决于实现。一旦创建,线程就是对等的,并且可以创建其他线程。线程之间没有隐含的层次结构或依赖关系。

终止线程

我们使用以下例程来终止 POSIX 线程 -

#include <pthread.h>
pthread_exit (status) 

这里 pthread_exit 用于显式退出线程。通常,pthread_exit() 例程在线程完成其工作并且不再需要存在之后调用。

如果 main() 在它创建的线程之前完成,并以 pthread_exit() 退出,其他线程将继续执行。否则,它们将在 main() 完成时自动终止。

示例

这个简单的示例代码使用 pthread_create() 例程创建了 5 个线程。每个线程打印一个“Hello World!”消息,然后调用 pthread_exit() 终止。

#include <iostream>
#include <cstdlib>
#include <pthread.h>

using namespace std;

#define NUM_THREADS 5

void *PrintHello(void *threadid) {
   long tid;
   tid = (long)threadid;
   cout << "Hello World! Thread ID, " << tid << endl;
   pthread_exit(NULL);
}

int main () {
   pthread_t threads[NUM_THREADS];
   int rc;
   int i;
   
   for( i = 0; i < NUM_THREADS; i++ ) {
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);
      
      if (rc) {
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

使用 -lpthread 库编译以下程序如下 -

$gcc test.cpp -lpthread

现在,执行您的程序,它会给出以下输出 -

main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Hello World! Thread ID, 0
Hello World! Thread ID, 1
Hello World! Thread ID, 2
Hello World! Thread ID, 3
Hello World! Thread ID, 4

将参数传递给线程

此示例显示如何通过结构传递多个参数。您可以在线程回调中传递任何数据类型,因为它指向 void,如下例所述 -

#include <iostream>
#include <cstdlib>
#include <pthread.h>

using namespace std;

#define NUM_THREADS 5

struct thread_data {
   int  thread_id;
   char *message;
};

void *PrintHello(void *threadarg) {
   struct thread_data *my_data;
   my_data = (struct thread_data *) threadarg;

   cout << "Thread ID : " << my_data->thread_id ;
   cout << " Message : " << my_data->message << endl;

   pthread_exit(NULL);
}

int main () {
   pthread_t threads[NUM_THREADS];
   struct thread_data td[NUM_THREADS];
   int rc;
   int i;

   for( i = 0; i < NUM_THREADS; i++ ) {
      cout <<"main() : creating thread, " << i << endl;
      td[i].thread_id = i;
      td[i].message = "This is message";
      rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&td[i]);
      
      if (rc) {
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

当上面的代码编译并执行时,它会产生以下结果 -

main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Thread ID : 3 Message : This is message
Thread ID : 2 Message : This is message
Thread ID : 0 Message : This is message
Thread ID : 1 Message : This is message
Thread ID : 4 Message : This is message

加入和分离线程

我们可以使用以下两个例程来加入或分离线程 -

pthread_join (threadid, status) 
pthread_detach (threadid) 

pthread_join() 子例程阻塞调用线程,直到指定的“threadid”线程终止。创建线程时,其属性之一定义它是可连接的还是可分离的。只有创建为可连接的线程才能连接。如果线程被创建为分离的,则永远无法加入。

此示例演示如何使用 Pthread 连接例程等待线程完成。

#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>

using namespace std;

#define NUM_THREADS 5

void *wait(void *t) {
   int i;
   long tid;

   tid = (long)t;

   sleep(1);
   cout << "Sleeping in thread " << endl;
   cout << "Thread with id : " << tid << "  ...exiting " << endl;
   pthread_exit(NULL);
}

int main () {
   int rc;
   int i;
   pthread_t threads[NUM_THREADS];
   pthread_attr_t attr;
   void *status;

   // Initialize and set thread joinable
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

   for( i = 0; i < NUM_THREADS; i++ ) {
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], &attr, wait, (void *)i );
      if (rc) {
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }

   // free attribute and wait for the other threads
   pthread_attr_destroy(&attr);
   for( i = 0; i < NUM_THREADS; i++ ) {
      rc = pthread_join(threads[i], &status);
      if (rc) {
         cout << "Error:unable to join," << rc << endl;
         exit(-1);
      }
      cout << "Main: completed thread id :" << i ;
      cout << "  exiting with status :" << status << endl;
   }

   cout << "Main: program exiting." << endl;
   pthread_exit(NULL);
}

当上面的代码编译并执行时,它会产生以下结果 -

main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Sleeping in thread
Thread with id : 0 .... exiting
Sleeping in thread
Thread with id : 1 .... exiting
Sleeping in thread
Thread with id : 2 .... exiting
Sleeping in thread
Thread with id : 3 .... exiting
Sleeping in thread
Thread with id : 4 .... exiting
Main: completed thread id :0  exiting with status :0
Main: completed thread id :1  exiting with status :0
Main: completed thread id :2  exiting with status :0
Main: completed thread id :3  exiting with status :0
Main: completed thread id :4  exiting with status :0
Main: program exiting.

C语言

  1. C++ 运算符
  2. C++ 注释
  3. C++ 类模板
  4. C++ 概述
  5. C++ 常量/文字
  6. C++ 中的运算符
  7. C++ 中的数字
  8. C++ 参考资料
  9. C++ 模板
  10. C++ 多线程
  11. C# - 多线程
  12. 正确标记机械图纸中的螺纹