linux_c pthread

#include <pthread.h>
 int pthread_create(pthread_t *restrict thread,
              const pthread_attr_t *restrict attr,
              void *(*start_routine)(void*), void *restrict arg);
The  pthread_create()  function  shall  create  a  new   thread,   with
       attributes  specified  by  attr, within a process. If attr is NULL, the
       default attributes shall be used. If the attributes specified  by  attr
       are modified later, the thread’s attributes shall not be affected. Upon
       successful completion, pthread_create() shall store the ID of the  cre-
       ated thread in the location referenced by thread.

 

  1
  2 #include <stdio.h>
  3 #include <unistd.h>
  4 #include <pthread.h>
  5
  6 void *func(void *arg)
  7 {
  8
  9         while (1)
 10         {
 11                 printf("in func tid : %p\n", pthread_self());
 12                 sleep(1);
 13         }
 14 }
 15
 16 int main(void)
 17 {
 18         pthread_t tid;
 19
 20         printf("main thread id = %p \n", pthread_self());
 21         pthread_create(&tid, NULL,  func, NULL);
 22         printf("tid = %p\n", tid);
 23
 24         while (1)
 25         ;
 26         return 0;
 27 }

 pthread_join pthread_exit
  
函数pthread_join用来等待一个线程的结束。函数原型为:
  extern int pthread_join __P ((pthread_t __th, void **__thread_return));
  第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。它的函数原型为:
  extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
  唯一的参数是函数的返回代码,只要pthread_join中的第二个参数thread_return不是NULL,这个值将被传递给 thread_return。最后要说明的是,一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。
  在这一节里,我们编写了一个最简单的线程,并掌握了最常用的三个函数pthread_create,pthread_join和pthread_exit。下面,我们来了解线程的一些常用属性以及如何设置这些属性。
 2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <unistd.h>
  5 #include <pthread.h>
  6
  7 void *func(void *arg)
  8 {
  9         pthread_detach(pthread_self());
 10         int *num;
 11         num = malloc(4);
 12         *num = 99;
 13         sleep(3);
 14
 15         //return num;
 16         pthread_exit(num);
 17 }
 18
 19 int main(void)
 20 {
 21         int *p;
 22         pthread_t tid;
 23
 24         pthread_create(&tid, NULL, func, NULL);
 25
 26         pthread_join(tid, (void **)&p);
 27         printf("after join %d\n", *p);
 28
 29         return 0;
 30 }


互斥锁相关

互斥锁用来保证一段时间内只有一个线程在执行一段代码。

一 pthread_mutex_init

函数pthread_mutex_init用来生成一个互斥锁。NULL参数表明使用默认属性。如果需要声明特定属性的互斥锁,须调用函数 pthread_mutexattr_init。函数pthread_mutexattr_setpshared和函数 pthread_mutexattr_settype用来设置互斥锁属性。前一个函数设置属性pshared,它有两个取值, PTHREAD_PROCESS_PRIVATE和PTHREAD_PROCESS_SHARED。前者用来不同进程中的线程同步,后者用于同步本进程的不同线程。在上面的例子中,我们使用的是默认属性PTHREAD_PROCESS_ PRIVATE。后者用来设置互斥锁类型,可选的类型有PTHREAD_MUTEX_NORMAL、PTHREAD_MUTEX_ERRORCHECK、 PTHREAD_MUTEX_RECURSIVE和PTHREAD _MUTEX_DEFAULT。它们分别定义了不同的上所、解锁机制,一般情况下,选用最后一个默认属性。

二 pthread_mutex_lock pthread_mutex_unlock pthread_delay_np

   pthread_mutex_lock声明开始用互斥锁上锁,此后的代码直至调用pthread_mutex_unlock为止,均被上锁,即同一时间只能被一个线程调用执行。当一个线程执行到pthread_mutex_lock处时,如果该锁此时被另一个线程使用,那此线程被阻塞,即程序将等待到另一个线程释放此互斥锁。
  3 #include <unistd.h>
  4 #include <fcntl.h>
  5 #include <pthread.h>
  6
  7 int num = 0;
  8 #define LEN 10
  9
 10 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
 11 pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
 12
 13 void *func(void *arg)
 14 {
 15         int i, id = (int)arg;
 16
 17         for (i = 0; i < LEN; i++)
 18         {
 19                 if (id == 0)
 20                         pthread_mutex_lock(&mutex1);
 21                 else
 22                         pthread_mutex_lock(&mutex2);
 23                 printf("id = %d, tid = %p is running \n", id, pthread_self());
 24                 num += 1;
 25                 if (id == 0)
 26                         pthread_mutex_unlock(&mutex2);
 27                 else
 28                         pthread_mutex_unlock(&mutex1);
 29         }
 30 }
 31
 32 int main(void)
 33 {
 34         pthread_t tid1, tid2;
 35
 36         pthread_mutex_lock(&mutex2);
 37
 38         pthread_create(&tid1, NULL, func, (void *)0);
 39         pthread_create(&tid2, NULL, func, (void *)1);
 40
 41         pthread_join(tid1, NULL);
 42         pthread_join(tid2, NULL);
 43
 44         printf("num = %d\n", num);
 45
 46         return 0;
 47 }



注意:

1 需要说明的是,上面的两处sleep不光是为了演示的需要,也是为了让线程睡眠一段时间,让线程释放互斥锁,等待另一个线程使用此锁。下面的参考资料1里头说明了该问题。但是在linux下好像没有pthread_delay_np那个函数(我试了一下,提示没有定义该函数的引用),所以我用了sleep来代替,不过参考资料2中给出另一种方法,好像是通过pthread_cond_timedwait来代替,里头给出了一种实现的办法。

2 请千万要注意里头的注释comment1-5,那是我花了几个小时才找出的问题所在。
如果没有comment1和comment4,comment5,将导致在pthread_join的时候出现段错误,另外,上面的comment2和comment3是根源所在,所以千万要记得写全代码。因为上面的线程可能没有创建成功,导致下面不可能等到那个线程结束,而在用pthread_join的时候出现段错误(访问了未知的内存区)。另外,在使用memset的时候,需要包含string.h头文件哦

 

其他例程:

  1
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <unistd.h>
  5 #include <pthread.h>
  6
  7 void *func(void *arg)
  8 {
  9         int num = 0;
 10         pthread_detach(pthread_self());
 11         while (1)
 12         {
 13                 printf("hell %d\n", num++);
 14                 sleep(1);
 15         }
 16 }
 17
 18 void *func2(void *arg)
 19 {
 20         pthread_detach(pthread_self());
 21         sleep(5);
 22 //      pthread_cancel((pthread_t)arg);
 23         pthread_cancel(pthread_self());
 24         printf("after cancel\n");
 25         printf("after cancel\n");
 26         printf("after cancel\n");
 27         printf("after cancel\n");
 28         printf("after cancel\n");
 29         exit(0);
 30 }
 31
 32 int main(void)
 33 {
 34         pthread_t tid, tid2;
 35
 36         pthread_create(&tid, NULL, func, NULL);
 37         pthread_create(&tid2, NULL, func2, (void *)pthread_self());
 38         while (1)
 39         ;
 40         return 0;
 41 }

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值