GithubHelp home page GithubHelp logo

balloonwj / mybooksources Goto Github PK

View Code? Open in Web Editor NEW
198.0 5.0 94.0 46.27 MB

《C++ 服务器开发精髓》随书配套源码

C 80.61% C++ 3.64% Makefile 0.67% CMake 0.05% Shell 0.56% Perl 2.03% M4 0.98% XSLT 0.01% Python 0.08% HTML 3.23% Roff 0.08% CSS 0.02% Batchfile 0.01% Lua 0.13% Ruby 0.23% Tcl 7.66% Smarty 0.01%

mybooksources's Introduction

mybooksources's People

Contributors

balloonwj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mybooksources's Issues

linux信号量案例中47行任务队列tasks判空是否需要添加锁保护?

链接处代码第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;

运行代码片段3-5-4,并非如书本所说很难获得写锁,我的实验结果为: 在 centos 上几乎无法获得读锁,在 mac 上大约每获得5个读锁之后就能获得一个写锁

期待行为

运行程序,日志中显示极少次数获得了写锁

实际行为

在 centos 上几乎无法获得读锁,在 mac 上大约每获得5个读锁之后就能获得一个写锁

复现路径

mac

  • mac 环境:
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

Linux

  • Centos 环境:
[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.
  • Centos 复现步骤:
[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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.