这是《C++ 服务器开发精髓》的随书配套源码。
https://item.jd.com/13312460.html
你可以加入本书读者交流群一起交流学习,加微信 easy_coder 拉你进群。
微信搜索【高性能服务器开发】或者微信扫码关注。
《C++ 服务器开发精髓》随书配套源码
这是《C++ 服务器开发精髓》的随书配套源码。
https://item.jd.com/13312460.html
你可以加入本书读者交流群一起交流学习,加微信 easy_coder 拉你进群。
微信搜索【高性能服务器开发】或者微信扫码关注。
链接处代码第47行,调用tasks.empty() 前没有添加锁,而是在判空之后才加锁,这处代码在较高并发下情况下是否存在错误?我尝试将代码修改为每50毫秒产生一个任务,共计500个消费者线程进行消费,似乎没有发生任何错误。
if (tasks.empty())
continue;
pthread_mutex_lock(&mymutex);
pTask = tasks.front();
tasks.pop_front();
pthread_mutex_unlock(&mymutex);
pTask->doTask();
delete pTask;
运行程序,日志中显示极少次数获得了写锁
在 centos 上几乎无法获得读锁,在 mac 上大约每获得5个读锁之后就能获得一个写锁
apples-Mac-mini-1243:~ apple$ g++ --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
apples-Mac-mini-1243:~ apple$
/Users/apple/Desktop/linux_rwlock01/cmake-build-debug/linux_rwlock01
read threadID: read threadID: read threadID: 0x700002340000read threadID: 0x7000023c30000x70000223a0000x7000021b7000, resourceID: 0
, resourceID: 0, resourceID:
read threadID: 0
0x7000022bd000, resourceID: 0, resourceID: 0
write threadID: 0x700002446000, resourceID:1
read threadID: 0x70000223a000, resourceID: 1
read threadID: read threadID: read threadID: 0x7000022bd000, resourceID: 1
0x7000023c3000read threadID: , resourceID: 0x700002340000, resourceID: 11
0x7000021b7000, resourceID: 1
write threadID: 0x700002446000, resourceID:2
read threadID: read threadID: 0x7000022bd0000x70000223a000, resourceID: , resourceID: 2
read threadID: read threadID: 20x7000021b7000
0x7000023c3000, resourceID: , resourceID: read threadID: 2
2
0x700002340000, resourceID: 2
write threadID: 0x700002446000, resourceID:3
read threadID: 0x7000021b7000, resourceID: 3
read threadID: 0x7000022bd000, resourceID: 3
read threadID: read threadID: 0x7000023c3000, resourceID: 30x700002340000
, resourceID: 3
read threadID: 0x70000223a000, resourceID: 3
write threadID: 0x700002446000, resourceID:4
read threadID: 0x7000021b7000, resourceID: 4
read threadID: read threadID: 0x7000023c3000, resourceID: 0x700002340000, resourceID: 4
read threadID: 0x7000022bd000, resourceID: 4
4
[root@localhost cpp-server-development]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@localhost cpp-server-development]# g++ --version
g++ (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[root@localhost linux_rwlock01]# ls
CMakeLists.txt main.cpp README.md test
[root@localhost linux_rwlock01]# pwd
/home/cpp-server-development/ch03/3.5/linux_rwlock01
[root@localhost linux_rwlock01]# g++ -std=c++11 -g -o test main.cpp -lpthread
[root@localhost linux_rwlock01]# ./test
read threadID: 140312867624704, resourceID: 0
read threadID: 140312901195520, resourceID: 0
read threadID: 140312892802816, resourceID: 0
read threadID: 140312884410112, resourceID: 0
read threadID: 140312876017408, resourceID: 0
read threadID: 140312867624704, resourceID: 0
read threadID: 140312901195520, resourceID: 0
read threadID: 140312892802816, resourceID: 0
read threadID: 140312884410112, resourceID: 0
read threadID: 140312876017408, resourceID: 0
read threadID: 140312867624704, resourceID: 0
read threadID: 140312901195520, resourceID: 0
read threadID: 140312892802816, resourceID: 0
read threadID: 140312884410112, resourceID: 0
read threadID: 140312876017408, resourceID: 0
read threadID: 140312867624704, resourceID: 0
read threadID: 140312901195520, resourceID: 0
read threadID: 140312892802816, resourceID: 0
read threadID: 140312884410112, resourceID: 0
read threadID: 140312876017408, resourceID: 0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.