兩個(gè)并行運(yùn)行的程序片斷,如線程,同時(shí)讀寫同一塊內(nèi)存,其結(jié)果是不確定的。這時(shí)候我們需要互斥鎖,保證一個(gè)線程先讀寫完,然后另一個(gè)線程才可以讀寫。
1 沒(méi)有使用mutex的例子
$ cat mutex-example-1.c
#include <stdio.h> //ptrinf
#include <pthread.h> //pthread_xxx
#include <unistd.h> //sleep
int global_para = 0;
void *do_sth(void *p)
{
long i = (long)p;
printf("thread %d - start to read\n", i);
int temp = global_para;
sleep(2);
printf("thread %d - end of reading\n", i);
printf("thread %d - start to write\n", i);
sleep(2);
global_para = temp + (int)i;
printf("thread %d - end of reading\n", i);
}
int main()
{
pthread_t t1, t2;
pthread_create(&t1, 0, do_sth, (void *)1);
pthread_create(&t2, 0, do_sth, (void *)2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("-- global_para is %d --\n", global_para);
}
# gcc mutex-example-1.c -o mutex-example-1 -lpthread && ./mutex-example-1
thread 2 - start to read
thread 1 - start to read
thread 1 - end of reading
thread 1 - start to write
thread 2 - end of reading
thread 2 - start to write
thread 1 - end of reading
thread 2 - end of reading
-- global_para is 2 --
每一次的運(yùn)行結(jié)果都可能不一樣。
$ gcc mutex-example-1.c -o mutex-example-1 -lpthread && ./mutex-example-1
thread 2 - start to read
thread 1 - start to read
thread 2 - end of reading
thread 2 - start to write
thread 1 - end of reading
thread 1 - start to write
thread 2 - end of reading
thread 1 - end of reading
-- global_para is 1 --
2 使用mutex的例子
$ cat mutex-example-2.c
#include <stdio.h> //ptrinf,perror
#include <stdlib.h> //exit
#include <pthread.h> //pthread_xxx
#include <unistd.h> //sleep
int global_para = 0;
pthread_mutex_t lock;
void die(char *s)
{
perror(s);
exit(1);
}
void *do_sth(void *p)
{
long i = (long)p;
pthread_mutex_lock(&lock);
printf("thread %d - start to read\n", i);
int temp = global_para;
sleep(2);
printf("thread %d - end of reading\n", i);
printf("thread %d - start to write\n", i);
sleep(2);
global_para = temp + (int)i;
printf("thread %d - end of reading\n", i);
pthread_mutex_unlock(&lock);
}
int main()
{
pthread_t t1, t2;
if (pthread_mutex_init(&lock, NULL) != 0)
{
die("pthread_mutex_init()");
}
pthread_create(&t1, 0, do_sth, (void *)1);
pthread_create(&t2, 0, do_sth, (void *)2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&lock);
printf("-- global_para is %d --\n", global_para);
}
$ gcc mutex-example-2.c -o mutex-example-2 -lpthread && ./mutex-example-2
thread 2 - start to read
thread 2 - end of reading
thread 2 - start to write
thread 2 - end of reading
thread 1 - start to read
thread 1 - end of reading
thread 1 - start to write
thread 1 - end of reading
-- global_para is 3 --
因?yàn)槭谴凶x寫,程序運(yùn)行慢了很多,但結(jié)果正確了。
3 使用std::lock_guard
$ cat mutex-example-3.cpp
#include <stdio.h> //ptrinf,perror
#include <stdlib.h> //exit
#include <pthread.h> //pthread_xxx
#include <unistd.h> //sleep
#include <mutex> //std::mutex
int global_para = 0;
std::mutex g_mutex;
void *do_sth(void *p)
{
long i = (long)p;
const std::lock_guard<std::mutex> lock(g_mutex);
printf("thread %d - start to read\n", i);
int temp = global_para;
sleep(2);
printf("thread %d - end of reading\n", i);
printf("thread %d - start to write\n", i);
sleep(2);
global_para = temp + (int)i;
printf("thread %d - end of reading\n", i);
return NULL;
}
int main()
{
pthread_t t1, t2;
pthread_create(&t1, 0, do_sth, (void *)1);
pthread_create(&t2, 0, do_sth, (void *)2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("-- global_para is %d --\n", global_para);
return 0;
}
$ g++ mutex-example-3.cpp -o mutex-example-3 -lpthread && ./mutex-example-3
thread 2 - start to read
thread 2 - end of reading
thread 2 - start to write
thread 2 - end of reading
thread 1 - start to read
thread 1 - end of reading
thread 1 - start to write
thread 1 - end of reading
-- global_para is 3 --
參考
http://www.thegeekstuff.com/2012/05/c-mutex-examples/?refcom
https://en.cppreference.com/w/cpp/thread/lock_guard