Chinaunix首页 | 论坛 | 博客
  • 博客访问: 183935
  • 博文数量: 54
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2018
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-31 23:14
文章存档

2014年(2)

2013年(52)

分类: 嵌入式

2013-08-26 20:11:28

线程寄生于进程;

由于进程的地址是私有的因此在进程间上下切换时,系统开销大,从而引入线程。在同一个进程中创建的线程共享该进程的地址空间。

多线程通过第三方线程库来实现。

亲缘线程共享:
            1,可执行的指令;
            2,静态数据;
            3,进程中打开的文件描述符;
            4,信号处理函数;
            5,信号处理函数;
            6,当前工作目录;
            7,用户ID;
            8,用户组ID。

没有同步的线程竞争执行。

线程很容易实现通信(通过全局变量等交换);

*********************************************
创建线程:主线程和线程竞争执行。

点击(此处)折叠或打开

  1. #include <pthread.h>
  2. #include <stdio.h>

  3. int a;

  4. void *fun(void *x)
  5. {
  6.     while(1)
  7.     {
  8.         a = 0;
  9.         sleep(2);
  10.         printf("aaaaaaaaa%daaaaaaaaa\n", a);
  11.     }
  12. }

  13. int main()
  14. {    
  15.     pthread_t t;
  16.     pthread_create(&t, NULL, fun, NULL);
  17.     printf("creat_thread : %ld\n", t);
  18.     while(1)
  19.     {
  20.         a = 100;
  21.         sleep(1);
  22.         printf("==========%d==========\n", a);
  23.     }
  24. }
线程传参:
创建了两个线程,都调用同一个函数。根据不同的参数产生不同结果。

点击(此处)折叠或打开

  1. #include <pthread.h>
  2. #include <stdio.h>

  3. void *fun(void *a)
  4. {
  5.     while(1)
  6.     {
  7.         sleep(1);
  8.         printf("%s\n", (char *)a);
  9.     }
  10. }

  11. int main()
  12. {
  13.     
  14.     pthread_t t, t1;
  15.     pthread_create(&t, NULL, fun, "hello");
  16.     printf("creat_thread : %ld\n", t);
  17.     pthread_create(&t1, NULL, fun, "word");
  18.     printf("creat_thread : %ld\n", t1);

  19.     while(1);
  20. }
线程等待函数:
当线程t调用函数fun运行结束才会运行printf。

点击(此处)折叠或打开

  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <malloc.h>

  5. void *fun(void *x)
  6. {
  7.     char *buf = malloc(100);
  8.     strcpy(buf, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  9.     pthread_exit(buf);
  10. }

  11. int main()
  12. {
  13.     pthread_t t;
  14.     pthread_create(&t, NULL, fun, NULL);
  15.     
  16.     char *p;
  17.     pthread_join(t, (void **)&p);
  18.     printf("%s\n", p);
  19. }
多线程同步:使用信号量函数控制执行顺序。同步即线程按照一定顺序执行。
                  线程同步和互斥靠信号量和互斥锁控制。

点击(此处)折叠或打开

  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <semaphore.h>

  4. sem_t a;
  5. sem_t b;
  6. sem_t c;
  7. void *fun(void *x)
  8. {
  9.     while(1)
  10.     {
  11.         sem_wait(&a);//申请资源,并值减一;执行wait后的代码
  12.             printf("hello\n");
  13.         sem_post(&b);//释放资源,并值加一
  14.     }
  15. }

  16. void *fun1(void *x)
  17. {
  18.     while(1)
  19.     {
  20.         sem_wait(&c);
  21.             printf("word\n");
  22.         sem_post(&a);
  23.     }
  24. }

  25. void *fun2(void *x)
  26. {
  27.     while(1)
  28.     {
  29.         sem_wait(&b);
  30.             printf("the\n");
  31.         sem_post(&c);
  32.     }
  33. }


  34. int main()
  35. {
  36.     sem_init(&a, 0, 1);
  37.     sem_init(&b, 0, 0);
  38.     sem_init(&c, 0, 0);

  39.     pthread_t t, t1;
  40.     pthread_create(&t, NULL, fun, NULL);
  41.     printf("creat_thread : %ld\n", t);
  42.     pthread_create(&t1, NULL, fun1, NULL);
  43.     printf("creat_thread : %ld\n", t1);
  44.     pthread_create(&t1, NULL, fun2, NULL);
  45.     printf("creat_thread : %ld\n", t1);

  46.     while(1);
  47. }
线程互斥:由于线程间资源共享,同时也有其互斥的场合。
                互斥锁用来保护临界资源的安全。(临界资源,线程双方会用到的资源);
                线程需先获得互斥锁才能访问临界资源。访问完后释放。
                这里的线程没有同步,竞争执行。lock和unlock中间的资源保护后数据才得以完整。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <string.h>

  4. pthread_mutex_t mylock;

  5. void *fun_show(void *a)
  6. {
  7.     while(1)
  8.     {
  9.         usleep(10000);
  10.         pthread_mutex_lock(&mylock);
  11.         printf("%s\n", (char *)a);
  12.         pthread_mutex_unlock(&mylock);
  13.     }
  14. }

  15. void *fun_rev(void *a)
  16. {
  17.     int i = 0;
  18.     char *p = (char *)a;
  19.     int len = strlen(p);
  20.     while(1)
  21.     {
  22.         pthread_mutex_lock(&mylock);
  23.         for(i = 0;i<len/2; i++)
  24.         {
  25.             p[i] = p[i]^p[len-i-1];
  26.             p[len-i-1] = p[i]^p[len-i-1];
  27.             p[i] = p[i]^p[len-i-1];
  28.         }
  29.         pthread_mutex_unlock(&mylock);
  30.     }
  31. }

  32. int main()
  33. {
  34.     pthread_mutex_init(&mylock, NULL);
  35.     char buf[] = "123456789";
  36.     pthread_t t;
  37.     pthread_create(&t, NULL, fun_show, buf);
  38.     pthread_create(&t, NULL, fun_rev, buf);
  39.     while(1);
  40. }








阅读(1743) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~