随笔分类 -  Linux C/C++

1

摘要:仅使用互斥保护每个成员函数 问题 1 尽管运用互斥保护共享数据,条件竞争仍然无法避免,考虑如下代码: if (!q.empty()) { const int value = q.front(); q.pop(); do_something(value); } 在 empty() 和 front() 阅读全文

posted @ 2023-04-20 16:43 lyc2002 阅读(323) 评论(0) 推荐(0)

摘要:~~~bash # 在 remote 端 sudo apt install openssh-server # 在 local 端 VSCode 点击新建 ssh,输入 ssh @ -A,修改 config,点击 connect # 在 local 端 cmd 运行 ssh-keygen -t rsa 阅读全文

posted @ 2023-04-06 22:12 lyc2002 阅读(81) 评论(0) 推荐(0)

摘要:能解决什么问题? 需求:在给定数组中找目标值,返回目标值所在下标 方案一:返回 magic number,-1 表示给定数组不存在目标值,>= 0 表示目标值的下标 int find_target(int arr[], size_t sz, int tar); 方案二:返回 pair,bool 指示 阅读全文

posted @ 2023-03-29 21:39 lyc2002 阅读(129) 评论(0) 推荐(0)

摘要:介绍 #include <sys/uio.h> ssize_t writev(int fd, const struct iovec *iov, int iovcnt); ssize_t readv(int fd, const struct iovec *iov, int iovcnt); struc 阅读全文

posted @ 2023-03-01 11:19 lyc2002 阅读(48) 评论(0) 推荐(0)

摘要:介绍 #include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); 功能:将文件映射到内存中 参数: addr:允许用户使用某个特定的地址作为这段内存的起 阅读全文

posted @ 2023-03-01 10:08 lyc2002 阅读(49) 评论(0) 推荐(0)

摘要:介绍 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat(const char *pathname, struct stat *statbuf); struct stat { dev_t st_dev; 阅读全文

posted @ 2023-03-01 09:41 lyc2002 阅读(34) 评论(0) 推荐(0)

摘要:介绍 #include <sys/types.h> #include <sys/socket.h> int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); 功能:设置套接字可选 阅读全文

posted @ 2023-02-27 19:34 lyc2002 阅读(41) 评论(0) 推荐(0)

摘要:介绍 #include <signal.h> int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); 功能:为一个信号设置处理函数 参数: signum:要捕获的信号类型 act:新的信号处理 阅读全文

posted @ 2023-02-27 11:23 lyc2002 阅读(42) 评论(0) 推荐(0)

摘要:介绍 异步线程只有一个,由主线程充当,它负责监听所有 socket 上的事件 如果监听 socket 上发生读事件(有新的连接请求到来),主线程就接受得到新的连接 socket,然后往 epoll 内核事件表上注册该连接 socket 上的读写事件 如果连接 socket 上发生读写事件(客户端和服 阅读全文

posted @ 2023-02-25 17:40 lyc2002 阅读(61) 评论(0) 推荐(0)

摘要:#ifndef LOCKER_H #define LOCKER_H #include <pthread.h> #include <semaphore.h> #include <exception> // 封装信号量 class sem { public: sem() { if (sem_init(& 阅读全文

posted @ 2023-02-25 15:04 lyc2002 阅读(21) 评论(0) 推荐(0)

摘要:介绍 初始化 #include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); 功能:初始化信号量 参数: sem:信号量变量地址 pshared:0,线程同步;非 0,进程同步 value:初始化信号 阅读全文

posted @ 2023-02-24 13:56 lyc2002 阅读(62) 评论(0) 推荐(0)

摘要:介绍 #include <pthread.h> pthread_cond_t cond; int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr); int pthrea 阅读全文

posted @ 2023-02-23 20:00 lyc2002 阅读(51) 评论(0) 推荐(0)

摘要:介绍 #include <pthread.h> int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr); int pthread_rwlock_rdlo 阅读全文

posted @ 2023-02-23 16:18 lyc2002 阅读(36) 评论(0) 推荐(0)

摘要:介绍 初始化锁 #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); 功能:初始化互斥锁 参数: mutex:互斥 阅读全文

posted @ 2023-02-23 15:31 lyc2002 阅读(43) 评论(0) 推荐(0)

摘要:介绍 获得线程号 #include <pthread.h> pthread_t pthread_self(void); 功能:得到线程 id 参数:无 返回值:调用此函数的线程 id 创建线程 #include <pthread.h> int pthread_create(pthread_t *th 阅读全文

posted @ 2023-02-23 13:12 lyc2002 阅读(40) 评论(0) 推荐(0)

摘要:select 介绍 #include <sys/select.h> #include <sys/time.h> int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *tim 阅读全文

posted @ 2023-02-21 22:45 lyc2002 阅读(32) 评论(0) 推荐(0)

摘要:目录结构 CMakeLists.txt 编写 # 指定 CMake 的最小版本要求 cmake_minimum_required(VERSION 3.0) # 指定工程名称 project(soldier) # 显式定义变量 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAG 阅读全文

posted @ 2023-02-14 10:39 lyc2002 阅读(47) 评论(0) 推荐(0)

摘要:编译注意 程序要想使用 gdb 调试,编译时要加上 -g 传入参数 设置传入参数:set args 显示传入参数:show args 代码 查看代码:list 文件名:函数名或行号 设置查看代码行数:set listsize 断点 打断点:break 文件名:函数名或行号 删除断点:delete 端 阅读全文

posted @ 2023-01-10 15:11 lyc2002 阅读(36) 评论(0) 推荐(0)

摘要:简单示例 src = $(wildcard ./*.c) objs = $(patsubst %.c, %.o, $(src)) target = app $(target):$(objs) $(CC) $^ -o $@ %.o:%.c $(CC) -c $< -o $@ .PHONY:clean 阅读全文

posted @ 2023-01-10 13:27 lyc2002 阅读(36) 评论(0) 推荐(0)

摘要:目录结构 静态库制作与使用 cd src # 汇编生成.o文件 gcc add.c div.c mult.c sub.c -c -I../include # 生成静态库 ar rcs libcalc.a add.o div.o mult.o sub.o mv libcalc.a ../lib/ cd 阅读全文

posted @ 2023-01-08 17:32 lyc2002 阅读(48) 评论(0) 推荐(0)

1