首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

多线程编程总结

2012-12-14 
多线程编程小结1.Linux进程与线程Linux进程创建一个新线程时,线程将拥有自己的栈(因为线程有自己的局部变

多线程编程小结

1.Linux进程与线程

Linux进程创建一个新线程时,线程将拥有自己的栈(因为线程有自己的局部变量),但与它的创建者共享全局变量、文件描述符、信号句柄和当前目录状态。

Linux通过fork创建子进程与创建线程之间是有区别的:fork创建出该进程的一份拷贝,这个新进程拥有自己的变量和自己的PID,它的时间调度是独立的,它的执行几乎完全独立于父进程。

进程可以看成一个资源的基本单位,而线程是程序调度的基本单位,一个进程内部的线程之间共享进程获得的时间片。


2._REENTRANT宏

在一个多线程程序里,默认情况下,只有一个errno变量供所有的线程共享。在一个线程准备获取刚才的错误代码时,该变量很容易被另一个线程中的函数调用所改变。类似的问题还存在于fputs之类的函数中,这些函数通常用一个单独的全局性区域来缓存输出数据。

为解决这个问题,需要使用可重入的例程。可重入代码可以被多次调用而仍然工作正常。编写的多线程程序,通过定义宏_REENTRANT来告诉编译器我们需要可重入功能,这个宏的定义必须出现于程序中的任何#include语句之前。

_REENTRANT为我们做三件事情,并且做的非常优雅:

(1)它会对部分函数重新定义它们的可安全重入的版本,这些函数名字一般不会发生改变,只是会在函数名后面添加_r字符串,如函数名gethostbyname变成gethostbyname_r。

(2)stdio.h中原来以宏的形式实现的一些函数将变成可安全重入函数。

(3)在error.h中定义的变量error现在将成为一个函数调用,它能够以一种安全的多线程方式来获取真正的errno的值。


3.线程的基本函数

大多数pthread_XXX系列的函数在失败时,并未遵循UNIX函数的惯例返回-1,这种情况在UNIX函数中属于一少部分。如果调用成功,则返回值是0,如果失败则返回错误代码。

1.线程创建:

#include <pthread.h>

int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

参数说明:

thread:指向pthread_create类型的指针,用于引用新创建的线程。

attr:用于设置线程的属性,一般不需要特殊的属性,所以可以简单地设置为NULL。

*(*start_routine)(void *):传递新线程所要执行的函数地址。

arg:新线程所要执行的函数的参数。

调用如果成功,则返回值是0,如果失败则返回错误代码。

2.线程终止

#include <pthread.h>

void pthread_exit(void *retval);

参数说明:

retval:返回指针,指向线程向要返回的某个对象。

线程通过调用pthread_exit函数终止执行,并返回一个指向某对象的指针。注意:绝不能用它返回一个指向局部变量的指针,因为线程调用该函数后,这个局部变量就不存在了,这将引起严重的程序漏洞。

3.线程同步

#include <pthread.h>

int pthread_join(pthread_t th, void **thread_return);

参数说明:

th:将要等待的张璐,线程通过pthread_create返回的标识符来指定。

thread_return:一个指针,指向另一个指针,而后者指向线程的返回值。

一个简单的多线程Demo(thread1.c):

#include <pthread.h>#include <stdio.h>#include <stdlib.h>#define NUM 6void *thread_function(void *arg);int main() {    int res;    pthread_t a_thread[NUM];    void *thread_result;    int index;    for (index = 0; index < NUM; ++index) {        res = pthread_create(&a_thread[index], NULL, thread_function, (void *) index);        if (res != 0) {            perror("Thread create failed!");            exit(EXIT_FAILURE);        }        sleep(1);    }    printf("Waiting for threads to finished...\n");    for (index = NUM - 1; index >= 0; --index) {        res = pthread_join(a_thread[index], &thread_result);        if (res == 0) {            printf("Picked up a thread:%d\n", index + 1);        }        else {            perror("pthread_join failed\n");        }    }    printf("All done\n");    exit(EXIT_SUCCESS);}void *thread_function(void *arg) {        int my_number = (int) arg;    int rand_num;    printf("thread_function is running. Argument was %d\n", my_number);    rand_num = 1 + (int) (9.0 * rand() / (RAND_MAX + 1.0));    sleep(rand_num);    printf("Bye from %d\n", my_number);    pthread_exit(NULL);} 

编译这个程序:

gcc -D_REENTRANT thread7.c -o thread7 –lpthread

运行这个程序:

$ ./thread7

thread_function is running. Argument was 0

thread_function is running. Argument was 1

thread_function is running. Argument was 2

thread_function is running. Argument was 3

thread_function is running. Argument was 4

Bye from 1

thread_function is running. Argument was 5

Waiting for threads to finished...

Bye from 5

Picked up a thread:6

Bye from 0

Bye from 2

Bye from 3

Bye from 4

Picked up a thread:5

Picked up a thread:4

Picked up a thread:3

Picked up a thread:2

Picked up a thread:1

All done


9.小结

本文主要介绍了Linux环境下的多线程编程,介绍了信号量和互斥量、线程属性控制、线程同步、线程终止、取消线程及多线程并发。

本文比较简单,只作为初学Linux多线程编程入门之用。


热点排行