linux 应用程序 mutex,Linux mutex 编程

本文通过三个示例展示了在多线程环境下,未使用、使用mutex以及使用std::lock_guard对共享资源进行读写操作的情况。未使用互斥锁时,结果不确定;引入互斥锁后,保证了读写的串行化,虽然程序运行速度变慢,但结果正确。使用std::lock_guard进一步简化了互斥锁的管理,确保了资源的安全访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两个并行运行的程序片断,如线程,同时读写同一块内存,其结果是不确定的。这时候我们需要互斥锁,保证一个线程先读写完,然后另一个线程才可以读写。

1 没有使用mutex的例子

$ cat mutex-example-1.c

#include //ptrinf

#include //pthread_xxx

#include //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 --

每一次的运行结果都可能不一样。

$ 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 //ptrinf,perror

#include //exit

#include //pthread_xxx

#include //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 --

因为是串行读写,程序运行慢了很多,但结果正确了。

3 使用std::lock_guard

$ cat mutex-example-3.cpp

#include //ptrinf,perror

#include //exit

#include //pthread_xxx

#include //sleep

#include //std::mutex

int global_para = 0;

std::mutex g_mutex;

void *do_sth(void *p)

{

long i = (long)p;

const std::lock_guard<: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 --

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值