聊天室-线程池-tcp
项目介绍
客户端
1、注册昵称 2、发送消息|接收消息 3、创建登录,注册界面(待补充)
服务端
1、等待客户端连接 2、接收客户端信息 3、通知所有人xxx进入了聊天室 4、接收其他所有消息 5、xxx退出了聊天室
基础准备
这里用到了network和pthreadpool的工具
queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include <stdbool.h>
//基本的队列结构体
typedef struct Queue
{
void** arr;
int cal;
int front;
int rear;
}Queue;
// 创建队列
Queue* create_queue(int cal);
// 销毁队列
void destroy_queue(Queue* queue);
// 入队
void push_queue(Queue* queue,void* arg);
// 出队
void pop_queue(Queue* queue);
// 队空
bool empty_queue(Queue* queue);
// 队满
bool full_queue(Queue* queue);
// 队头
void* front_queue(Queue* queue);
// 队尾
void* rear_queue(Queue* queue);
#endif//QUEUE_H
pthreadpool.h
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include "queue.h"
#include <pthread.h>
//定义一个线程入口函数
typedef void (*EnterFP)(void*);
//定义线程池结构体
typedef struct ThreadPool
{
//线程数量
int thread_cnt;
//线程id
pthread_t* tids;
//存放线程的仓库(队列)
Queue* store;
//线程的入口函数
EnterFP enter;
//定义几个加锁,条件变量
pthread_mutex_t hlock;
pthread_mutex_t tlock;
pthread_cond_t empty;
pthread_cond_t full;
}ThreadPool;
//线程池的创建
ThreadPool* create_threadpool(int thread_cnt,int store_cal,EnterFP enter);
//线程池的启动
void start_threadpool(ThreadPool* thread);
//生产