多线程

推荐阅读:https://blog.csdn.net/sinat_22336563/article/details/71588703

多线程是多任务处理的一种特殊形式,多任务处理允许让电脑同时运行两个或两个以上的程序。一般情况下,两种类型的多任务处理:基于进程和基于线程

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

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

 Linux 操作系统,我们要使用 POSIX 编写多线程 C++ 程序。POSIX Threads 或 Pthreads 提供的 API 可在多种类 Unix POSIX 系统上可用,比如 FreeBSD、NetBSD、GNU/Linux、Mac OS X 和 Solaris。

#include <pthread.h>//创建线程需要的头文件
pthread_create (thread, attr, start_routine, arg) //创建线程的函数   thread 指向线程标识符指针。 attr 一个不透明的属性对象,可以被用来设置线程属性。您可以指定线程属性对象,也可以使用默认值 NULL。
start_routine 线程运行函数起始地址,一旦线程被创建就会执行 arg 运行函数的参数。它必须通过把引用作为指针强制转换为 void 类型进行传递。如果没有传递参数,则使用 NULL。

终止一个POXIS线程

#include <pthread.h>
pthread_exit (status) 

在这里,pthread_exit 用于显式地退出一个线程。通常情况下,pthread_exit() 函数是在线程完成工作后无需继续存在时被调用。

如果 main() 是在它所创建的线程之前结束,并通过 pthread_exit() 退出,那么其他线程将继续执行。否则,它们将在 main() 结束时自动被终止

 创建线程成功时,函数返回 0,若返回值不为 0 则说明创建线程失败。

 typedef unsigned long int pthread_t;
pthread_t用于声明线程ID。

sizeof (pthread_t) =4;

  

#include <iostream>
// 必须的头文件
#include <pthread.h>
 
using namespace std;
 
#define NUM_THREADS 5
 
// 线程的运行函数
void* say_hello(void* args)
{
    cout << "Hello Runoob!" << endl;
    return 0;
}
 
int main()
{
    // 定义线程的 id 变量,多个变量使用数组
    pthread_t tids[NUM_THREADS];
    for(int i = 0; i < NUM_THREADS; ++i)
    {
        //参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
        int ret = pthread_create(&tids[i], NULL, say_hello, NULL);
        if (ret != 0)
        {
           cout << "pthread_create error: error_code=" << ret << endl;
        }
    }
    //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
    pthread_exit(NULL);
}
$ g++ test.cpp -lpthread -o test.o

  传递多个参数

#include<iostream>
#include<pthread.h>
using namespace std;
#define NUM_THREADS 5
struct book{
int id;
char *p;
};
//线程运行函数
void* say_hello(void *args){
 struct book *mydata;
 mydata = (struct book *)args;
  cout<<"hello"<<mydata->p;
  return 0;
}


int main(){
 pthread_t tids[NUM_THREADS];
 struct book b[NUM_THREADS];
 for(int i=0;i<NUM_THREADS;i++){
  b[i].id=i;
  b[i].p=(char*)"sdfsd";
  int rst =  pthread_create(&tids[i],NULL,say_hello,(void*)(&b[i]));
  if(rst){
   cout<<"创建线程失败"<<rst<<endl;
  }
 }
 pthread_exit(NULL);
}

 连接和分离线程

我们可以使用以下两个函数来连接或分离线程:

pthread_join (threadid, status) 
pthread_detach (threadid) 
原文地址:https://www.cnblogs.com/webcyh/p/11307198.html