c - Why is pthread_join not returning? -



c - Why is pthread_join not returning? -

i think pthread_join should homecoming value , allow main thread process code after that. in past experience, work. stuck it. somehow doesn't homecoming , block main thread. or may main thread executes task. don't know why. in code below, cannot reach "thread created2" until terminate client. idea?

int main(int argc, char *argv[]) { int sockfd, port; /* hear on sock_fd, new connection on new_fd */ struct sockaddr_in my_addr; /* address info */ struct sockaddr_in their_addr; /* connector's address info */ socklen_t sin_size; if(signal(sigint, sigintevent) == sig_err) printf("can't grab sigint!"); /* generate socket */ if ((sockfd = socket(af_inet, sock_stream, 0)) == -1) { perror("socket"); exit(1); } if (argc > 1) { port = atoi(argv[1]); } else { port = myport; } /* generate end point */ my_addr.sin_family = af_inet; /* host byte order */ my_addr.sin_port = htons(port); /* short, network byte order */ my_addr.sin_addr.s_addr = inaddr_any; /* auto-fill ip */ /* bzero(&(my_addr.sin_zero), 8); zjl*/ /* 0 rest of struct */ /* bind socket end point */ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \ == -1) { perror("bind"); exit(1); } /* start listnening */ if (listen(sockfd, maxconnections) == -1) { perror("listen"); exit(1); } createpool(maxconnections); /* create node pointer head of list */ head = (node*)malloc(sizeof(node)); openfile(); printf("server starts listnening ...\n"); int new_fd; sin_size = sizeof(struct sockaddr_in); while((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size))) { printf("accepted!\n"); printf("server: got connection!\n"); //tnode* tthread = (tnode*)threaddequeue(); pthread_t pt; printf("got tthread.\n"); if((pthread_create(&pt, null, runservice,(void*)&new_fd)) != 0) { printf("error creating thread."); abort(); } printf("thread created.\n"); if( pthread_join(pt, null) != 0 ) { printf("error joining thread"); abort(); } printf("thread created2.\n"); } exit(1); }

from documentation can read next info pthread_join

the pthread_join() function waits thread specified thread terminate. if thread has terminated, pthread_join() returns immediately. thread specified thread must joinable.

this indicates in case parent thread waiting completion of kid thread pt. kid thread pt executing runservice still not returned/completed. hence parent thread maintain on waiting completion( not returning pthread_join method).

you should seek review code of runservice understand situation.

c linux multithreading sockets pthreads

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -