int main(int argc, char **argv) { int listenfd, *connfd, port, clientlen, i; struct sockaddr_in clientaddr; pthread_t tid; port_queue prod_var; /* Check command line args */ if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } port = atoi(argv[1]); connfdqp = queueInit (); prod_var.port = port; prod_var.q = connfdqp; Pthread_create(&tid, NULL, producer, &prod_var); printf("Producer thread created %u\n", (unsigned int) tid); Pthread_detach(tid); printf ("Producer thread detached\n"); for (i = 0; i < MAXTHREAD; i++) { Pthread_create(&tid, NULL, consumer, connfdqp); printf("Consumer thread created %u\n", (unsigned int) tid); Pthread_detach(tid); printf("Consumer thread detached\n"); } printf("Main thread exited\n"); Pthread_exit(NULL); }
template<bool check_depleted> void abc_para_test() { concat<char> str , *dep1 = new concat<char> , *dep2 = new concat<char> ; assert(!str.ready()); assert(!dep1->closed()); assert(!dep2->closed()); pthread_t thread[2]; Pthread_create(&thread[0], NULL, abc, dep1); Pthread_detach(thread[0]); Pthread_create(&thread[1], NULL, abc, dep2); Pthread_detach(thread[1]); str.give(dep1); str.give(dep2); str.close(); for (int i = 0; i < 2; i++) { for (char let = 'a'; let <= 'z'; let++) { if (check_depleted) { assert(!str.depleted()); assert(str.ready()); } assert(str.get() == let); } } assert(!str.ready()); assert(str.depleted()); assert(str.closed()); }
static void * time_echo(void *sockfd) { int fd,maxfdp; fd_set rset,wset; Pthread_detach(pthread_self()); char buf[MAXLINE]; time_t ticks; struct timeval tv; fd=(int)sockfd; FD_ZERO(&rset); FD_ZERO(&wset); for( ; ; ){ FD_SET(fd, &rset); maxfdp = fd + 1; tv.tv_sec=5; tv.tv_usec=0; ticks = time(NULL); snprintf(buf, sizeof(buf), "%.24s\r\n",ctime(&ticks)); Write(fd, buf, strlen(buf)); Select(maxfdp, &rset, NULL, NULL, &tv); if(FD_ISSET(fd, &rset)){ printf("time client termination: EPIPE error detected\n"); FD_CLR(fd,&rset); Close(fd); return NULL; } } Close(fd); return (NULL); }
static void *echosel(void *arg)/* Echo Thread*/ { int connfd,wr,fileflags; ssize_t n; char buf[MAXLINE]; connfd = *((int *) arg); free(arg); Pthread_detach(pthread_self());/* Detach Thread*/ back: while ( (n = readline(connfd, buf, MAXLINE)) > 0)/*Read data from Client*/ { wrback: wr= writen(connfd, buf, n);/*write back data from Client*/ } if(wr<0&& errno == EPIPE) /*Handling Write Error due to EPIPE */ { Fputs(" Echo Client termination:EPIPE error detected\n", stdout); Close(connfd); return; } else if (wr<0 && errno == EINTR)/*Handling Write Error due to EINTR */ { goto wrback; } else if (wr<0)/*Handling Write Error */ { Fputs("Echo Client termination:Write error detected,error occured due to\n", stdout); Fputs(strerror(errno),stdout); Fputs("\n",stdout); Close(connfd); return; } if (n < 0 && errno == EINTR)/*Handling read Error due to EINTR */ { goto back; } else if (n < 0) { /*Handling read Error */ Fputs(" Echo Client termination:socket read returned with value -1, error occured due to\n", stdout); Fputs(strerror(errno),stdout); Fputs("\n",stdout); Close(connfd); return; } else /*Handling client Termination */ { Fputs(" Echo Client termination:socket read returned with value 0\n", stdout); Close(connfd); return; } Close(connfd); return (NULL); }
static void *time_serv(void *arg) { printf("This is the new window for time service\n"); Pthread_detach(pthread_self()); int sockfd=*((int*)arg); free(arg); time_t ticks; char buff[MAXLINE]; while(1) { if(Readable_timeo(sockfd, 5)==0) { ticks=time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(sockfd, buff, strlen(buff)); printf("Current time is sent to client\n"); } else break; } printf("This time service is terminated\n"); Close(sockfd); return NULL; }
void *serv_thread(void *vargp) { int res; char buf[SHORT_BUF]; int connfd; Pthread_mutex_lock(&psyc); connfd = *(int *)vargp; Pthread_mutex_unlock(&psyc); Pthread_detach(pthread_self()); #ifdef DEBUG printf("one service thread is created...\n"); #endif for(;;){ res = recv(connfd, buf, SHORT_BUF, 0); if (res == -1) { delay_sys("fail to recv\n"); break; } else if (res == 0) { delay_sys("the connect broken\n"); break; } buf[res] = 0; printf("Have receve the commind : %s\n", buf); if( tasks(buf, connfd) < 0) break; } printf("cilent %d is left...\n", connfd); close(connfd); return NULL; }
void *thread(void *vargp) /* thread routine */ { Pthread_detach(pthread_self()); printf("Hello, world!\n"); while(1); return NULL; }
static void* doit(void* arg) { Pthread_detach(pthread_self()); str_echo((int)arg); Close((int)arg); return NULL; }
/* * thread function */ void *thread_func(void *vargp) { Pthread_detach(pthread_self()); while (1) { int connfd = sbuf_remove(&sbuf); doit(connfd); Close(connfd); } }
void *doit_thread(void *vargp){ int connfd = *((int *)vargp); Pthread_detach(pthread_self()); Free(vargp); doit(connfd); Close(connfd); return NULL; }
/* * thread - using for process request from client * (please see slides of Lecture: Concurrency page 46 for more detail) */ void *thread(void *vargp) { int connfd = *((int *)vargp); Pthread_detach(pthread_self()); // detach! important Free(vargp); doit(connfd); // see tiny.c for more info Close(connfd); return NULL; }
static void * doit(void *arg) { Pthread_detach(pthread_self()); str_echo((int) arg); /* same function as before */ Close((int) arg); /* done with connected socket */ return(NULL); }
void* doit(void* arg) { void web_child(int); Pthread_detach(pthread_self()); web_child((int)arg); Close((int)arg); return (NULL); }
void *thread(void *vargp) { Pthread_detach(pthread_self()); while (1) { int connfd = sbuf_remove(&sbuf); /* Remove connfd from buffer */ echo_cnt(connfd); /* Service client */ Close(connfd); } }
/* Thread routine */ void *thread(void *vargp) { int connfd = *((int *)vargp); Pthread_detach(pthread_self()); //line:conc:echoservert:detach Free(vargp); //line:conc:echoservert:free echo(connfd); Close(connfd); return NULL; }
void *handle_request_threaded(void *vargp) { int connfd = *((int *) vargp); Pthread_detach(pthread_self()); Free(vargp); handle_request(connfd); Close(connfd); return NULL; }
void *thread(void *vargp) { int connfd = *(int *)vargp; Pthread_detach(pthread_self()); /* 分离线程 */ Free(vargp); echo(connfd); Close(connfd); return NULL; }
Spread::Spread(SpreadNode &spreadNode):mSpreadNode(spreadNode) { mSpreadNode.isFinished = false; pthread_mutex_init(&mMutex,NULL); pthread_cond_init(&mCond,NULL); Pthread_create(&mPid, NULL, &StartSpreadThread, this); Pthread_detach(mPid); }
void *thread(void *vargp) { Pthread_detach(Pthread_self()); while (1) { int connfd = sbuf_remove(&sbuf); echo_cnt(connfd); Close(connfd); } }
/* * worker_thread - thread that initialized first, then invoke when * reqeust comming in */ void *worker_thread(void *vargp) { #ifdef DEBUG printf("enter worker_thread\n"); #endif Pthread_detach(pthread_self()); while (1) { int client_fd = sbuf_remove(&sbuf); request_handler(client_fd); } }
/* * connection handler as a thread for each connection * */ void *conn_handler (void *vargp){ unsigned int thread_id = pthread_self(); printf("NET*** [ 0x%x ] *** New connection \n" , thread_id); int connfd = *((int *)vargp); Pthread_detach(pthread_self()); //line:conc:echoservert:detach Free(vargp); //free allocated descriptor in main thread to prevent memory leak echo(connfd, thread_id); Close(connfd); return NULL; }
void * thread(void * vargp) { Pthread_detach(pthread_self()); //分离当前线程 while(1) { int connfd = sbuf_remove(&sbuf); //从buffer取出描述符 doit(connfd); Close(connfd); //关闭连接 } return NULL; }
/* $begin thread */ void *thread(void *vargp) { int connfd = *((int *)vargp); V(&mut_fd); Pthread_detach(pthread_self()); Free(vargp); doit(connfd); printf("connfd: %d is closed\n",connfd); close(connfd); return NULL; }
/* thread handler: do proxy*/ void *proxy_thread(void *argp) { int connfd = *((int *)argp); Pthread_detach(pthread_self()); Free(argp); //connfd = pop_fd_q(); //printf("### proxy_thread: %d, tid: %d\n", connfd, gettid()); run_proxy(connfd); return NULL; }
/** @brief Handles each incoming connection. * Detaches the thread from the parent, removes a single connection * descriptor from the shared buffer, and handles its request. * @param vargp NULL pointer. * @return NULL. */ void *conn_handler(void *vargp) { int conn_fd; Pthread_detach(pthread_self()); while(1) { conn_fd = sbuf_remove(&sbuf); handle_request(conn_fd); close(conn_fd); } }
/* thread routine */ void *thread(void *vargp) { int connfd = *((int *)vargp); printf("New thread created! fd = %d >>>>>>>>>>>>>>>>>>\n", connfd); Pthread_detach(pthread_self()); Free(vargp); Signal(SIGPIPE, sigpipe_handler); proxy(connfd); Close(connfd); printf("Thread is closing! fd = %d <<<<<<<<<<<<<<<<<<<<<<<<\n", connfd); return NULL; }
static void * do_echo(void *arg) { int connfd; connfd=*((int *)arg); free(arg); Pthread_detach(pthread_self()); str_echo(connfd); Close(connfd); return(NULL); }
void *thread (void *vargp) { thread_args args; args = *((thread_args *) vargp); Pthread_detach(pthread_self()); // handle segment fault: it is sometimes weird Signal(SIGSEGV, sigsegv_handler); // Valar Dohaeris serve(args.fd); // Valar Morghulis Close(args.fd); Free(vargp); return NULL; }
static void * doit(void *arg) { int connfd; connfd = *((int *) arg); free(arg); Pthread_detach(pthread_self()); str_echo(connfd); /* same function as before */ Close(connfd); /* we are done with connected socket */ return(NULL); }
void * ServeThread(void * vargp) { Pthread_detach(pthread_self()); while (1) { int connfd = sbuf_remove(&ClientPool); fprintf(stderr, "Thread %llu handling request %d.\n", (unsigned long long int)pthread_self(), connfd); ServeClient(connfd); Close(connfd); fprintf(stderr, "Thread %llu's request %d ended.\n", (unsigned long long int)pthread_self(), connfd); } }