Linux 下互斥锁

hsy1010 2014-03-03 05:27:32
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int lock_var=1;
time_t end_time;

void pthread1(void *arg);
void pthread2(void *arg);

int main(int argc, char *argv[])
{
pthread_t id1,id2;
pthread_t mon_th_id;
int ret;
end_time = time(NULL)+10;

pthread_mutex_init(&mutex,NULL);

ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);
if(ret!=0)
perror("pthread cread1");

ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);
if(ret!=0)
perror("pthread cread2");
pthread_join(id1,NULL);
pthread_join(id2,NULL);
exit(0);
}

void pthread1(void *arg)
{
int i;
while(time(NULL) < end_time)
{
if(pthread_mutex_lock(&mutex)!=0)
{
perror("pthread_mutex_lock");
}
else
{
printf("pthread1:pthread1 lock the variable\n");
}

for(i=0;i<1;i++)
{
sleep(1);
lock_var+=2;
}

if(pthread_mutex_unlock(&mutex)!=0)
{
perror("pthread_mutex_unlock");
}
else
{
printf("pthread1:pthread1 unlock the variable\n");
}

sleep(1);
}
}

void pthread2(void *arg)
{
int nolock=0;
int ret;
while(time(NULL) < end_time)
{
ret=pthread_mutex_lock(&mutex);

if(ret==EBUSY)
{
printf("pthread2:the variable is locked by pthread1\n");
}

else
{
if(ret!=0)
{
perror("pthread-mutex_trylock");
exit(1);
}
else
{
printf("pthread2 got lock.the variable is %d\n",lock_var);
}
if(pthread_mutex_unlock(&mutex)!=0)
{
perror("pthread_mutex_unlock");
}
else
{
printf("pthread2:pthread2 unlock the variable\n");
}
}
sleep(2);
}
}

上面是一个简单的互斥锁,但是有一点不明白,为什么两个线程同时得到了同一个锁,打印如下
pthread1:pthread1 lock the variable
pthread1:pthread1 unlock the variable
pthread2 got lock.the variable is 3
pthread2:pthread2 unlock the variable
pthread1:pthread1 lock the variable
pthread2 got lock.the variable is 5
pthread2:pthread2 unlock the variable
pthread1:pthread1 unlock the variable
pthread1:pthread1 lock the variable
pthread2 got lock.the variable is 7
pthread2:pthread2 unlock the variable
pthread1:pthread1 unlock the variable
pthread1:pthread1 lock the variable
pthread2 got lock.the variable is 9
pthread2:pthread2 unlock the variable
pthread1:pthread1 unlock the variable
pthread1:pthread1 lock the variable
pthread2 got lock.the variable is 11
pthread2:pthread2 unlock the variable
pthread1:pthread1 unlock the variable
求解释。。。。 越详细越好!!!
...全文
269 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010599631 2014-03-07
  • 打赏
  • 举报
回复
http://baike.baidu.com/link?url=eGmJqitTvncQwfDkWkdIOAL7BGRTU8Vn-pPnEcNDlfSfefvEBGuRn8u_24aUYcRLo_DZrJpSS4Tk1qaZEs3ZGq 你看看trylock的功能和函数返回啊,你这样判断是没意义的。trylock只有两种返回值,0和非0,而且不会导致休眠,所以。。。
hsy1010 2014-03-05
  • 打赏
  • 举报
回复
如果换成trylock为什么没出现这种情况啊!!!
void pthread2(void *arg)
{
	int ret;
	while(time(NULL) < end_time)
	{	
		ret=pthread_mutex_trylock(&mutex);

		if(ret == EBUSY)
		{
		  printf("pthread2:the variable is locked by pthread1\n");
		}
		else
		{
		  if(ret!=0)
		  {
			 perror("pthread-mutex_trylock");
			 exit(1);
		  }
		  else
		  {
			printf("pthread2 got lock.the variable is %d\n",lock_var);
		  }
		  if(pthread_mutex_unlock(&mutex)!=0)
		  {
			perror("pthread_mutex_unlock");
		  }
		  else
		  {
			printf("pthread2:pthread2 unlock the variable\n");
		  }
       }		
		sleep(2);
	}
}
hsy1010 2014-03-04
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int lock_var=1;
time_t end_time;

void pthread1(void *arg);
void pthread2(void *arg);

int main(int argc, char *argv[])
{
	pthread_t id1,id2;
	int ret;
	end_time = time(NULL)+10;
	
	ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);
	if(ret!=0)
		perror("pthread cread1");

	ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);
	if(ret!=0)
		perror("pthread cread2");
	pthread_join(id1,NULL);
	pthread_join(id2,NULL);
	exit(0);
}

