该模型和多进程模型的思想类似,只是把进程换成了线程,因为线程的创建比进程创建开销小。但这并不是说多线程就一定比多进程优秀,进程和线程都有各自的优缺点,具体请自行查阅线程和进程相关的内容,完整代码戳这里
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include "public_head.h"
#define LISTEN_BACKLOG 50
sem_t sem;
void *thread_func(void *arg)
{
int acceptfd = *(int *)arg;
//sem += 1
sem_post(&sem);
int i = 0;
ssize_t readret = 0;
char read_buff[256] = { 0 };
char write_buff[256] = { 0 };
while (1)
{
memset(read_buff, 0, sizeof(read_buff