multithreading - Using Linux C. I use sleep in thread function,but the whole process ends? -
when program goes sleepsleep (1)in thread function ,it ends. have thread?
void send_packet(int sockfd, struct sockaddr_in dest_addr) { int packetsize; while( nsend < max_no_packets) { printf("send %d\n", nsend); nsend++; packetsize=pack(nsend); if( sendto(sockfd,sendpacket,packetsize,0,(struct sockaddr *)&dest_addr,sizeof(dest_addr) )<0 ) { perror("sendto error"); continue; } printf(" %d\n", packetsize); sleep(1); } } int find_host(void * arg) { send_packet(sockfd, present_addr); recv_packet(sockfd, recvpacket, from); pthread_create(&ntid, null, find_host, (void *)&inaddr);
in 1 comment write main thread ends. taken pthread_create man page:
the new thread terminates in 1 of following ways:
[...]
* of threads in process calls exit(3), or main thread performs return main(). causes termination of threads in process.
your main thread needs pthread_join wait sender terminate.
Comments
Post a Comment