void pthread1(void *arg)
{
	int i;
	while(time(NULL) < end_time)
	{
		if(pthread_mutex_lock(&mutex)!=0)
		{
			perror("pthread_mutex_lock");
		}
		 else
		 {
			printf("pthread1:pthread1 lock the variable\n");
		 }
		 
		 for(i=0;i<1;i++)
		 {
			sleep(1);
			lock_var+=2;
		 }
		 
		if(pthread_mutex_unlock(&mutex)!=0)
		{
			perror("pthread_mutex_unlock");
		}
		else
		{
			printf("pthread1:pthread1 unlock the variable\n");
		}
		
	sleep(1);
	}
}

void pthread2(void *arg)
{
	int ret;
	while(time(NULL) < end_time)
	{	
		ret=pthread_mutex_lock(&mutex);
		if(ret!=0)
		{
			perror("pthread_mutex_lock");
			exit(1);
		}
		else
		{
			printf("pthread2 got lock.the variable is %d\n",lock_var);
		}
		if(pthread_mutex_unlock(&mutex)!=0)
		{
			perror("pthread_mutex_unlock");
		}
		else
		{
			printf("pthread2:pthread2 unlock the variable\n");
		}	
		sleep(2);
	}
}
谢谢上面的两位,我现在把代码改成这样。运行结果如下: pthread1:pthread1 lock the variable pthread1:pthread1 unlock the variable pthread2 got lock.the variable is 3 pthread2:pthread2 unlock the variable pthread1:pthread1 lock the variable pthread1:pthread1 unlock the variable pthread2 got lock.the variable is 5 pthread2:pthread2 unlock the variable pthread1:pthread1 lock the variable pthread2 got lock.the variable is 7 pthread2:pthread2 unlock the variable pthread1:pthread1 unlock the variable pthread1:pthread1 lock the variable pthread2 got lock.the variable is 9 pthread2:pthread2 unlock the variable pthread1:pthread1 unlock the variable pthread1:pthread1 lock the variable pthread2 got lock.the variable is 11 pthread2:pthread2 unlock the variable pthread1:pthread1 unlock the variable 如果是trylock的话,貌似就不会出现这种情况。
u010599631 2014-03-04
  • 打赏
  • 举报
回复
并没有同时给一个锁上锁的情况。考虑你的printf函数,你的两个线程中,都是在解锁后进行打印消息。 设想,线程1刚刚解锁完,就去调用线程2,等2运行到sleep,再回来运行线程1剩下的printf,那么也就出现了你上面看到的情况。 这是典型的错误理解。因为第二个printf根本不在锁内,不必竞争锁,所以随机出来。 如果要测试锁,光靠打印信息不够,建议用变量的修改比较合理。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int lock_var=1;
time_t end_time;

void pthread1(void *arg);
void pthread2(void *arg);

int main(int argc, char *argv[])
{
	pthread_t id1,id2;
	pthread_t mon_th_id;
	int ret;
	end_time = time(NULL)+10;

	pthread_mutex_init(&mutex,NULL);
	
	ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);
	if(ret!=0)
		perror("pthread cread1");

	ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);
	if(ret!=0)
		perror("pthread cread2");
	pthread_join(id1,NULL);
	pthread_join(id2,NULL);
	exit(0);
}

void pthread1(void *arg)
{
	int i;
	while(time(NULL) < end_time)
	{
		if(pthread_mutex_lock(&mutex)!=0)
		{
			perror("pthread_mutex_lock");
		}
		 else
		 {
			printf("pthread1:pthread1 lock the variable\n");
		 }
		 
		 for(i=0;i<1;i++)
		 {
			sleep(1);
			lock_var+=2;
		 }
		printf("pthread1:pthread1 unlock the variable\n"); 
		if(pthread_mutex_unlock(&mutex)!=0)
		{
			perror("pthread_mutex_unlock");
		}
		else
		{
			
		}
		
	sleep(1);
	}
}

void pthread2(void *arg)
{
	int nolock=0;
	int ret;
	while(time(NULL) < end_time)
	{	
		ret=pthread_mutex_lock(&mutex);
		if(ret==EBUSY)	
		{
			printf("pthread2:the variable is locked by pthread1\n");
		}	
		else
		{
			if(ret!=0)
			{
				perror("pthread-mutex_trylock");
				exit(1);
			}
			else
			{
				printf("pthread2 got lock.the variable is %d\n",lock_var);
			}
			printf("pthread2:pthread2 unlock the variable\n");
			if(pthread_mutex_unlock(&mutex)!=0)
			{
				perror("pthread_mutex_unlock");
			}
			else
			{
				
			}
		}
		sleep(2);
	}
}
zhxianbin 2014-03-04
  • 打赏
  • 举报
