Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2150511
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2015-08-03 16:10:09

一. 线程
1. 基本函数
a. 关于pthread_id
  1. /usr/include/x86_64-linux-gnu/bits/pthreadtypes.h:
  2. typedef unsigned long int pthread_t;
定义为unsigned long int,这样打印时就可以用%u来打印了
b. 线程创建函数
线程函数都在:/usr/include/pthread.h中
  1. extern int pthread_create (pthread_t *__restrict __newthread,
  2.                const pthread_attr_t *__restrict __attr,
  3.                void *(*__start_routine) (void *),
  4.                void *__restrict __arg) __THROWNL __nonnull ((1, 3));
arg1: 值传递,获取了新创建的线程id
arg2: 线程的属性
arg3: 线程回调函数
arg4: 回调函数的参数
c.进程等侍线程结束
  1. extern int pthread_join (pthread_t __th, void **__thread_return);
线程是依赖于进程存在的,一旦进制结束就没线程什么事了。
为了防止进制结束时,还有线程要执行,需要等侍线程结束
这个等侍函数就是pthread_join
d. 获取线程id
  1. extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__));
注意:只有在线性的回调函数中调用才可以获取id,在main函数中是main的thread_id
2. 创建线程
2.1.1 创建线程不带参数

  1. cong@msi:/work/test/thread/1start$ cat start.c
  2. #include "utils.h"

  3. void* thread_routine(void* arg)
  4. {
  5.     dbmsg("%u",(unsigned int)pthread_self());
  6.     return NULL;
  7. }
  8. int main ( int argc, char *argv[] )
  9. {
  10.     pthread_t thread_id;
  11.     void* thread_result;
  12.     int status;
  13.     //创建线程: 无属性设置,回调是thread_routine,无参数
  14.     status = pthread_create(&thread_id, NULL, thread_routine, NULL);
  15.     if(status != 0)
  16.     {
  17.          dbmsg("%s",strerror(errno));
  18.          return -1;
  19.     }
  20.     dbmsg("%u", (unsigned int)thread_id);
  21.     
  22.     status = pthread_join(thread_id, &thread_result);    //等侍线程执行结束
  23.     if( status != 0)
  24.     {
  25.         dbmsg("%s", strerror(errno));
  26.         return -1;
  27.     }

  28.     if(thread_result == NULL)
  29.         return 0;
  30.     return EXIT_SUCCESS;
  31. }
1start.rar (下载后改名为1start.rar)
2.1.2 创建线程带参数
  1. cong@msi:/work/test/thread/1start$ cat start.c
  2. #include "utils.h"

  3. void* thread_routine(void* arg)
  4. {
  5.     printf("%s", (char*) arg)//打印参数
  6.     return NULL;
  7. }

  8. int main ( int argc, char *argv[] )
  9. {
  10.     pthread_t thread_id;
  11.     char buf[1024]= "hello,world\n";
  12.     //创建线程: 将buf用为参数
  13.     pthread_create(&thread_id, NULL, thread_routine, (void*)buf);
  14.     
  15.     pthread_join(thread_id, NULL);

  16.     return EXIT_SUCCESS;
  17. }
cong@msi:/work/test/thread/1start$ ./start
hello,world
2.2 线程退出
  1. cong@msi:/work/test/thread/2exit$ cat exit.c
  2. #include "utils.h"

  3. void* thread_routine(void* arg)
  4. {
  5.     dbmsg("%u",(unsigned int)pthread_self())
  6.     return ((void*)2);   //将线程结束值返回
  7. }

  8. int main ( int argc, char *argv[] )
  9. {
  10.     pthread_t thread_id;
  11.     void* thread_result;
  12.     int status;
  13.     //创建线程: 无属性设置,回调是thread_routine,无参数
  14.     status = pthread_create(&thread_id, NULL, thread_routine, NULL);
  15.     
  16.     status = pthread_join(thread_id, &thread_result);   //等侍线程结束,结束时返回值放在thread_result中

  17.     dbmsg("thread exit code=%d", (int)thread_result);   //打印看一下

  18.     return EXIT_SUCCESS;
  19. }
cong@msi:/work/test/thread/2exit$ ./exit
exit.c:main[19]: thread exit code=2     -->就是这个 
2exit.rar  (下载后改名为3mutex.tar.gz)
2.3 锁
a.不明显但不等于不存在
两个线程同时访问一个数据时,要加锁
  1. cong@msi:/work/test/thread/3mutex$ cat mutex.c
  2. #include "utils.h"
  3. int globle;                             //要保护的全局变量
  4. pthread_mutex_t thread_mutex;

  5. void* thread_routine(void* arg)
  6. {
  7.     pthread_mutex_lock(&thread_mutex);      //2.上锁
  8.     globle ++;                              //把要保护的数据放中间
  9.     dbmsg("globle=%d",globle);
  10.     pthread_mutex_unlock(&thread_mutex);    //3.解锁
  11.     return ((void*)2);
  12. }

  13. int main ( int argc, char *argv[] )
  14. {
  15.     pthread_t thread_id1;
  16.     pthread_t thread_id2;
  17.     void* thread_result;
  18.     int status;

  19.     globle = 1;
  20.     pthread_mutex_init(&thread_mutex, NULL);     //1.初始化锁

  21.     status = pthread_create(&thread_id1, NULL, thread_routine, NULL);
  22.     status = pthread_create(&thread_id2, NULL, thread_routine, NULL);
  23.     
  24.     status = pthread_join(thread_id1, &thread_result);
  25.     status = pthread_join(thread_id2, &thread_result);

  26.     return EXIT_SUCCESS;
  27. }
