互斥锁mutex

时间:2021-04-21 01:48:23

https://blog.csdn.net/rqc112233/article/details/50015069

//g++ mute.cpp -o mute -g -lrt -lpthread
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <syslog.h> static pthread_mutex_t testlock;
pthread_t test_thread; void *test(void *)
{
pthread_mutex_lock(&testlock);
printf("thread Test() \n");
syslog(LOG_DEBUG, "thread Test() \n");
pthread_mutex_unlock(&testlock);
} int main()
{
openlog("syslog", LOG_PID, LOG_DAEMON);
pthread_mutex_init(&testlock, NULL);
pthread_mutex_lock(&testlock); printf("Main lock \n");
syslog(LOG_DEBUG, "Main lock \n");
pthread_create(&test_thread, NULL, &test, NULL);
sleep(); //更加明显的观察到是否执行了创建线程的互斥锁
printf("Main unlock \n");
syslog(LOG_DEBUG, "Main unlock \n");
pthread_mutex_unlock(&testlock);
sleep();
printf("Main after sleep \n");
syslog(LOG_DEBUG, "Main after sleep \n");
pthread_join(test_thread, NULL);
pthread_mutex_destroy(&testlock);
closelog();
return ;
}

小技巧:借助syslog 记录时间  cat /var/log/syslog

输出:

Jul   :: Linux syslog[]: Main lock
Jul :: Linux syslog[]: Main unlock
Jul :: Linux syslog[]: thread Test()
Jul :: Linux syslog[]: Main after sleep

稍微改下:

//g++ mute.cpp -o mute -g -lrt -lpthread
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <syslog.h> static pthread_mutex_t testlock;
pthread_t test_thread; void *test(void *)
{
pthread_mutex_lock(&testlock);
printf("thread Test() \n");
syslog(LOG_DEBUG, "thread Test() \n");
sleep(); //更加明显的观察到是否执行了创建线程的互斥锁
pthread_mutex_unlock(&testlock);
} int main()
{
openlog("syslog", LOG_PID, LOG_DAEMON);
pthread_mutex_init(&testlock, NULL);
pthread_mutex_lock(&testlock); printf("Main lock \n");
syslog(LOG_DEBUG, "Main lock \n");
pthread_create(&test_thread, NULL, &test, NULL);
sleep(); //更加明显的观察到是否执行了创建线程的互斥锁
printf("Main unlock \n");
syslog(LOG_DEBUG, "Main unlock \n");
pthread_mutex_unlock(&testlock);
sleep();
pthread_mutex_lock(&testlock);
printf("Main after sleep \n");
syslog(LOG_DEBUG, "Main after sleep \n");
pthread_mutex_unlock(&testlock);
pthread_join(test_thread, NULL);
pthread_mutex_destroy(&testlock);
closelog();
return ;
}

输出为:

Jul   :: Linux syslog[]: Main lock
Jul :: Linux syslog[]: Main unlock
Jul :: Linux syslog[]: thread Test()
Jul :: Linux syslog[]: Main after sleep

主线程初始化互斥锁和上锁, 然后创建子线程, sleep 3s,

子线程会卡在pthread_mutex_lock(&testlock);直到主线程解锁

主线程解锁后, 子线程持有锁, sleep 2s ,  主线程sleep 1s,

主线程会卡在pthread_mutex_lock(&testlock);直到子线程解锁

阻塞的方式等待子线程结束.销毁锁

JAVA版本:

  public boolean waitDone() {
final Object waitDoneLock = new Object();
final Runnable unlockRunnable = new Runnable() {
@Override
public void run() {
synchronized (waitDoneLock) {
waitDoneLock.notifyAll();
}
}
}; synchronized (waitDoneLock) {
mCameraHandler.post(unlockRunnable);
try {
waitDoneLock.wait();
} catch (InterruptedException ex) {
QLog.v(TAG, "waitDone interrupted");
return false;
}
}
return true;
} public boolean waitDone(long timeout) {
final Object waitDoneLock = new Object();
final Runnable unlockRunnable = new Runnable() {
@Override
public void run() {
synchronized (waitDoneLock) {
waitDoneLock.notifyAll();
}
}
}; synchronized (waitDoneLock) {
mCameraHandler.post(unlockRunnable);
try {
waitDoneLock.wait(timeout);
mCameraHandler.removeCallbacks(unlockRunnable);
} catch (InterruptedException ex) {
QLog.v(TAG, "waitDone interrupted");
return false;
}
}
return true;
}