预先创建线程池的服务器程序

1) 定义存放已连接套接字描述符共享数组
2)创建线程池
3)等待用户连接
4)等待为之服务的客户描述符

#include <time.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdlib.h>

typedef struct {
    pthread_t thread_tid;
    long thread_count;
}Thread;
Thread *tptr;

int clifd[32],iget,iput;
pthread_mutex_t clifd_mutex;
pthread_cond_t clifd_cond;

void
web_child(int sockfd)
{
    ssize_t     n;
    char        buf[1024];

    printf("web child\n");
    while ( (n = read(sockfd, buf, 1024)) > 0)
        write(sockfd, buf, n);

    if (n < 0)
        printf("str_echo: read error");
}

void *
thread_main(void * arg)
{
    int connfd;
    void web_child(int);


    for(;;){
        pthread_mutex_lock(&clifd_mutex);

        printf("get mutex\n");

        while(iget == iput){
            int n;
            if ((n=pthread_cond_wait(&clifd_cond, &clifd_mutex)) != 0)
            {
                printf("wait err\n");
                return 0;
            }
        }

        connfd = clifd[iget];

        printf("connfd get \n");

        if(++iget == 10) 
            iget = 0;

        pthread_mutex_unlock(&clifd_mutex);

        tptr[(int) arg].thread_count++;

        web_child(connfd);

        close(connfd);
    }

}
void pthread_make(int i)
{
    void *thread_main(void*);
    pthread_create(&tptr[i].thread_tid, NULL, &thread_main, (void*)i);
}


int main(int argc, char** argv)
{

    int i,sockfd, connfd;
    void pthread_make(int);
    socklen_t addrlen, clilen;
    struct sockaddr_in servaddr;


    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("sock error\r\n");
        return 0;
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(13000);
    servaddr.sin_addr.s_addr = inet_addr("0.0.0.0");

    if( bind(sockfd, (struct sockaddr* )&servaddr, sizeof(servaddr)) < 0)
    {
        printf("bind error\r\n");
        return 0;
    }

    if(listen(sockfd, 10) < 0)
    {
        printf("listen error\r\n");
        return 0;
    }

    int threadnum = 10;
    iget = iput = 0;
    tptr = calloc(threadnum , sizeof(Thread));

    for(i = 0; i< 10; i++){
        pthread_make(i);
    }
    for( ; ;)
    {

        connfd = accept(sockfd, NULL, NULL);
    printf("accept sock %d \r\n",connfd);   
    pthread_mutex_lock(&clifd_mutex);

    clifd[iput] = connfd;
    iput++;
    if(iput == 10) 
        iput = 0;

    printf("iput iget %d %d \n",iput,iget); 
    int n;          
    if ( (n = pthread_cond_signal(&clifd_cond)) != 0){

        printf("signal err\n");
        return 0;
    }

    pthread_mutex_unlock(&clifd_mutex);

    }
}

http://blog.chinaunix.net/uid-11572501-id-3456343.html
介绍了pthread_cond_signal与pthread_cond_wait详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值