博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux 互斥锁之trylock的简单测试
阅读量:6894 次
发布时间:2019-06-27

本文共 1377 字,大约阅读时间需要 4 分钟。

 
/*
************************************************************************
* Filename: n_trylock.c
* Description:
* Version: 1.0
* Created: 2011骞?4鏈?2鏃?20鏃?9鍒?2绉?
* Revision: none
* Compiler: gcc
* Author: wenhao (wh), hnrain1004@gmail.com
* Company: sunplusapp
* ***********************************************************************
*/
#include
<
stdio.h
>
#include
<
pthread.h
>
#include
<
errno.h
>
pthread_mutex_t mutex;
void
*
thread_a(
void
*
arg)
{
printf(
"
thread a enter\n
"
);
pthread_mutex_lock(
&
mutex);
printf(
"
mutex a lock\n
"
);
sleep(
6
);
pthread_mutex_unlock(
&
mutex);
printf(
"
mutex a unlock\n
"
);
}
void
*
thread_b(
void
*
arg)
{
printf(
"
thread b enter\n
"
);
#if
1
pthread_mutex_trylock(
&
mutex);
#else
while
(pthread_mutex_trylock(
&
mutex)
==
EBUSY)
{
printf(
"
pthread b trylock\n
"
);
sleep(
1
);
}
#endif
printf(
"
mutex b lock\n
"
);
pthread_mutex_unlock(
&
mutex);
printf(
"
mutex b unlock\n
"
);
}
int
main(
int
argc,
char
**
argv)
{
pthread_t tid_a,tid_b;
int
err;
if
(pthread_mutex_init(
&
mutex,NULL)
!=
0
)
{
perror(
"
pthread_mutex_init
"
);
}
err
=
pthread_create(
&
tid_a,NULL,thread_a,NULL);
if
(err
!=
0
)
{
perror(
"
pthread_create thread_a
"
);
}
sleep(
1
);
err
=
pthread_create(
&
tid_b,NULL,thread_b,NULL);
if
(err
<
0
)
{
perror(
"
pthread_create thread_b
"
);
}
sleep(
10
);
printf(
"
the main close\n
"
);
return
0
;
}

转载地址:http://ymzdl.baihongyu.com/

你可能感兴趣的文章
MySQL扩展从php 5.5开始弃用
查看>>
Crash日志的分析基础
查看>>
Nginx+Https配置+Tomcat
查看>>
Real Time Web Solution for Chat by MVC SignalR Hub
查看>>
iPhone6如何进入DFU模式
查看>>
Python机器学习笔记(一):64位win7安装Python+numpy+matplotlib
查看>>
如何调整查询关键词的最大长度?
查看>>
基于宏天Est平台开发CRM系统效果预览图
查看>>
5分钟入门Elasticsearch, 从如何安装,到索引和查询(基于Window系统)
查看>>
mysql /* Authentication plugin 'caching_sha2_password' cannot be loaded: 找不到指定的模块。 */...
查看>>
[新手向视频]新版PyCharm创建项目为什么会有问题
查看>>
使用weinre远程调试网页
查看>>
ES6 异步编程之 Promise 从认识到使用
查看>>
StandardWrapper分析-tomcat6.x源码阅读
查看>>
mysql 删除重复记录只剩下一条
查看>>
突然的自我
查看>>
R语言系列:R语言中的对象
查看>>
PHP用&&和||缩写条件语句
查看>>
百度地图API
查看>>
121 项目 033 笔记向 gradle
查看>>