现在的位置: 首页 > 综合 > 正文

Linux多线程函数解析

2013年01月17日 ⁄ 综合 ⁄ 共 6035字 ⁄ 字号 评论关闭

Linux多线程函数用得比较多的是下面的3个
pthread_create(),pthread_exit(),pthread_join();它们都是在头文件<pthread.h>之中。编译时需要加静态库-lpthread

 

下面是函数的说明:
  pthread_create是UNIX环境创建线程函数
int pthread_create(
      pthread_t *restrict tidp,
      const pthread_attr_t *restrict_attr,
      void*(*start_rtn)(void*),
      void *restrict arg);
返回值
  
若成功则返回0,否则返回出错编号
  返回成功时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于制定各种不同的线程属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个万能指针参数arg,如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。
  linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread。
  由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取。对对象的存取都限定于基于由 restrict 修饰的指针表达式中。 由 restrict 修饰的指针主要用于函数形参,或指向由 malloc() 分配的内存空间。restrict 数据类型不改变程序的语义。 编译器能通过作出 restrict 修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。
参数
  第一个参数为指向线程标识符的指针。
  第二个参数用来设置线程属性。
  第三个参数是线程运行函数的起始地址。
  最后一个参数是运行函数的参数。
另外,在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库


pthread_exit(void* retval);
线程通过调用pthread_exit函数终止自身执行,就如同进程在结束时调用exit函数一样。这个函数的作用是,终止调用它的线程并返回一个指向某个对象的指针。该指针可以通过pthread_join(pthread_t tpid, void **value_ptr)中的第二个参数value_ptr获取到。

 

函数pthread_join用来等待一个线程的结束。函数原型为:
  extern int pthread_join __P (pthread_t __th, void **__thread_return);
第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,
它可以用来存储被等待线程退出时的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。如果执行成功,将返回0,如果失败则返回一个错误号。
所有线程都有一个线程号,也就是Thread ID。其类型为pthread_t。通过调用pthread_self()函数可以获得自身的线程号。

 


下面是一个简单的例子,子线程thread_fun会打出5次“this is thread_fun print!”然后调用pthread_exit退出,并返回一个指向字符串“this is thread return value!”的指针。在主函数里面调用pthread_join等待thread_fun线程结束,然后读取子线程的返回值到value中,再打印出来。
输出结果是:
pthread_create ok!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
pthread exit value: this is thread return value!

  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <stdlib.h>  
  4. #include <sys/types.h>  
  5. #include <errno.h>  
  6. #include <pthread.h>  
  7. //////////////////////////////////////////////////////  
  8. void *thread_fun(void *arg) {  
  9.     int i=0;  
  10.     char *value_ptr = "this is thread return value!\n";  
  11.     for (i=0; i<5; i++) {  
  12.         printf("this is thread_fun print!\n");  
  13.         sleep(1);  
  14.     }  
  15.     pthread_exit((void*)value_ptr);  
  16. }  
  17. //////////////////////////////////////////////////////  
  18. int main(int argc, char **argv) {  
  19.     pthread_t pid;  
  20.     int ret;  
  21.     void* value;  
  22.   
  23.     ret = pthread_create(&pid, NULL, thread_fun, NULL);  
  24.     if (ret) {  
  25.         printf("pthread_create failed!\nerrno:%d\n", errno);  
  26.         return -1;  
  27.     }  
  28.     printf("pthread_create ok!\n");  
  29.   
  30.     pthread_join(pid, &value);  
  31.     printf("pthread exit value: %s\n", value);  
  32.     return 0;  
  33. }  
  34.   
  35.   

二、线程的同步
当多个线程共享相同的内存的时候,必须保证所有线程看到的都是一致的数据视图,也就是必须保证对共享数据的原子操作,线程之间的同步有三种基本方式:
1、互斥量(mutex)
通过线程之间的互斥来保护数据,确保同一时间只会有一个线程来访问数据。互斥量实际上是一把锁,它不是用来锁数据的,而是锁互斥量本身,每个线程在访问共享数据之前都对这个互斥量进行加锁,加锁成功的话就可以独占地访问数据,如果加锁过程已经互斥量已经被锁住了,则它就必须等待直到其他线程释放这个互斥量,当有多个线程访问等待同一个互斥锁的时候,第一个变为运行状态的线程获得互斥锁,其他的则继续等待。
互斥量用pthread_mutex_t来表示,使用之前必须初始化,可以静态分配其为常量,也可以动态分配其值,如果是动态分配,使用完后必须释放。
#include<pthread.h>
pthread_mutex_t mlock = PTHREAD_MUTEX_INITIALIZER ; //静态分配常量
int pthread_mutex_init(pthread_mutex_t * restrict mutex,
                                            contst pthread_mutexattr_t * restrict attr) ;
