互斥锁mutex
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(3); //更加明显的观察到是否执行了创建线程的互斥锁 printf("Main unlock \n"); syslog(LOG_DEBUG, "Main unlock \n"); pthread_mutex_unlock(&testlock); sleep(1); printf("Main after sleep \n"); syslog(LOG_DEBUG, "Main after sleep \n"); pthread_join(test_thread, NULL); pthread_mutex_destroy(&testlock); closelog(); return 0; }
小技巧:借助syslog 记录时间 cat /var/log/syslog
输出:
Jul 5 16:23:24 Linux syslog[19982]: Main lock Jul 5 16:23:27 Linux syslog[19982]: Main unlock Jul 5 16:23:27 Linux syslog[19982]: thread Test() Jul 5 16:23:28 Linux syslog[19982]: 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(2); //更加明显的观察到是否执行了创建线程的互斥锁 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(3); //更加明显的观察到是否执行了创建线程的互斥锁 printf("Main unlock \n"); syslog(LOG_DEBUG, "Main unlock \n"); pthread_mutex_unlock(&testlock); sleep(1); 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 0; }
输出为:
Jul 6 10:31:40 Linux syslog[2498]: Main lock Jul 6 10:31:43 Linux syslog[2498]: Main unlock Jul 6 10:31:43 Linux syslog[2498]: thread Test() Jul 6 10:31:45 Linux syslog[2498]: 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; }