Pthread_join崩溃程序

时间:2012-03-01 19:56:11

标签: c multithreading pthreads

我已经查看了pthread_join示例,我在调试此代码时遇到了麻烦。当我没有运行pthread_join并将其注释掉时,我的代码正常工作,但是当我使用它时,我得到一个回溯。

我如何使用GDB跟踪线程?或者更好我将如何解决这个问题?

struct thread_param tp[THREADNUM]; 

 int
 search_funct(struct conn_pair temp, enum conn_state markState)
{
pthread_t threads[THREADNUM]; /* Threads */
int thread_cr_res = 0;
int thread_join_res = 0; /* Various ints */
results = 0; /* Zero the results before moving on */

int number = num_elements;
int divisor = THREADNUM;
int remainder = number % divisor;
int multi = number / divisor;

// Initalize counters
int startValue = 0;
int endValue = 0;
int i = 0; 

/* Create the threads */

while(i < divisor) {    

    i++;
    startValue = endValue;
    endValue = i*multi;

    if( i == divisor) {
        endValue = endValue +remainder;
    }
    #ifdef __DEBUG__
    printf("%d.) start %d, end %d\n",i,startValue,endValue);
    #endif

    /* Pass parameters to thread as struct */
    tp[i].conn = (struct conn_pair *)&temp;
    tp[i].start = startValue;
    tp[i].end = endValue;
    tp[i].markState = markState;
    thread_cr_res = pthread_create(&threads[i], 
                NULL, 
                thread_function, 
                (void*)&tp[i]);

    if(thread_cr_res != 0){
        fprintf(stderr,"THREAD CREATE ERROR");
        return (-1);
    }
}
//~ /* Joining and wait for the threads */
//~ for (i = 0; i < (THREADNUM-1); i++){
    //~ int rc = pthread_join(threads[i], NULL);
    //~ if (rc)
        //~ {
        //~ printf("# ERROR: Return code from pthread_join() is %d\n", rc);
        //~ }     
//~ }

/* Wait for the results since threads don't return a value */
printf("results: %d",results);
if(results > 0)
    return results;
else
    return modeCode;
}

帕拉姆:

struct thread_param {
        struct conn_pair *conn;
    int start;
    int end;
    enum conn_state markState;
};

线程功能

void *thread_function(void *arg){

int i = 0;

    for(i = ((struct thread_param*)arg)->start; i < ((struct thread_param*)arg)->end; i++)
    {

    if( \
             (!memcmp(&((struct thread_param*)arg)->conn->ip_src, &connection_arr[i].ip_src, sizeof(connection_arr[i].ip_src)) && \
              !memcmp(&((struct thread_param*)arg)->conn->ip_dst, &connection_arr[i].ip_dst, sizeof(connection_arr[i].ip_dst)))  && \
             (((struct thread_param*)arg)->conn->ip_p == connection_arr[i].ip_p) && \
             ((((struct thread_param*)arg)->conn->th_dport == connection_arr[i].th_dport) || \
              (((struct thread_param*)arg)->conn->th_sport == connection_arr[i].th_sport)))
    {
            #ifdef __DEBUG__
             printf("matched something\n");
             #endif

            /* Special states to be marked for cleanup */
            if (((connection_arr[i].state == rpc_tmp) || \
                    (connection_arr[i].state == ftp_actv_tmp) || \
                    (connection_arr[i].state == ftp_pasv_tmp)) && \
                    (((struct thread_param*)arg)->markState == marked_cleanup)
                    )
            {
               // connection_arr[i].state = marked_cleanup;
                pthread_mutex_lock( &mutex1 );
                results += 3;
                dirtyElements=i;
                pthread_mutex_unlock( &mutex1 );
                pthread_exit((void *)0); 

           }
           /* States that shouldn't be overwritten since we need to know these */
           if(((struct thread_param*)arg)->conn->ip_p == IPPROTO_TCP){
               pthread_mutex_lock( &mutex1 );
               results += 2;
               pthread_mutex_unlock( &mutex1 );
               pthread_exit((void *)0); 
           }

           /* Ultimately upsert the connection state */
            //connection_arr[i].state = ((struct thread_param*)arg)->markState;
            pthread_mutex_lock( &mutex1 );
            results += 2;
            pthread_mutex_unlock( &mutex1 ); 
            pthread_exit((void *)0);
        }

    }
}  

pthread_exit((void *)0);
}

以下是我看到的错误:

# ERROR: Return code from pthread_join() is 22
results: 0
0x11   192.168.1.100:54925 -> 239.255.255.250:1900  10
65535 allocated, 1 used

*** glibc detected *** ./prog: malloc(): memory corruption: 0x00000000

1 个答案:

答案 0 :(得分:2)

Youd threads[0]未初始化,您开始从索引1创建线程。

编辑0:

另一个问题 - 将局部变量temp的地址传递给线程函数:

search_funct(struct conn_pair temp, enum conn_state markState)
{
    ...
    tp[i].conn = (struct conn_pair *)&temp;

那是Bad TM