- 论坛徽章:
- 0
|
本人写了一个线程池的程序,现在遇到一个问题
给你一个能用的,希望对你有启发
- #include <stdio.h>;
- #include <errno.h>;
- #include <sys/types.h>;
- #include <sys/socket.h>;
- #include <sys/signal.h>;
- #include <sys/select.h>;
- #include <sys/sem.h>;
- #include <netinet/in.h>;
- #include <pthread.h>;
- pthread_mutex_t mutex;
- pthread_cond_t cond;
- pthread_t tid, tid2, tid3;
- void *thread_start(void *arg)
- {
- while(1)
- {
- pthread_mutex_lock(&mutex);
- if( pthread_cond_wait(&cond,&mutex) == EINTR )
- {
- printf("eintr...............\n");
- }
- pthread_mutex_unlock(&mutex);
- }
- pthread_exit(0);
- }
- int killHandle(int signo)
- {
- printf("get here.\n");
- pthread_mutex_destroy(&mutex);
- pthread_cond_destroy(&cond);
- pthread_exit(0);
- exit(0);
- }
- int createthread(void)
- {
- pthread_mutex_init(&mutex,NULL);
- pthread_cond_init(&cond, NULL);
- pthread_create(&tid, NULL, thread_start, NULL);
- pthread_create(&tid2, NULL, thread_start, NULL);
- pthread_create(&tid3, NULL, thread_start, NULL);
- }
- int main(void)
- {
- int sockfd = -1;
- int ret = 0;
- int listenfd = 0;
- struct sockaddr_in server_addr;
- signal(SIGTERM, (void *)killHandle);
- signal(SIGINT, (void *)killHandle);
- signal(SIGPIPE, (void *)killHandle);
- createthread();
- sockfd = socket(AF_INET, SOCK_STREAM, 0 );
- if( sockfd == -1 )
- {
- printf("getSocket:socket create error.\n");
- return -1;
- }
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(50001);
- server_addr.sin_addr.s_addr = INADDR_ANY;
- bzero(&(server_addr.sin_zero));
- ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
- if( ret == -1 )
- {
- printf("getSocket:socket bind error.\n");
- return -1;
- }
- listenfd = listen(sockfd, 120);
- for( ;; )
- {
- printf("main:get here 1\n");
- ret = select(sockfd+1, NULL, NULL, NULL, NULL);
- if( ret == EINTR )
- {
- printf("main: eintr\n");
- return 0;
- }
- printf("main:get here 2\n");
- }
- return 0;
- }
复制代码
楼上说的也不正确,代码问题出在signal(SIGINT, (void *)killHandle); |
|