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 839087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:24:37+00:00 2026-05-15T05:24:37+00:00

New to pthread programming, and stuck on this error when working on a C++&C

  • 0

New to pthread programming, and stuck on this error when working on a C++&C mixed code.

What I have done is to call the c code in the thread created by the c++ code. There is a static boolean pointer is_center used in the thread and should got free when the thread finishes.

However I noticed that every time when the program processed into the c function, the value of the boolean pointer would be changed and the segmentation fault then happened due to the free(). And the problem only happens when the c code is used. Remove the c code and the multi-thread c++ part works well.

Detail code is as follows:

static bool *is_center;

// omit other codes in between ...

void streamCluster( PStream* stream)
{
    // some code here ...
    while(1){
        // some code here ...
        is_center = (bool*)calloc(points.num,sizeof(bool));

        // start the parallel thread here.
        // the c code is invoked in this function.
        localSearch(&points,kmin, kmax,&kfinal); // parallel

        free(is_center);
    }

And the function using parallel is as follows (my c code is invoked in each thread):

void localSearch( Points* points, long kmin, long kmax, long* kfinal ) {
    pthread_barrier_t barrier;
    pthread_t* threads = new pthread_t[nproc];
    pkmedian_arg_t* arg = new pkmedian_arg_t[nproc];

    pthread_barrier_init(&barrier,NULL,nproc);

    for( int i = 0; i < nproc; i++ ) {
            arg[i].points = points;
            arg[i].kmin = kmin;
            arg[i].kmax = kmax;
            arg[i].pid = i;
            arg[i].kfinal = kfinal;
            arg[i].barrier = &barrier;

            pthread_create(threads+i,NULL,localSearchSub,(void*)&arg[i]);
    }

    for ( int i = 0; i < nproc; i++) {
        pthread_join(threads[i],NULL);
    }

    delete[] threads;
    delete[] arg;
    pthread_barrier_destroy(&barrier);
}

Finally the function calling my c code:

void* localSearchSub(void* arg_) {                                                                                                                                                        

    int eventSet = PAPI_NULL;                                                                                                                                                                                                                                                                                                                                     
    begin_papi_thread(&eventSet);                                                                                                                                                         

    pkmedian_arg_t* arg= (pkmedian_arg_t*)arg_;                                                                                                                                             
    pkmedian(arg->points,arg->kmin,arg->kmax,arg->kfinal,arg->pid,arg->barrier);                                                                                                            

    end_papi_thread(&eventSet);                                                                                                                                                                                                                                                                                                                                                 

    return NULL;                                                                                                                                                                            
}   

And from gdb, what I have got for the is_center is:

Breakpoint 2, localSearchSub (arg_=0x600000000000bc40) at streamcluster.cpp:1711
1711      end_papi_thread(&eventSet);
(gdb) s

Hardware watchpoint 1: is_center

Old value = (bool *) 0x600000000000bba0
New value = (bool *) 0xa93f3
0x400000000000d8d1 in localSearchSub (arg_=0x600000000000bc40) at streamcluster.cpp:1711
1711      end_papi_thread(&eventSet);

Any suggestions? Thanks in advance!

Some new information about the code: for the c code, I am using the PAPI package. I write my own papi wrapper to initialize and read system counters. The code is as follows:

void begin_papi_thread(int* eventSet)                                                                                                                                                     
{                                                                                                                                                                                         
    int thread_id = pthread_self();                                                                                                                                                       
    // Events                                                                                                                                                                             
    if (PAPI_create_eventset(eventSet)) {                                                                                                                                                 
        PAPI_perror(return_value, error_string, PAPI_MAX_STR_LEN);                                                                                                                        
        printf("*** ERROR *** Failed to create event set for thread %d: %s\n.", thread_id, error_string);                                                                                 
    }                                                                                                                                                                                     
    if((return_value = PAPI_add_events(*eventSet, event_code, event_num)) != PAPI_OK)                                                                                                     
    {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        printf("*** ERROR *** Failed to add event for thread %d: %d.\n", thread_id, return_value);                                                                                                                                                                                                                                                                         
    }                                                                                                                                                                                     
    // Start counting                                                                                                                                                                     
    if ((return_value = PAPI_start(*eventSet)) != PAPI_OK) {                                                                                                                                                                                                                                                                                                              
        PAPI_perror(return_value, error_string, PAPI_MAX_STR_LEN);                                                                                                                                                                                                                                                                                                         
        printf("*** ERROR *** PAPI failed to start the event for thread %d: %s.\n", thread_id, error_string);
    }                                                                                                                                                                                                                                                                                                                                                                             
}  
void end_papi_thread(int* eventSet)                                                                                                                                                       
{                                                                                                                                                                                         
    int thread_id = pthread_self();                                                                                                                                                       
    int i;                                                                                                                                                                                

    long long * count_values = (long long*)malloc(sizeof(long long) * event_num);                                                                                                         
    if (PAPI_read(*eventSet, count_values) != PAPI_OK)                                                                                                                                    
        printf("*** ERROR *** Failed to load count values.\n");                                                                                                                           

    if (PAPI_stop(*eventSet, &dummy_values) != PAPI_OK) {
        PAPI_perror(return_value, error_string, PAPI_MAX_STR_LEN);
        printf("*** ERROR *** PAPI failed to stop the event for thread %d: %s.\n", thread_id, error_string);
        return;
    }
    if(PAPI_cleanup_eventset(*eventSet) != PAPI_OK)
        printf("*** ERROR *** Clean up failed for the thread %d.\n", thread_id);                                                                                                          
} 
  • 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-15T05:24:38+00:00Added an answer on May 15, 2026 at 5:24 am

    I don’t think you’ve posted enough code to really understand your problem, but it looks suspicious that you’ve declared is_center global. I assume you’re using it in more than one place, possibly by multiple threads (localSearchSub mentions it, which is your worker thread function).

    If is_center is being read or written by multiple threads, you probably want to protect it with a pthread mutex. You say it is “freed when the thread finishes”, but you should be aware that there are nprocs threads, and it looks like they’re all working on an array of is_center[points] bools. If points != nproc, this could b e a bad thing[1]. Each thread should probably work on its own array, and localSearch should aggregate the results.

    The xxx_papi_thread functions don’t get any hits on Google, so I can only imagine it’s your own… unlikely we’ll be able to help you, if the problem is in there 🙂

    [1]: Even if points == nproc, it is not necessarily OK to write to different elements of an array from multiple threads (it’s compiler and processor dependent). Be safe, use a mutex.

    Also, this is tagged C++. Can you replace the calloc and dynamic arrays (using new) with vectors? It might end up easier to debug, and it certainly ends up easier to maintain. Why do you hate and want to punish the readers of your code? 😉

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

Sidebar

Ask A Question

Stats

  • Questions 508k
  • Answers 508k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to use Reflection then the missing piece… May 16, 2026 at 4:26 pm
  • Editorial Team
    Editorial Team added an answer I use the following code on .Net 3.5 SP1: var… May 16, 2026 at 4:26 pm
  • Editorial Team
    Editorial Team added an answer Finally found the solution HERE Short explanation is that the… May 16, 2026 at 4:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I come into a strange problem in pthread programming I've compiled the following code
I have a fairly specific question about concurrent programming in C. I have done
Let's say I have started new thread with pthread_create() and then detached it using
Iam new to c programming and needs some help. long *taskids[NUM_THREADS]; for(t=0; t<NUM_THREADS; t++)
Using POSIX threads & C++, I have an Insert operation which can only be
I am trying to start a method from my main() as a new thread
New to PHP, would love to get to know it better. Currently working on
Quite new to functional languages, but I'm maintaining someone else's code with a lot
In the type of embedded programming I'm getting into, determinism and transparency of the
I'm programming an application(client/server) in C++ for linux using epoll y pthreads but I

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.