linux下互斥锁的使用

源码如下


#include <cstdio>  
#include <cstdlib>  
#include <unistd.h>  
#include "iostream"  
#include <pthread.h>  
using namespace std;  
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  	//初始化 互斥锁
int tmp;  
void* thread(void *arg)  
{  
	cout << "thread id is " << pthread_self() << endl;  	//打印当前线程id
	pthread_mutex_lock(&mutex);  	//加锁  place1
	tmp = 12;  
	cout << "Now a is " << tmp << endl;  
	sleep(5);
	cout << "Now after a is " << tmp << endl;  
	pthread_mutex_unlock(&mutex);  	//解锁	place1
	return NULL;  
}  

void* thread2(void *arg)  
{  
	cout << "thread id2 is " << pthread_self() << endl;  	
	pthread_mutex_lock(&mutex);  	//加锁	place1
	tmp = 22;  
	cout << "Now a is " << tmp << endl;  
	sleep(5);
	cout << "Now after a is " << tmp << endl;  
	pthread_mutex_unlock(&mutex);  	//解锁	place1
	return NULL;  
}  

int main()  
{  	
	//thread id
	pthread_t id;  
	cout << "main thread id is " << pthread_self() << endl;  
	tmp = 3;  
	cout << "In main func tmp = " << tmp << endl;  
	if (!pthread_create(&id, NULL, thread, NULL))  
	{  
		cout << "Create thread success!" << endl;  
	}  
	else  
	{  
		cout << "Create thread failed!" << endl;  
	} 


	//thread id2
	pthread_t id2;  
	cout << "main thread id is " << pthread_self() << endl;  
	tmp = 13;  
	cout << "In main func tmp = " << tmp << endl;  
	if (!pthread_create(&id2, NULL, thread2, NULL))  
	{  
		cout << "Create thread success!" << endl;  
	}  
	else  
	{  
		cout << "Create thread failed!" << endl;  
	} 

	
	pthread_join(id, NULL);  	//等待一个线程的结束。阻塞	place2
	pthread_join(id2, NULL);  	//等待一个线程的结束。阻塞	place2
	pthread_mutex_destroy(&mutex);  
	return 0;  
}  

主线程起了 id 和id2两个子线程。

按照上述源码:

执行结果是,

此时id和id2是同步的。

异步创建好两个线程,id 和id2,然后主程序阻塞在 pthread_join(id, NULL);

然后id 和id2 随即(貌似总是id2先) 执行各自的线程函数;

由于加锁了,所以若先执行了id2,则会在id2中一直运行,包括里面的sleep5s;

5s之后,id2释放锁,id拿到锁,才进入到id的线程函数中。

id线程函数执行完成后才执行pthread_join的阻塞函数。


一.若只将 4处place1注释掉。

则此时 id和id2两个线程是异步的 

异步创建好两个线程,id 和id2,然后主程序阻塞在 pthread_join(id, NULL);

id和id2同时执行,同时sleep 5s,5s之后,同时离开各自的线程函数。

然后进入到thread的阻塞函数中去。


二.若只将 2出place2注释掉

     main函数创建完两个线程后,也不管子线程的工作情况,main函数继续执行,知道return。

    此时,两个子函数都还没有执行完。


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统中,互斥锁(Mutex)是一种用于保护共享资源的同步原语。它可以确保一个线程独占某个共享资源,直到它完成操作并释放该资源。 互斥锁的所有内容包括: 1. 头文件 在Linux系统中,互斥锁的头文件是`<pthread.h>`。在使用互斥锁时,首先需要包含该头文件。 2. 初始化互斥锁使用互斥锁之前,需要先创建并初始化一个互斥锁。可以使用`pthread_mutex_t`类型变量来表示互斥锁。在初始化互斥锁时,可以使用宏函数`pthread_mutex_init()`。 3. 加锁和解锁互斥锁 当一个线程需要访问共享资源时,它需要先获得该资源的互斥锁。可以使用宏函数`pthread_mutex_lock()`来获得互斥锁,如果互斥锁已经被其他线程占用,则该函数会阻塞当前线程,直到互斥锁可用。 当线程完成对共享资源的访问后,需要释放互斥锁。可以使用宏函数`pthread_mutex_unlock()`来释放互斥锁。 4. 销毁互斥锁 在不再需要使用互斥锁时,需要将其销毁。可以使用宏函数`pthread_mutex_destroy()`来销毁互斥锁。 5. 示例代码 下面是一个使用互斥锁的示例代码: ``` #include <pthread.h> pthread_mutex_t mutex; void* thread_func(void* arg) { // 获得互斥锁 pthread_mutex_lock(&mutex); // 访问共享资源 // 释放互斥锁 pthread_mutex_unlock(&mutex); return NULL; } int main() { // 初始化互斥锁 pthread_mutex_init(&mutex, NULL); // 创建线程 pthread_t tid; pthread_create(&tid, NULL, thread_func, NULL); // 等待线程结束 pthread_join(tid, NULL); // 销毁互斥锁 pthread_mutex_destroy(&mutex); return 0; } ``` 以上就是互斥锁Linux系统中的所有内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值