0

我遇到了array[MAX]of 的问题p_thread。当我的数组已满时,我返回第一个位置并制作一个pthread_join(array[0], NULL). 后来,当我尝试拥有一个时pthread_create(array[0] ... .. ),都失败了。

这是失败的功能,是通过套接字与客户端通信的服务器的一部分。最奇怪的是,如果我决定增加 wlist[] 的索引,则执行正常(未定义的空间被写入)......

我究竟做错了什么?

typedef struct worker_t {
    pthread_t tid;
    int channel;
    int libero;
} worker_t;

worker_t wlist[THREADMAX];

static int dispatcher(){
    int test = -1;
    int count = 0;
    int i = 0 ;
    int client_channel;
    while(pending_sig == 0){
        if((client_channel = acceptConnection(channel_fd)) == -1){
            if (errno == EINTR){
                continue;
                }else{
                perror(ACCEPTCON);
                return -1;
            }
        }
        while(wlist[i].tid != 0 ){
            i++;
            count ++;
            if ( count >= THREADMAX){
                if(i >= THREADMAX){
                    i = 0;
                }
                pthread_join((wlist[i].tid),NULL);
                break;
            }

            wlist[i].channel = client_channel;


            if ( pthread_create(&(wlist[i].tid), NULL, worker_start,
                  &(wlist[i].channel)) != 0) {
                perror(THRCREATE);
                return -1;
            }
        }

        return 0;
    }
}
4

0 回答 0