Linux C 多线程编程

来源:互联网 发布:彗星dns优化器 绿色 编辑:程序博客网 时间:2024/04/29 06:35

简介

进程— 资源分配的最小单位
线程— 程序执行的最小单位

进程是一个程序的一个实例,拥有自己独立的各种段(数据段,代码段等等),每次创建一个进程需要从操作系统分配这些资源给他,消耗一定的时间,在linux下C语言创建一个进程使用fork()函数;
线程是一个轻量级的进程,除了自己少数的资源,不用用其他资源,且一个进程可以创建多个线程,这些线程共享进程的资源,创建线程的时间要比创建进程少很多,(几十分之一),从函数角度是使用clone()创建。
使用线程处理文件I/O或者socket处理都是非常有优势的,将一个大人物分解成若干个小任务,每个线程处理一个任务,线程之间切换不需要花很多时间,而且线程之间数据交换很方便,共享存储区。

C语言中使用多线程的函数

pthread_t在头文件/usr/include/bits/pthreadtypes.h中定义:
typedef unsigned long int pthread_t;
它是一个线程的标识符

创建线程

pthread_create(thread, attr, start_routine, arg);
extern int pthread_create __P ((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 则说明创建线程失败。
创建线程成功后,新创建的线程则运行参数三和参数四确定的函数,原来的线程则继续运行下一行代码。

结束线程

pthread_exit (status);
extern void pthread_exit P ((void *__retval)) __attribute ((noreturn));
pthread_exit 用于显式地退出一个线程
唯一的参数是函数的返回代码
通常情况下,pthread_exit() 函数是在线程完成工作后无需继续存在时被调用。
如果 main() 是在它所创建的线程之前结束,并通过 pthread_exit() 退出,那么其他线程将继续执行。否则,它们将在 main() 结束时自动被终止。

线程等待

pthread_join (threadid, status)
extern int pthread_join __P ((pthread_t __th, void **__thread_return));
第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。
这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。
最后要说明的是,一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。

实例

#include <pthread.h>#include <stdio.h>#define NUM_THREADS 5      //线程个数void *say_hello(void *args){    printf("Hello Runoob!\n");}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) {            printf("pthread_create error: error_code = %d\n", ret);        }    }    //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;    pthread_exit(NULL);}//g++ test.cpp -lpthread -o test
#include <pthread.h>#include <stdio.h>#include <stdlib.h>#define NUM_THREADS 5      //线程个数void *print_hello(void *threadid){    // 对传入的参数进行强制类型转换,由无类型指针变为整形数指针,然后再读取    int tid = *((int*)threadid);    printf("Hello Runoob! 线程 ID, %d\n", tid);    pthread_exit(NULL);}int main(){    pthread_t threads[NUM_THREADS];    int index[NUM_THREADS];    for (int i = 0; i < NUM_THREADS; ++i) {        printf("main() : 创建线程, %d\n", i);        index[i] = i;        int ret = pthread_create(&threads[i], NULL, print_hello, (void*)&(index[i]));        if (ret != 0) {            printf("pthread_create error: error_code = %d\n", ret);            exit(-1);        }    }    pthread_exit(NULL);}
//通过结构体传递多个参数。可以在线程回调中传递任意的数据类型,因为它指向 void#include <pthread.h>#include <stdio.h>#include <stdlib.h>#define NUM_THREADS 5      //线程个数struct thread_data{    int thread_id;    double message;};void *print_hello(void *threadarg){    struct thread_data *my_data =  (struct thread_data *) threadarg;    printf("Thread ID : %d\n", my_data->thread_id);    printf("Message : %f\n", my_data->message);    pthread_exit(NULL);}int main(){    //定义线程的 id 变量,多个变量使用数组    pthread_t threads[NUM_THREADS];    struct thread_data td[NUM_THREADS];    for (int i = 0; i < NUM_THREADS; ++i) {        printf("main() : creating thread, %d\n", i);        td[i].thread_id = i;        td[i].message = i;        int ret = pthread_create(&threads[i], NULL, print_hello, (void*)&(td[i]));        if (ret != 0) {            printf("pthread_create error: error_code = %d\n", ret);            exit(-1);        }    }    //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;    pthread_exit(NULL);}

输出结果

main() : creating thread, 0main() : creating thread, 1Thread ID : 0Message : 0.000000main() : creating thread, 2Thread ID : 1main() : creating thread, 3Message : 1.000000Thread ID : 2main() : creating thread, 4Message : 2.000000Thread ID : 3Thread ID : 4Message : 3.000000Message : 4.000000
原创粉丝点击