Hi All,
This compiles and works OK but I want to check I've done it right.
I have no control over the definition of MYSTRUCT but I do have control over the rest. There is one MYSTRUCT handle for all processes to share and use thread safely.
I have to pass a copy of the client descritor and the shared MYSTRUCT handle to each connection thread.
Problem is the server is falling over after about a day.
Thanks for your help, rotis23
Code:
//in /usr/include header file
typedef void* MYSTRUCT;
//in local header file
struct thread_data
{
MYSTRUCT mainstruct;
int client_socket;
};
//in main program
int serverloop(MYSTRUCT working_struct)
{
struct thread_data *myTD = NULL;
//initiased other stuff
for(;;)
{
cli_size = sizeof(cli);
client = accept(s,(struct sockaddr*)&cli,(socklen_t*)&cli_size);
if(client == -1)
{
//error handling
}
myTD = (struct thread_data*)malloc(sizeof *myTD);
myTD->mainstruct = working_struct;
myTD->client_socket = client;
res = pthread_create(&tid,&thread_attr,HandleRequest,myTD);
if (res != 0)
{
//error handling
}
pthread_detach(tid);
}
//close stuff
}
void *HandleRequest(void *argp)
{
struct thread_data *myTTD;
myTTD = (struct thread_data*)argp;
//do stuff with (int)myTTD->client_socket and (MYSTRUCT)myTTD->mainstruct
close((int)myTTD->client_socket);
free(myTTD);
pthread_exit(NULL);
return NULL;
}