Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7602967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:32:53+00:00 2026-05-30T23:32:53+00:00

I have looked all over for pthread_join examples, I am having troubles debugging this

  • 0

I have looked all over for pthread_join examples, I am having troubles debugging this code. My code works correctly when I don’t run a pthread_join and have it commented out, but when I use it, I get a backtrace.

How would I use GDB to trace a thread? or better how would I fix this?

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;
};

Thread function

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);
}

Here is the error I see:

# 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:32:54+00:00Added an answer on May 30, 2026 at 11:32 pm

    Youd threads[0] is not initialized, you start creating threads from index 1.

    Edit 0:

    Another problem – you pass address of a local variable temp to the thread function:

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

    That is BadTM.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked all over but can't find an answer to this... Quite simply
I have looked all over and can't figure this out: how do you target
I have looked all over and found no solution, any help on this would
Ok, I have looked all over and I am having a difficult time trying
i have looked all over the place regarding this problem. All i am trying
I have looked all over stack overflow, for the answer to this question, and
I know this is frequently asked, but I have looked all over the internet
I've looked all over the place, but it seems that examples I have seen
I have looked all over the place but I cant seem to get this
Preface: I have looked all over stackoverflow.com and google for this. I have found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.