美文网首页
APUE//线程同步2

APUE//线程同步2

作者: ustclcl | 来源:发表于2019-03-25 21:23 被阅读0次

时间锁

#include <pthread.h>
#include <time.h>
int pthread_mutex_timedlock(pthread_mutex_t * restrict mutex, const struct timespec *restrict tsptr);

愿意等待timespec描述的时间,达到时间返回错误码ETIMEOUT
一个例子

// APUE example11-13                                       
// pthread_mutex_timedlock                                 
// lcl 20190325                                            
//                                                         
#include "../myapue.h"                                     
#include <pthread.h>                                       
#include <time.h>                                          
                                                           
int main (void)                                            
{                                                          
    int err;                                               
    struct timespec tout;                                  
    struct tm *tmp;                                        
    char buf[64];                                          
    pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;      
                                                           
    pthread_mutex_lock(&lock);                             
    printf("the mutex now is locked\n");                   
    clock_gettime(CLOCK_REALTIME, &tout);                  
    tmp = localtime(&tout.tv_sec);                         
    strftime(buf, sizeof(buf), "%r", tmp);                 
    printf("current time is %s\n",buf);                    
                                                           
    tout.tv_sec += 10;                                     
    err = pthread_mutex_timedlock(&lock,&tout);            
                                                           
    clock_gettime(CLOCK_REALTIME, &tout);                  
    tmp = localtime(&tout.tv_sec);                         
    strftime(buf,sizeof(buf), "%r", tmp);                  
    printf("the time is now %s\n",buf);                    
                                                           
    if(err == 0)                                           
      printf("mutex locked again!\n");                     
    else                                                   
      printf("can't lock mutex again:%s\n",strerror(err)); 
    exit(0);                                               
}                                                          
                                                           
                                                           

运行结果:

the mutex now is locked
current time is 01:08:51 PM
the time is now 01:09:01 PM
can't lock mutex again:Connection timed out

读写锁,三种状态读加锁、写加锁和不加锁

#include <pthread.h>
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
                                      const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destory(pthread_rwlock_t *rwlock);

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)

如上,函数名可以很好的反映函数的功能。

  • 写锁阻塞其他加锁
  • 读锁阻塞写锁但不阻塞读锁
  • 写锁在阻塞时,不再响应其他读锁请求。

相关文章

  • APUE//线程同步2

    时间锁 愿意等待timespec描述的时间,达到时间返回错误码ETIMEOUT一个例子 运行结果: 读写锁,三种状...

  • APUE//线程同步

    互斥量mutex 下面的例子演示对一个对象进行引用计数。引用计数是受互斥量保护的,每回hold对象时,加锁,引用计...

  • APUE//线程同步3

    使用条件变量进行线程同步 barrier,屏障

  • 【APUE】线程

  • APUE线程

    十一章 线程 多线程可以实现在某一个时刻能够做不止一件事,每个线程处理各自独立的任务 好处如下通过为每种事件类型分...

  • 腾讯Java面试题

    基础部分: 1、HashTable线程同步,HashMap非线程同步。 2、HashTable不允许<键,值>有空...

  • 程序员腾讯2年3次面试,总结“高频”java面试问题,成功拿下1

    基础部分: 1、HashTable线程同步,HashMap非线程同步。 2、HashTable不允许<键,值>有空...

  • OpenMP多线程——Parallel for

    多线程——线程同步 数据竞争问题 线程互斥同步——critical 线程互斥同步——atmoic 线程互斥同步——...

  • iOS GCD之dispatch_semaphore

    dispatch_semaphore主要应用有:(1)线程同步(同步任务)(2)线程加锁(资源访问控制) 相关函数...

  • iOS多线程(四)

    多线程安全隐患解决方案 1.解决方案:使用线程同步技术(协同步调,按预定的先后次序进行) 2.常用的线程同步技术:...

网友评论

      本文标题:APUE//线程同步2

      本文链接:https://www.haomeiwen.com/subject/mccivqtx.html