/* v2 */ #include #include #include #include #include static int cond = 0; static void *thr(void *arg) { int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); const int y = 1; struct sockaddr_in new_conn; socklen_t new_conn_len = sizeof(new_conn); setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y)); listen(fd, SOMAXCONN); /* Signal main thread we are ready and wait until it flips back to zero. */ cond = 1; while(cond) ; accept(fd, (void *)&new_conn, &new_conn_len); printf("finished\n"); return NULL; } int main(void) { pthread_t id; int i; pthread_create(&id, NULL, thr, NULL); /* Wait until subthread has arrived at accept(), then flip back */ while(!cond) ; cond = 0; pthread_cancel(id); pthread_join(id, NULL); return 0; }