概述
一 代码
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <string.h>
#include <cstdlib>
int gcn = 0;
pthread_mutex_t mutex;
//共享锁
pthread_rwlock_t rwlock;
//读写锁
void *thread_1(void *arg) {
int j;
volatile int a;
for (j = 0; j < 10000000; j++) {
pthread_mutex_lock(&mutex);
// 上锁
a = gcn;
//只读全局变量gcn
pthread_mutex_unlock(&mutex);
//解锁
}
pthread_exit((void *)0);
}
void *thread_2(void *arg) {
int j;
volatile int b;
for (j = 0; j < 10000000; j++) {
pthread_mutex_lock(&mutex);
// 上锁
b = gcn;
//只读全局变量gcn
pthread_mutex_unlock(&mutex);
//解锁
}
pthread_exit((void *)0);
}
void *thread_3(void *arg) {
int j;
volatile int a;
for (j = 0; j < 10000000; j++) {
pthread_rwlock_rdlock(&rwlock);
// 上锁
a = gcn;
//只读全局变量gcn
pthread_rwlock_unlock(&rwlock);
//解锁
}
pthread_exit((void *)0);
}
void *thread_4(void *arg) {
int j;
volatile int b;
for (j = 0; j < 10000000; j++) {
pthread_rwlock_rdlock(&rwlock);
// 上锁
b = gcn;
//只读全局变量gcn
pthread_rwlock_unlock(&rwlock);
//解锁
}
pthread_exit((void *)0);
}
int mutextVer(void)
{
int j,err;
pthread_t th1, th2;
struct timeval start;
clock_t t1,t2;
struct timeval end;
pthread_mutex_init(&mutex, NULL); //初始化互斥锁
gettimeofday(&start,NULL);
err = pthread_create(&th1, NULL, thread_1, (void *)0);
if (err != 0) {
printf("create new thread error:%sn", strerror(err));
exit(0);
}
err = pthread_create(&th2, NULL, thread_2, (void *)0);
if (err != 0) {
printf("create new thread error:%sn", strerror(err));
exit(0);
}
err = pthread_join(th1, NULL);
if (err != 0) {
printf("wait thread done error:%sn", strerror(err));
exit(1);
}
err = pthread_join(th2, NULL);
if (err != 0) {
printf("wait thread done error:%sn", strerror(err));
exit(1);
}
gettimeofday(&end,NULL);
pthread_mutex_destroy(&mutex); //销毁互斥锁
long long total_time=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);
total_time /= 1000;
printf("total mutex time is %lld msn",total_time);
return 0;
}
int rdlockVer(void)
{
int j,err;
pthread_t th1, th2;
struct timeval start;
clock_t t1,t2;
struct timeval end;
pthread_rwlock_init(&rwlock, NULL); //初始化读写锁
gettimeofday(&start,NULL);
err = pthread_create(&th1, NULL, thread_3, (void *)0);
if (err != 0) {
printf("create new thread error:%sn", strerror(err));
exit(0);
}
err = pthread_create(&th2, NULL, thread_4, (void *)0);
if (err != 0) {
printf("create new thread error:%sn", strerror(err));
exit(0);
}
err = pthread_join(th1, NULL);
if (err != 0) {
printf("wait thread done error:%sn", strerror(err));
exit(1);
}
err = pthread_join(th2, NULL);
if (err != 0) {
printf("wait thread done error:%sn", strerror(err));
exit(1);
}
gettimeofday(&end,NULL);
pthread_rwlock_destroy(&rwlock); //销毁互斥锁
long long total_time=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);
total_time /= 1000;
printf("total rwlock time is %lld msn",total_time);
return 0;
}
int main()
{
mutextVer();
rdlockVer();
return 0;
}
二 运行
[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
total mutex time is 505 ms
total rwlock time is 693 ms
三 说明
即使在读情况下,读写锁依然比互斥锁速度慢。那是不是说读写锁没什么作用呢?不是这样的,虽然速度上可能不如互斥锁,但并发性好,并发性对于用户体验非常重要。对于并发要求高的地方,应该优先考虑读写锁。
最后
以上就是殷勤小熊猫为你收集整理的C++的互斥锁和读写锁速度比较实战的全部内容,希望文章能够帮你解决C++的互斥锁和读写锁速度比较实战所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复