Я просмотрел примеры 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;
}
Param:
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