在多线程的网络服务器程序中, 对同一个客户端多线程同时发送数据是经常可能发生的事情, 也就是有可能会多
线程的对一个fd调用send/write, 那么这种操作是否需要加锁?并发写套接字是否导致系统缓冲区数据混乱呢? 网上搜
了下,有人说可以写,有人说不能,linux man page也没有说明。 看来需要写程序测试。 写了个server的代码进行
测试。
10个线程同时对一个fd进行write, 看看客户端会收到什么数据。
服务端代码:
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <thread>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
int sockfd;
inline void mysend(const char *s) {
const char *s &#