I wrote this code in eclipse Linux and when run it only one time function shade() execute and do something after that only function socketHandler() execute,
why this is happen?
Code:int main(){ ////bind and listen while (true) { cout << "\n waiting for a connection" << endl; csock = (int*) malloc(sizeof(int)); if ((*csock = accept(hsock, (sockaddr*) &sadr, &addr_size)) != -1) { printf("---------------------\nReceived connection from %s\n",inet_ntoa(sadr.sin_addr)); pthread_t th1, th2, th3; pthread_create(&thread_id, 0, &SocketHandler, (void*) csock);// thread object pthread_create(&th1, 0, Shade, 0); // create it pthread_detach(thread_id); pthread_detach(th1); } } void *SocketHandler(void *lp) { while (1) { int *csock = (int*) lp; //do something fflush(stdout); usleep(100); } pthread_exit(0); } void * Shade(void *rt) { while (1) { //do something fflush(stdout); usleep(200); } pthread_exit(0); }





Reply With Quote
