
c++
cwdben
Do not go gentle into that good night
展开
-
c++11 实现设计一个多线程打印程序,第i个线程只打印i-1数字
#include <thread>#include <iostream>#include <mutex>#include <condition_variable>#include <vector>#include <functional>using namespace std;mutex print_mutex;condition_variable cond;string s = "";int flag = 0;原创 2022-03-09 22:12:57 · 1180 阅读 · 0 评论 -
解决webbench运行时卡住的问题
问题描述:使用webbench进行压力测试的时候,在baidu上一般来说正常。在自己的webserver上测试,有时会卡住而没有返回值。在修改为长连接的时候尤其会发生。如下图必须要ctrl+c终止。github上也有这样的问题长连接问题 · Issue #31 · linyacool/WebServer解决思路:找到可能被卡住的地方测试时修改了部分源码进行打印,看每次1000个子进程中被阻塞了的进程数判断。1:代码健壮性增加:从while (s = Socket(host, port)原创 2021-12-23 22:06:25 · 1014 阅读 · 0 评论 -
修改webbench支持长连接
概述我们知道HTTP协议采用“请求-应答”模式,当使用普通模式,即非KeepAlive模式时,每个请求/应答客户和服务器都要新建一个连接,完成之后立即断开连接(HTTP协议为无连接的协议);当使用Keep-Alive模式(又称持久连接、连接重用)时,Keep-Alive功能使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接。http 1.0中默认是关闭的,需要在http头加入"Connection: Keep-Alive",才能启用Keep-原创 2021-12-23 20:35:40 · 531 阅读 · 0 评论 -
webbench详解与源码注释
webbench总共就是三个文件,makefile,socket.c,webbench.cmakefile完成编译,sudo make install安装到 /usr/local/bin目录下面。其实 make install命令就是把可执行文件拷贝到/bin目录下面。INSTALL(1) User Commands INSTALL(1)NAME install - copy files and原创 2021-12-23 20:18:24 · 1020 阅读 · 0 评论 -
python与c++程序通过mqtt通信
mqtt安装与基本测试#安装mosquitto mqtt#引入仓库sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa#更新源sudo apt-get update#安装mosquittosudo apt install mosquitto#安装客户端sudo apt-get install mosquitto-clients######测试#一个终端开启sub mqttmosquitto_sub -h localhost原创 2021-12-11 15:18:47 · 3124 阅读 · 1 评论 -
More Effective C++读书笔记
More Effective C++读书笔记基础议题条款1:区别指针与引用条款2:尽量使用C++风格的类型转换条款3:不要对数组使用多态条款4:非必要不提供默认构造函数操作符条款5:谨慎定义类型转换函数条款6:自增(increment)、自减(decrement)操作符前缀形式与后缀形式的区别条款7:不要重载”&&”, “||”,或”,”条款8:理解各种不同含义的new和delete异常条款9:使用析构函数防止资源泄漏条款10:在构造函数中防止资源泄漏条款11:禁止异常信息(exceptio原创 2021-07-09 17:40:00 · 409 阅读 · 0 评论 -
effective c++读书笔记
effective c++读书笔记一级目录二级目录三级目录Charpter 1. 让自己习惯C++条款01: 视C++为一个语言联邦条款02: 尽量以const,enum,inline替换#define条款03: 尽可能使用const条款04: 确定对象被使用前已先被初始化Charpter 2. 构造/析构/赋值运算条款05: 了解C++默默编写并调用哪些函数条款06: 若不想使用编译器自动生成的函数,就该明确拒绝条款07: 为多态基类声明virtual析构函数条款08: 别让异常逃离析构函数条款09: 绝原创 2021-06-29 11:43:45 · 366 阅读 · 2 评论 -
基于operator new重载的小型内存池
基于重载的小型内存池#include <cstddef>#include <iostream>using namespace std;//ref. C++Primer 3/e, p.765//per-class allocator class Screen {public: Screen(int x) : i(x) { }; int get() { return i; } void* operator new(size_t); voi原创 2021-06-08 10:12:51 · 301 阅读 · 0 评论 -
::operator new/::operator delete的重载
::operator new/::operator delete是可以重载的。new delete是不可以重载的。他们的作用范围都是全命名空间,需要小心重载//----------------------------------------------------#include <cstdlib> //malloc, free#include <iostream> #include <complex>using namespace std;void* myA原创 2021-06-07 21:58:42 · 477 阅读 · 0 评论 -
c++ Startup
c++ Startup问题c++的进入点在哪里?–启动函数,但是可以被gcc设定什么代码比`main()更早被执行?–启动函数在main前面的函数什么代码在main()之后被执行?–析构那几个内存空间内存的heap结构如何?–空闲链表程序进入点c++进入点并不是main,在linux下可以由gcc的-e参数指定。可以认为在这个参数下,启动函数变成了我们自己写的blabla。而且main就根本不会被调用。$ cat entrypoint.c int blabla(){ printf("Ye原创 2021-06-06 20:12:27 · 842 阅读 · 2 评论 -
空struct的size
在看stl的源码分析的时候,仿函数是一个仅有函数的struct,这样的成员是否占用空间。理论上应该是0,侯捷老师说因为编译器实现的原因会是1。因为当时离现在比较久了,就测试一下#include <iostream>using namespace std;template <class Arg,class Result>struct my_unary_function{ typedef Arg arguement_type; typedef Result re原创 2021-06-03 20:37:42 · 279 阅读 · 1 评论 -
new/delete 与 operator new/delete详解
new有三种调用形式//throwing (1) void* operator new (std::size_t size);//nothrow (2) void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;//placement (3) void* operator new (std::size_t size, void* ptr) noexcept;(1) throw原创 2021-05-21 11:06:04 · 313 阅读 · 0 评论 -
利用shm_open的共享内存在无亲缘进程间通信例程
头文件定义储存在共享内存的结构体,以及一些宏定义#include <unistd.h>#include <stdint.h>struct shm_ect_data { /* struct stored in shared memory */ int32_t targetPosition; //电机的目标位置 int32_t targetVelocity; //电机当前运行速度 int8_t opModeSet; //电机运行模原创 2021-05-14 16:26:47 · 322 阅读 · 0 评论 -
Linux下使用信号处理,终端执行ctrl+c后的SIGINT信号
很多时候我们写的程序是一个一直运行的,调试的时候,我们直接按ctrl+c终止程序,那么在while后面的一些close函数的调用,在没有设置中断函数的时候,是不会被执行的。错误例子#include <iostream>#include <unistd.h>using namespace std;int main(){ while(1){ //sleep(1); } printf("closing\n");}编译运行,ct原创 2021-05-13 20:01:46 · 2745 阅读 · 0 评论 -
基于共享内存和信号触发的聊天室服务器方案(包括多进程处理)
源码部分借鉴自游双的《Linux高性能服务器编程》完整源码参见github-chatroom1.全局配置client_data#define BUFFER_SIZE 64#define USER_LIMIT 5#define FD_LIMIT 65535#define MAX_EVENT_NUMBER 1024#define PROCESS_LIMIT 65535int sig_pipefd[2];bool stop_child = false; //子进程的标志位struct cl原创 2021-05-12 17:12:27 · 336 阅读 · 0 评论 -
关于epoll_create的size,以及创建出的句柄epollfd是否需要close探讨
sizeepoll_creat的函数圆形如下:int epoll_create(int size);其中,size的含义值得琢磨。首先参考官方给出的资料:gun库里的注释/* Creates an epoll instance. Returns an fd for the new instance. The "size" parameter is a hint specifying the number of file descriptors to be associated wit原创 2021-05-11 22:11:05 · 953 阅读 · 0 评论 -
基于poll的聊天室程序
源码部分借鉴自游双的《Linux高性能服务器编程》客户端#include <sys/socket.h>#include <sys/types.h>#include <fcntl.h>#include <unistd.h>#include <assert.h>#include<stdio.h>#include <stdlib.h>#include <netinet/in.h>#include &原创 2021-05-10 08:58:02 · 341 阅读 · 1 评论 -
epoll的ET模式和LT模式的例程与分析
例程来自游双的《Linux高性能服务器编程》#include <sys/socket.h>#include <sys/types.h>#include <sys/epoll.h>#include <fcntl.h>#include <unistd.h>#include <assert.h>#include<stdio.h>#include <stdlib.h>#include <neti原创 2021-05-06 19:12:59 · 222 阅读 · 0 评论 -
aio_read()的例程详解
man手册AIO(7) Linux Programmer's Manual AIO(7)NAME aio - POSIX asynchronous I/O overviewDESCRIPTION The POSIX asynchronous I/O (AIO) interface allows applications to ini‐ tiate one or more I/O原创 2021-05-05 21:59:36 · 1890 阅读 · 0 评论 -
syslog()的使用和例程
man手册命令行man openlog即可查看;写的非常详细,看完其实就懂了。NAME closelog, openlog, syslog, vsyslog - send messages to the system loggerSYNOPSIS #include <syslog.h> void openlog(const char *ident, int option, int facility); void syslog(in原创 2021-05-05 09:35:30 · 1843 阅读 · 0 评论 -
assert()指针的用法
非空指针,可以过assert#include <assert.h>#include <iostream>using namespace std;int main(int argc, char const *argv[]){ int *p; int a; p= &a; //p=nullptr; assert(p); cout<<"ok"<<endl;原创 2021-04-23 13:54:24 · 1107 阅读 · 0 评论 -
c++迭代器的地址问题以及&*v.begin() 用法
看个项目用到这种用法,写了个测试程序测了一下。不能直接取迭代器的地址对迭代器加*,即获得该迭代器所指的元素对迭代器加*,再加&,就是该元素的地址#include <vector>#include <iostream>using namespace std;int main(int argc, char const *argv[]){ vector<int> v(10,0); //cout<<"&原创 2021-04-22 22:50:21 · 2310 阅读 · 0 评论 -
从sockaddr 中获得ip地址并打印
参考链接struct addrinfo *res; // populated elsewhere in your codestruct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;char ipAddress[INET_ADDRSTRLEN];inet_ntop(AF_INET, &(ipv4->sin_addr), ipAddress, INET_ADDRSTRLEN);printf("The IP ad原创 2021-04-14 20:49:45 · 4737 阅读 · 0 评论