结果:
cong@msi:/work/test/thread/3mutex$ ./mutex
mutex.c:thread_routine[9]: globle=2
mutex.c:thread_routine[9]: globle=3
b.来个明显的
b.1 第1个线程运行时,在回调函数中产生一个局部变量buf_1指向参数buf1的首地址,
获取锁,进行打印
b.2 此时第2个线程开始运行,在回调函数中产生一个局部变量buf_2指向参数buf1的首地址,
获取不了锁,不能进行打印,一直在等侍线程1运行结束释放锁
b.3然后线程2开始打印
b.4 如果没有锁时,就会出乱子
  1. cong@msi:/work/test/thread/3mutex$ cat ./mutex.c
  2. #include "utils.h"
  3. pthread_mutex_t thread_mutex;

  4. void* thread_routine(void* arg)
  5. {
  6.     char* buf = (char*) arg;
  7.     //pthread_mutex_lock(&thread_mutex);     //加上锁之后就不存在问题了
  8.     while(*buf != '\0')
  9.     {
  10.         printf("%c", *buf);
  11.         buf++;
  12.     }
  13.     printf("\n");
  14.     //pthread_mutex_unlock(&thread_mutex);
  15.     return NULL;
  16. }

  17. int main ( int argc, char *argv[] )
  18. {
  19.     pthread_t thread_id1;
  20.     pthread_t thread_id2;
  21.     void* thread_result;
  22.     int status;
  23.     int i;
  24.     char buf1[1024];
  25.     char buf2[1024];
  26.     for(i=0;i<1023; i++)
  27.     {
  28.         buf1[i] = 'a';
  29.         buf2[i] = 'b';
  30.     }
  31.     buf1[1023] = '\0';
  32.     buf2[1023] = '\0';


  33.     pthread_mutex_init(&thread_mutex, NULL);

  34.     status = pthread_create(&thread_id1, NULL, thread_routine, (void*)buf1);
  35.     status = pthread_create(&thread_id2, NULL, thread_routine, (void*)buf2);
  36.     
  37.     status = pthread_join(thread_id1, &thread_result);
  38.     status = pthread_join(thread_id2, &thread_result);

  39.     return EXIT_SUCCESS;
  40. }
结果:
  1. cong@msi:/work/test/thread/3mutex$ ./mutex   -->第1次执行没有出现问题
  2. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  4. cong@msi:/work/test/thread/3mutex$ ./mutex  -->第2次执行发现出问题了,这跟等公交车一样,不等车时一个接一个的车来了,真要等车时车反而不来了
  5. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabaaaaabaaabaaaabaaaabaaaabaaaabaaaabaaaabaaaabaaaabaaaabaaaabaaaabaaaabaaaababbabababaaabaaaabbbabbbbbabbbbbabbbbabbbbabbbbabaaaabaaaabaaaaabaaaabaaaabababaaaabaaaababababbbababaabaaaabaaaababababbbbbabbabaaaabaaaababababbbbabaababbbbabaaababbbabababaaaaabababaaaababbbabbbbabbbbabaaaababababbbbabbbbaaaabaaaabaaaababbabbbbabababbbbaaababababbbbabaaaababababbbbaaaabaaababbbbabaaaaabaaaababbbbabbbbabbbbabaaaababababaaaabbbaaababbbbabbbbbabbbbabbbbbabbbbabbbbabbbbabbbbabaaaababababbbbabababbbbabbbbabababbbbabbbbabbbbbabbbbabbbbaaaababbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbababbbbabababababababbbbabbbababbbbabaaaababababbbbabbbbabababaaaababbabaaaabaaababbbbbabbbbabaaaababbabaaaababbbbabaaaababbbbabbbbbabbbbbabbbbbabbbbbabbbbbabbbbabaaaababbbbabaaaababababbbbabaaaabaaaababbbbabbbbabaaaababbabbbbabbbbabababaaaababbabbbbaaabaaaababaabaaaababbbbabbbbababababababababbbbababbabbbbabababaaababbbbabaaaababbabbbbabaaaababbabaaaababbabbbbabaaaaabaaaababaabaaaaabaaababbbbababbabbbbabaabaaaaabaaaababbabbbbabaaaababbabbbbabaaaabaaaababaabababaaaababbbbabbbbabaaaababaabababaaaababaababbabbbbbabbbbabbbbabaaaababaababbabaaaabaaaaabaaaababaababbabababbbbabababaaaabaaaababbbbabababaaaaabaaaaababababbbbabbbbbabbbbabbbbabbbbabaaaababaababbbababbbbabaaaababbbbabaaaababaabaaa
  6. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
3mutex.rar (下载后改名为3mutex.tar.gz)
阅读(1350) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~