
本文共 3105 字,大约阅读时间需要 10 分钟。
一、C++ 多线程
多线程是多任务处理的一种特殊形式,多任务处理允许让电脑同时运行两个或两个以上的程序。一般情况下,有两种类型的多任务处理:基于进程和基于线程。
基于进程的多任务处理是指多个程序的并发执行,而基于线程的多任务处理则是指同一程序的不同片段的并发执行。多线程程序包含可以同时运行的两个或多个部分,每个部分称为一个线程,每个线程定义了一个单独的执行路径。
本教程假设您使用的是 Linux 操作系统,我们要使用 POSIX 编写多线程 C++ 程序。POSIX Threads 或 Pthreads 提供的 API 可在多种类 Unix POSIX 系统上可用,比如 FreeBSD、NetBSD、GNU/Linux、Mac OS X 和 Solaris。
1.1 创建线程
下面的程序可以用来创建一个 POSIX 线程:
#include <pthread.h>int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine)(void *), void *arg);
在这里,pthread_create 创建一个新的线程,并让它可执行。参数说明如下:
- thread:指向线程标识符指针。
- attr:一个不透明的属性对象,可以用于设置线程属性。可以指定线程属性对象,也可以使用默认值 NULL。
- start_routine:线程运行函数起始地址,一旦线程被创建就会执行。
- arg:运行函数的参数。必须通过把引用作为指针强制转换为 void 类型进行传递。如果没有传递参数,则使用 NULL。
创建线程成功时,函数返回 0,若返回值不为 0 则说明创建线程失败。
1.2 终止线程
使用下面的程序可以终止一个 POSIX 线程:
#include <pthread.h>int pthread_exit(void *status);
在这里,pthread_exit 用于显式地退出一个线程。通常情况下,pthread_exit() 函数是在线程完成工作后无需继续存在时被调用。如果 main() 在它所创建的线程之前结束,并通过 pthread_exit() 退出,那么其他线程将继续执行。否则,它们将在 main() 结束时自动被终止。
1.3 向线程传递参数
以下简单实例代码展示了如何通过结构传递多个参数。您可以在线程回调中传递任意的数据类型,因为它指向 void,如下面的实例所示:
#include <pthread.h>#include
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) { td[i].thread_id = i; td[i].message = (char*)"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);
}
1.4 连接和分离线程
我们可以使用以下两个函数来连接或分离线程:
pthread_join(pthread_t *threadid, void **status)pthread_detach(pthread_t *threadid)
pthread_join() 函数阻碍调用程序,直到指定的 threadid 线程终止为止。只有创建时定义为可连接的线程(通过 pthread_attr_setdetachstate() 设置为 PTHREAD_CREATE_JOINABLE)才能被连接。如果线程创建时被定义为可分离的,则它永远也不能被连接。
以下简单实例代码展示了如何使用 pthread_join() 函数来等待线程的完成:
#include <pthread.h>#include
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;
pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);for (i = 0; i < NUM_THREADS; ++i) { rc = pthread_create(&threads[i], NULL, wait, (void *)&i); if (rc) { cout << "Error:unable to create thread," << rc << endl; exit(-1); }}pthread_attr_destroy(&attr);for (i = 0; i < NUM_THREADS; ++i) { rc = pthread_join(threads[i], &rc); if (rc) { cout << "Error:unable to join," << rc << endl; exit(-1); } cout << "Main: completed thread id :" << i; cout << " exiting with status :" << rc << endl;}cout << "Main: program exiting." << endl;pthread_exit(NULL);
}
发表评论
最新留言
关于作者