回复
pthread_mutex_init(&mutex,NULL); //main 中这行删除 ret=pthread_mutex_lock(&mutex); //pthread2 中这行应改为 ret=pthread_mutex_trylock(&mutex);
布鲁克斯南南 2014-03-04
  • 打赏
  • 举报
回复
pthread_mutex_init(&mutex,NULL); 你的mutex已经在代码前初始化了、这里应该不需要再用这个函数了
u010599631 2014-03-04
  • 打赏
  • 举报
回复
printf("pthread1:pthread1 unlock the variable\n"); 要解锁前打印
if(pthread_mutex_lock(&mutex)!=0)
        {
            perror("pthread_mutex_lock");
        }
         else
         {
            printf("pthread1:pthread1 lock the variable\n");
         }
          
         for(i=0;i<1;i++)
         {
            sleep(1);
            lock_var+=2;
         }
        printf("pthread1:pthread1 unlock the variable\n"); 
        if(pthread_mutex_unlock(&mutex)!=0)
        {
            perror("pthread_mutex_unlock");
        }
        else
        {
             
        }
hsy1010 2014-03-03
  • 打赏
  • 举报
回复
pthread1:pthread1 lock the variable pthread1:pthread1 unlock the variable pthread2 got lock.the variable is 3 pthread2:pthread2 unlock the variable pthread1:pthread1 lock the variable pthread1:pthread1 unlock the variable pthread2 got lock.the variable is 5 pthread2:pthread2 unlock the variable pthread1:pthread1 lock the variable pthread2 got lock.the variable is 7 pthread2:pthread2 unlock the variable pthread1:pthread1 unlock the variable pthread1:pthread1 lock the variable pthread2 got lock.the variable is 9 pthread2:pthread2 unlock the variable pthread1:pthread1 unlock the variable pthread1:pthread1 lock the variable pthread2 got lock.the variable is 11 pthread2:pthread2 unlock the variable pthread1:pthread1 unlock the variable 为什么存在同时给一个锁上锁的情况,不懂。
hsy1010 2014-03-03
  • 打赏
  • 举报
回复
刚刚可能是把代码复制、粘贴后成了这个样子。现在应该好了吧。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int lock_var=1;
time_t end_time;

void pthread1(void *arg);
void pthread2(void *arg);

int main(int argc, char *argv[])
{
	pthread_t id1,id2;
	pthread_t mon_th_id;
	int ret;
	end_time = time(NULL)+10;

	pthread_mutex_init(&mutex,NULL);
	
	ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);
	if(ret!=0)
		perror("pthread cread1");

	ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);
	if(ret!=0)
		perror("pthread cread2");
	pthread_join(id1,NULL);
	pthread_join(id2,NULL);
	exit(0);
}

void pthread1(void *arg)
{
	int i;
	while(time(NULL) < end_time)
	{
		if(pthread_mutex_lock(&mutex)!=0)
		{
			perror("pthread_mutex_lock");
		}
		 else
		 {
			printf("pthread1:pthread1 lock the variable\n");
		 }
		 
		 for(i=0;i<1;i++)
		 {
			sleep(1);
			lock_var+=2;
		 }
		 
		if(pthread_mutex_unlock(&mutex)!=0)
		{
			perror("pthread_mutex_unlock");
		}
		else
		{
			printf("pthread1:pthread1 unlock the variable\n");
		}
		
	sleep(1);
	}
}

void pthread2(void *arg)
{
	int nolock=0;
	int ret;
	while(time(NULL) < end_time)
	{	
		ret=pthread_mutex_lock(&mutex);
		if(ret==EBUSY)	
		{
			printf("pthread2:the variable is locked by pthread1\n");
		}	
		else
		{
			if(ret!=0)
			{
				perror("pthread-mutex_trylock");
				exit(1);
			}
			else
			{
				printf("pthread2 got lock.the variable is %d\n",lock_var);
			}
			if(pthread_mutex_unlock(&mutex)!=0)
			{
				perror("pthread_mutex_unlock");
			}
			else
			{
				printf("pthread2:pthread2 unlock the variable\n");
			}
		}
		sleep(2);
	}
}
zhxianbin 2014-03-03
  • 打赏
  • 举报
回复
请把代码整理一下,没看明白!

18,829

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