int pthread_mutex_destroy(pthread_mutex_t * mutex) ;
动态初始化的后一个参数是用来设置mutex的属性,在这里我们使用默认属性NULL。具体属性可以去查spec(有递归锁等各种性质的互斥量)。
对互斥量进行加锁的过程中如果互斥量已经上锁,则调用线程就会阻塞直到互斥量被解锁:
#include<pthread.h>
int pthread_mutex_lock(pthread_mutex_t * mutex) ;
int pthread_mutex_trylock(pthread_mutex_t * mutex) ;
int pthread_mutex_unlock(pthread_mutex_t * mutex) ;
第一个是锁,锁不了就会等待;第二个是试图锁,如果锁成功则锁住,如果未成功不会等待并返回0;
使用互斥量必须注意防止死锁,比如说同一个线程试图对同一个互斥量加锁多次等等。可以同时使用多个互斥量来控制对数据的访问,但要注意如果锁的粒度太粗就会出现很多线程等待同一个锁,如果锁的粒度太细那么过多的锁开销也会影响性能。

2、读写锁
读写锁在我看到的代码中好像使用得不是很多,读写锁分为两种锁状态,一个是读状态加锁一个是写状态加锁。读状态加锁是共享访问,写状态加锁就是独占访问,一次可有多个线程加锁读状态模式,但是只有一个线程能加锁写状态模式。
如果读写锁是写加锁状态时,所有企图对读写锁进行加锁的操作都会阻塞;如果读写锁是读加锁状态时候,所有试图对读模式进行加锁的线程就能获得访问权,此时如果再来一个线程请求进行写模式加锁,那么一般就会阻塞以后的读模式请求,以防止写模式过长时间的等待。读写锁主要适用于那些读远远大于写的数据结构。
读写锁只有一种初始化方式,动态方式:
#include<pthread.h>
int pthread_rwlock_init(pthread_rwlock_t * restrict rwlock,
                                              const pthread_rwlockattr_t * restrict attr) ;
int pthread_rwlock_destroy(pthread_rwlock_t * rwlock) ;
加锁方式:
#include<pthread.h>
int pthread_rwlock_rdlock(pthread_rwlock_t * rwlock) ;
int pthread_rwlock_wrlock(pthread_rwlock_t * rwlock) ;
int pthread_rwlock_unlock(pthread_rwlock_t * rwlock) ;
int pthread_rwlock_tryrdlock(pthread_rwlock_t * rwlock) ;
int pthread_rwlock_trywrlock(pthread_rwlock_t * rwlock) ;

3、条件变量
条件变量和mutex一样也是用得比较多的一种同步机制。条件变量和互斥量一起使用时允许线程之间的竞争在特定的条件下发生。
条件变量和mutex类似也是两种初始化方式:
#include<pthread.h>
pthread_cond_t mlock = PTHREAD_COND_INITIALIZER ;
int pthread_cond_init(pthread_cond_t * restrict cond,
                                          pthread_condattr_t * restrict attr) ;
int pthread_cond_destroy(pthread_cond_t * cond) ;
这里的cond只是用来在等待和唤醒之间传递信号的一个变量,真正的条件不是它。条件本身是由互斥量进行保护的,在判断或者改变条件之前首先要锁住互斥量。
等待条件变量:
#include<pthread.h>
int pthread_cond_wait(pthread_cond_t * restrict cond ,
                                          pthread_mutex_t * restrict mutex) ;
int pthread_cond_timewait(pthread_cond_t * restrict cond,
                                                  pthread_mutex_t * restrict mutex ,
                                                  const struct timespec * restrict timeout) ;
一个等待一个超时等待,超时等待的时间不是相对时间而是一个绝对时间。
唤醒等待的线程:
#include<pthread.h>
int pthread_cond_singal(pthread_cond_t * cond) ;
int pthread_cond_broadcast(pthread_cond_t * cond) ;
第一个唤醒一个等待的线程(有多个等待的时候是否随机唤醒?),第二个唤醒所有等待的线程,但实际上只有一个会运行。
现在说明一下pthread_cond_wait的原理,这个函数首先会把当前进程放入等待队列,然后会把mutex解锁,这样就可以等待条件的改变,当这个函数返回的时候它会把mutex重新加锁,这也是为什么多个线程同时被唤醒时实际只有一个运行其他的会因为mutex加锁而继续等待。可以看下面的例子:
24 void * increace(void * arg){
 25     pthread_mutex_lock(&mutex) ;
 26     cout <<"before increace, NUM = "<< num<<endl ;
 27     if(num%2 != 0)
 28         pthread_cond_wait(&cond, &mutex) ;
 29     num++ ;
 30     cout <<"after increace, NUM = "<< num<<endl ;
 31     pthread_mutex_unlock(&mutex) ;
 32 }
 33 void * decreace(void * arg){
 34     pthread_mutex_lock(&mutex) ;
 35     cout <<"before decreace, NUM = "<< num<<endl ;
 36     sleep(3) ;
 37     num--;
 38     if(num%2 == 0)
 39         pthread_cond_signal(&cond) ;
 40     num-- ;
 41     cout <<"after decreace, NUM = "<< num<<endl ;
 42     pthread_mutex_unlock(&mutex) ;
 43}

抱歉!评论已关闭.