c语言 多线程(详)

什么是线程,线程的优点是什么

线程在Unix系统下,通常被称为轻量级的进程,线程虽然不是进程,但却可以看作是Unix进程的表亲,同一进程中的多条线程将共享该进程中的全部系统资源,如虚拟地址空间,文件描述符和信号处理等等。但同一进程中的多个线程有各自的调用栈(call stack),自己的寄存器环境(register context),自己的线程本地存储(thread-local storage)。 一个进程可以有很多线程,每条线程并行执行不同的任务。
线程可以提高应用程序在多核环境下处理诸如文件I/O或者socket I/O等会产生堵塞的情况的表现性能。在Unix系统中,一个进程包含很多东西,包括可执行程序以及一大堆的诸如文件描述符地址空间等资源。在很多情况下,完成相关任务的不同代码间需要交换数据。如果采用多进程的方式,那么通信就需要在用户空间和内核空间进行频繁的切换,开销很大。但是如果使用多线程的方式,因为可以使用共享的全局变量,所以线程间的通信(数据交换)变得非常高效。

创建线程 pthread_create
线程创建函数包含四个变量,分别为: 1. 一个线程变量名,被创建线程的标识 2. 线程的属性指针,缺省为NULL即可 3. 被创建线程的程序代码 4. 程序代码的参数

For example: - pthread_t thrd1; - pthread_attr_t attr; - void thread_function(void argument); - char *some_argument;

pthread_create(&thrd1, NULL, (void *)&thread_function, (void *) &some_argument)

结束线程 pthread_exit
线程结束调用实例:pthread_exit(void *retval); //retval用于存放线程结束的退出状态

线程等待 pthread_join
pthread_create调用成功以后,新线程和老线程谁先执行,谁后执行用户是不知道的,这一块取决与操作系统对线程的调度,如果我们需要等待指定线程结束,需要使用pthread_join函数,这个函数实际上类似与多进程编程中的waitpid。 举个例子,以下假设 A 线程调用 pthread_join 试图去操作B线程,该函数将A线程阻塞,直到B线程退出,当B线程退出以后,A线程会收集B线程的返回码。 该函数包含两个参数:

pthread_t th //th是要等待结束的线程的标识
void **thread_return //指针thread_return指向的位置存放的是终止线程的返回状态。

调用实例:pthread_join(thrd1, NULL);

简单例子

计算0-1000000的总和

    #include<iostream>
    #include<pthread.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<inttypes.h>
    using namespace std;
    int64_t end,now,start,sum;
     
    pthread_mutex_t now_t;
     
    void *add(void* arg){
        while(1){
        ┊   pthread_mutex_lock(&now_t);
        ┊   if(now>end){
        ┊   ┊   pthread_mutex_unlock(&now_t);
        ┊   }
        ┊   sum+=now;
        ┊   now++;
        ┊   pthread_mutex_unlock(&now_t);
        }
    }
     
     
    int main(int argc,char *argv[]){
        sum=0;
        now=0;
        if(argc != 3){
        ┊   printf("参数 error\n");
        ┊   return 1;
        }
        int num_of_thread=0;
        num_of_thread=atoi(argv[2]);
        ┊end=atoi(argv[1]);
        pthread_t tid[num_of_thread];
        for(int i = 0;i < num_of_thread;i++){
        ┊   pthread_create(&tid[i],NULL,add,NULL);
        }
        for(int i=0;i<num_of_thread;i++){
        ┊   pthread_join(tid[i],NULL);//等待线程结束
        }
        printf("sum = %lld\n",sum);
		return 0;
	}

猜你喜欢

转载自blog.csdn.net/qq_40574571/article/details/88074281