I created a thread with pthread_create()
for the execution of a function, inside the function there is a write()
function to send data via socket descriptor. If the write()
function fails to send data (because the socket connection is lost), my thread is terminated.
Can I keep a thread when write()
fails?
This is my code:
void broadcastMsg(char *msg) {
int i=0;
while(1) {
...
...
// My thread terminated from here
if(write(client_database.sock_desc[i], msg, strlen(msg)) < 0) {
client_database.sock_desc[i] = -3;
i++;
continue;
}
i++;
}
}
/* Start thread from this function */
void *cliListener(void *argvp) {
int read_desc;
char buf[MAX_TRANSFER_BUF];
int cli_sock_desc_id = atoi(argvp);
while(1) {
memset(buf, 0, MAX_TRANSFER_BUF);
read_desc = read(client_database.sock_desc[cli_sock_desc_id], buf, MAX_TRANSFER_BUF);
...
...
broadcastMsg(buf);
}
}
int main(void) {
...
...
pthread_t tid_1;
pthread_create(&tid_1, NULL, cliListener, cli_listener_arg);
...
...
}