pthread_cond_wait()与pthread_mutex_lock()

1.A(主依次创建B,C),B,C三个线程

  A中无pthread_mutex_lock()或 pthread_mutex_unlock();

  B中开始3个pthread_mutex_unlock()然后2个pthread_mutex_lock(),照样把自己锁住;C中两个pthread_mutex_unlock()也被锁住;

2.

  A中1个pthread_mutex_unlock();

  B中开始3个pthread_mutex_unlock()然后2个pthread_mutex_lock(),B被解开;C中两个pthread_mutex_unlock()依然被锁住;

3.

  A中2个pthread_mutex_unlock();

  B中开始3个pthread_mutex_unlock()然后2个pthread_mutex_lock(),B被解开;C中两个pthread_mutex_unlock()依然被锁住;

4.

  A中3个pthread_mutex_unlock();

  B中开始3个pthread_mutex_unlock()然后2个pthread_mutex_lock(),B被解开;C中两个pthread_mutex_unlock()被解开;

5.pthread_cond_wait()需要两个条件才能被唤醒,pthread_mutex_unlock()与pthread_cond_signal()无先后顺序。。

 

(心法:S初值为1,即使不停地调用pthread_mutex_unlock()(它会首先判断S然后决定是否加1)依然不会大于1,

    线程调用pthread_mutex_lock()时,若S为0则睡眠,  若S为1,则S减1继续执行。。只要S为1时,睡眠队列第一

    个会被唤醒同时S减1。。所以S只有0,1两个值。。。。。。我真是个天才,,自个猜的。。)

///

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
void* testThreadPool(void *t);
pthread_mutex_t clifd_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t clifd_cond = PTHREAD_COND_INITIALIZER;

int main()
{   char  b=9;
    pthread_t  mythread;
    if (pthread_create(&mythread, NULL, (void*)testThreadPool, (void*)(&b)))
       {
         printf("pthread_create\n");
       }
  sleep(2);
  printf("father : 1\n");
  pthread_mutex_lock(&clifd_mutex);
  sleep(2);
  printf("father : 2\n");
  pthread_cond_signal(&clifd_cond);
  sleep(2);
  printf("father : 3\n");
  pthread_mutex_unlock(&clifd_mutex);
  printf("father : 4\n");
}
void* testThreadPool(void *t)
{
 
   printf("child :t is %d\n", (*(char *)t));
   printf("child :1\n");
   pthread_mutex_lock(&clifd_mutex);
   printf("child :2\n");
   pthread_cond_wait(&clifd_cond, &clifd_mutex);
   printf("child :3\n");
   pthread_mutex_unlock(&clifd_mutex);
   printf("child :4\n");
  
   printf("child :5\n");
}

 

 

[root@song pthread_cont_wait]# ./song
child :t is 9
child :1
child :2
father : 1
father : 2
father : 3
child :3
child :4
child :5
father : 4

 

///


#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
void* testThreadPool(void *t);
void* song(void* s);
pthread_mutex_t clifd_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t clifd_cond = PTHREAD_COND_INITIALIZER;

int main()
{   char  b=9;
    pthread_t  mythread;
     pthread_t  ssthread;
    if (pthread_create(&mythread, NULL, (void*)testThreadPool, (void*)(&b)))
       {
         printf("pthread_create\n");
       }
    if (pthread_create(&ssthread, NULL, (void*)song, (void*)(&b)))
       {
         printf("pthread_create\n");
       }
  sleep(2);
 printf("father : 1\n");
    pthread_mutex_lock(&clifd_mutex);
sleep(2);
 printf("father : 2\n");
       pthread_cond_signal(&clifd_cond);
sleep(2);
 printf("father : 3\n");
    pthread_mutex_unlock(&clifd_mutex);
sleep(2);
 printf("father : 4\n");
 sleep(3);
}
void* testThreadPool(void *t)
{
 
   printf("child :t is %d\n", (*(char *)t));
   printf("child :1\n");
   pthread_mutex_lock(&clifd_mutex);
   printf("child :2\n");
   pthread_cond_wait(&clifd_cond, &clifd_mutex);
   printf("child :3\n");
   pthread_mutex_unlock(&clifd_mutex);
   printf("child :4\n");
  
   printf("child :5\n");
     pthread_cond_signal(&clifd_cond);
}
void *song(void* s)
{  printf("song:1\n");
   pthread_mutex_lock(&clifd_mutex);
   printf("song :2\n");
   pthread_cond_wait(&clifd_cond, &clifd_mutex);
   printf("song:3\n");
   pthread_mutex_unlock(&clifd_mutex);
   printf("song:4\n");
   printf("song:5\n");
}
[root@song pthread_cont_wait]# ./song
child :t is 9
child :1
child :2
song:1
song :2
father : 1
father : 2
father : 3
child :3
child :4
child :5
song:3
song:4
song:5
father : 4
[root@song pthread_cont_wait]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pthread_mutex_lock和pthread_cond_wait的组合使用可以用于线程间的同步和通信。具体来说,pthread_mutex_lock用于保护共享资源,pthread_cond_wait则用于等待条件变量的发生。 当一个线程需要访问共享资源时,它首先需要获取互斥锁,如果互斥锁已经被其他线程占用,则该线程会被阻塞。当该线程成功获取互斥锁后,它可以访问共享资源,并且可以通过条件变量来等待某个条件的发生。如果条件不满足,则该线程会被阻塞,并且会自动释放互斥锁,以便其他线程可以访问共享资源。当条件满足时,另一个线程可以通过pthread_cond_signal或pthread_cond_broadcast来通知等待的线程,使其重新竞争互斥锁。 下面是一个简单的例子: ```c pthread_mutex_t mutex; pthread_cond_t cond; int count = 0; void* producer(void* arg) { while (1) { pthread_mutex_lock(&mutex); count++; printf("producer: count = %d\n", count); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); sleep(1); } } void* consumer(void* arg) { while (1) { pthread_mutex_lock(&mutex); while (count == 0) { pthread_cond_wait(&cond, &mutex); } count--; printf("consumer: count = %d\n", count); pthread_mutex_unlock(&mutex); sleep(1); } } int main() { pthread_t tid1, tid2; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); pthread_create(&tid1, NULL, producer, NULL); pthread_create(&tid2, NULL, consumer, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); return 0; } ``` 在这个例子中,生产者线程不断地增加count的值,并通过pthread_cond_signal来通知等待的消费者线程。消费者线程则通过pthread_cond_wait来等待count的值不为0。当count的值不为0时,消费者线程减少count的值,并通过printf来输出count的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值