gpt4 book ai didi

c - Pthread_join 崩溃程序

转载 作者:太空宇宙 更新时间:2023-11-04 03:02:41 24 4
gpt4 key购买 nike

我已经查看了所有 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

最佳答案

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

编辑0:

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

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

那是糟糕的TM

关于c - Pthread_join 崩溃程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9523052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com