pthread_mutex_t 和 alarm 一起用为什么就无效了?

社会栋梁 2013-11-29 10:05:48

#include<pthread.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/time.h>

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void sig_handler(int signo)
{
printf("enter mutex before\n");
pthread_mutex_lock(&mutex);
unsigned long long n = 0;
while(n<10000000000)
{
if(n%10000 == 0)
printf("1");
n++;
}
pthread_mutex_unlock(&mutex);
printf("leave mutex\n");
alarm(1);

}

void *pthread_func(void * arg)
{
alarm(1);
while(1)
{
pthread_mutex_lock(&mutex);
printf("enter mutex thread +++++++\n");
unsigned long long n = 0;
while(n<10000000000)
{
if(n%10000 == 0)
printf("0");
n++;
}
pthread_mutex_unlock(&mutex);
printf("leave mutex thread ------\n");
}

}

int main(int argc, char **argv)
{
pthread_t tid, tid_1;
int retval;

signal(SIGALRM, sig_handler);

if((retval = pthread_create(&tid, NULL, pthread_func, NULL)) < 0)
{
perror("pthread_create");
exit(-1);
}

sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGALRM);
pthread_sigmask(SIG_SETMASK,&sigset,NULL);

while(1)
{
printf("main pthread\n");
sleep(10);
}
return 0;
}




结果:
main pthread
enter mutex thread +++++++
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000enter mutex before
main pthread
main pthread
main pthread
。。。

为什么一直没有printf("leave mutex thread ------\n");就中断了呢?
...全文
182 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
missheaven2011 2013-11-29
引用 1 楼 sundq12 的回复:
http://www.cppblog.com/mysileng/archive/2013/01/05/196971.html看看这个文章就知道怎么回事了,要学会google嘛。
学习了
  • 打赏
  • 举报
回复
空的 2013-11-29
void *pthread_func(void * arg) 锁了 然后 print 0 1秒过了 触发SIGALRM void sig_handler(int signo) 再请求锁 好了,竞争了就堵死了
  • 打赏
  • 举报
回复
sundq12 2013-11-29
http://www.cppblog.com/mysileng/archive/2013/01/05/196971.html看看这个文章就知道怎么回事了,要学会google嘛。
  • 打赏
  • 举报
回复
相关推荐
发帖
C语言

6.6w+

社区成员

C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
帖子事件
创建了帖子
2013-11-29 10:05
社区公告
暂无公告