main函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <list.h>
#include <queue.h>
#define ENTER_CRITICAL_AREA(mutex) do{pthread_mutex_lock(mutex);}while(0)
#define QUIT_CRITICAL_AREA(mutex) do{pthread_mutex_unlock(mutex);}while(0)
unsigned pthread_index;
LIST_LINK *end_list = NULL;
//typedef struct thread_node
//{
// pthread_t tid; //线程id号
// char name[256]; //线程名字 ,必须唯一
// Que *amil_head, *mail_tail;
// th_fun th; //线程函数
//}TH_NODE;
typedef struct mail_box_system
{
pthread_mutex_t mutex; //保护邮件系统
LIST_LINK *thread_list;
}MBS;
MBS* mbs;
int send_msg(MBS*msb,char*recvname,DATATYPE data);
int recv_msg(MBS*msb,char*sendname,DATATYPE data);
MBS* create_mail_box_system()
{
MBS *temp =(MBS*)malloc(sizeof(MBS));
if(NULL == temp)
{
perror("create_mail_box_system mutex malloc failure\n");
return NULL;
}
int ret = pthread_mutex_init(&temp->mutex,NULL);
if(0 != ret)
{
perror("create_mail_box_system mutex init failure\n");
return NULL;
}
temp->thread_list = malloc(sizeof(LIST_LINK));
// memset(temp->thread_list, 0, sizeof(LIST_LINK));
temp->thread_list->next = NULL;
printf("mail box create ok!! \n");
return temp;
}
int destroy_mail_box_system(MBS*mbs)
{
pthread_mutex_destroy(&mbs->mutex);
LIST_LINK *temp = NULL;
LIST_LINK *find = mbs->thread_list;
while(find != NULL)
{
temp = find;
find = find->next;
free(temp);
}
free(find);
return 0;
}
int register_to_mail_system(MBS *mbs,char name[],th_fun th)
{
LIST_LINK* temp = malloc(sizeof(LIST_LINK));
if(NULL == temp)
{
perror("register to mail malloc \n");
return -1;
}
strcpy(temp->elem.name ,name);
temp->elem.th = th;
init_que(temp);