码迷,mamicode.com
首页 > 编程语言 > 详细

unix环境高级编程---线程

时间:2015-07-01 18:32:58      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

一、线程概念

如果进程需要完成多个任务的时候,需要对其进行串行化操作。而如果其中一个任务(比如io操作),造成任务执行的挂起。则可以分解任务,将任务分开执行。

其中的每个任务就是所谓的线程。


线程包含了表示进程内执行环境必需的信息。

进程的所有信息对该进程的所有线程都是共享的。包括可执行的程序文本、程序的全局内存和堆内存、栈以及文件描述符。


二、线程创建

新增的线程可以通过pthread_create()函数来创建。

pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void*), void*restrict arg );

当pthread_create成功返回时,由tidp指向的内存单元被设置为新创建线程的线程ID,attr参数用于定制各种不同的线程
属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无类型指针参数arg,如果需要向start_rtn函数传递
的参数不止一个,需要把这些参数放到一个结构体中,然后作为arg参数传入。

在linux下(gcc -pthread -o 文件名 可执行文件名  )进行编译
线程创建的时候并不能保证哪个线程会先运行。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void thread(void)
{
 int i;
 sleep(1);
 for(i=0;i<10;i++)
 printf("This is a pthread.\n");
}

int main(void)
{
 pthread_t id;
 int i,ret;

 ret=pthread_create(&id,NULL,(void *) thread,NULL);
// sleep(1);
 if(ret!=0){
  printf ("Create pthread error!\n");
  exit (1);
 }
 for(i=0;i<3;i++)
  printf("This is the main process.\n");

// pthread_join(id,NULL);

 pthread_exit(NULL);

 return (0);
}

三、线程同步

当多个控制线程共享相同的内存的时候,需要确保每个线程看到一致的数据视图。

为了解决数据同步的问题,线程使用锁,也就是在同一时间只允许一个 线程访问该变量。

1、互斥量

我们可以通过使用pthread的互斥接口保护数据,确保同一时间只有一个线程访问数据。

互斥变量用pthread_mutex_t数据类型来表示。

对数据量加锁需要调用pthread_mutex_lock

对互斥量解锁,需要调用pthread_mutex_unlock

下面的代码没有经过加锁,应该是1和2连续输出的,但现在运行到一半时候可能被另一个线程打进来。

#include <stdio.h>
#include <pthread.h>
#include <malloc.h>
#include <string.h>

void* th_func(void* arg){
        int i;
        for(i=0; i<5; i++){
                printf("1\n");
                sleep(1);
                printf("2\n");
        }
}

int main(void){
        int ret;
        pthread_t tid1,tid2;

        ret = pthread_create(&tid1, NULL, th_func, NULL);
        if(ret != 0){
                printf("pthread_create:%s\n",strerror(ret));
                return -1;
        }


        ret = pthread_create(&tid2, NULL, th_func, NULL);
        if(ret != 0){
                printf("pthread_create:%s\n",strerror(ret));
                return -1;
        }

        sleep(15);
        return 0;
}



1
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
2

如果加上线程锁之后:

就避免了以上现象的发生。

  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <malloc.h>  
  4. #include <string.h>  
  5.   
  6. pthread_mutex_t *mutex;  
  7.   
  8. void* th_func(void* arg){  
  9.         int i;  
  10.         for(i=0; i<5; i++){  
  11.                 pthread_mutex_lock(mutex);  
  12.                 printf("1\n");  
  13.                 sleep(1);  
  14.                 printf("2\n");  
  15.                 pthread_mutex_unlock(mutex);  
  16.         }  
  17. }  
  18.   
  19. int main(void){  
  20.         int ret,result = 0;  
  21.         pthread_t tid1,tid2;  
  22.   
  23.         mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));  
  24.         if(mutex == NULL){  
  25.                 perror("malloc");  
  26.                 result = -1;  
  27.                 goto FINALLY;  
  28.         }  
  29.         pthread_mutex_init(mutex,NULL);  
  30.   
  31.         ret = pthread_create(&tid1, NULL, th_func, NULL);  
  32.         if(ret != 0){  
  33.                 printf("pthread_create:%s\n",strerror(ret));  
  34.                 result = -1;  
  35.                 goto FINALLY;  
  36.         }  
  37.         ret = pthread_detach(tid1);  
  38.         if(ret != 0){  
  39.                 printf("pthread_detach:%s\n",strerror(ret));  
  40.                 result = -1;  
  41.                 goto FINALLY;  
  42.         }  
  43.   
  44.         ret = pthread_create(&tid2, NULL, th_func, NULL);  
  45.         if(ret != 0){  
  46.                 printf("pthread_create:%s\n",strerror(ret));  
  47.                 result = -1;  
  48.                 goto FINALLY;  
  49.         }  
  50.         ret = pthread_detach(tid2);  
  51.         if(ret != 0){  
  52.                 printf("pthread_detach:%s\n",strerror(ret));  
  53.                 result = -1;  
  54.                 goto FINALLY;  
  55.         }  
  56.   
  57.         sleep(15);  
  58.   
  59. FINALLY:  
  60.   
  61.         if(mutex != NULL){  
  62.                 pthread_mutex_destroy(mutex);  
  63.                 free(mutex);  
  64.         }  
  65.         return result;  
  66. }  



版权声明:本文为博主原创文章,未经博主允许不得转载。

unix环境高级编程---线程

标签:

原文地址:http://blog.csdn.net/a879365197/article/details/46710335

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!