All pastes #423152 Raw Edit

jengelh

public c v1 · immutable
·published 2007-04-03 22:56 UTC
rendered paste body
/* v2 */#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <pthread.h>#include <stdio.h>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;}