Question

Nouvelle programmation à pthread, et bloqué sur cette erreur lorsque vous travaillez sur un code mixte C ++ et C.

Ce que je l'ai fait est d'appeler le code c dans le thread créé par le code c ++. Il y a un is_center pointeur booléen statique utilisé dans le fil et doit se dégagea lorsque la fin du thread.

Cependant, je remarque que chaque fois que le programme a traité dans la fonction c, la valeur du pointeur booléen serait changé et la faute de segmentation, puis est passé en raison du libre (). Et le problème ne se produit que lorsque le code c est utilisé. Retirez le code c et c ++ multi-thread partie fonctionne bien.

Code de détail est comme suit:

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

Et la fonction à l'aide parallèle est comme suit (mon code c est invoqué dans chaque 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);
}

Enfin la fonction appelant mon code c:

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

Et de GDB, ce que j'ai le is_center est:

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

Toutes les suggestions? Merci à l'avance!

Certaines de nouvelles informations sur le code: le code c, je me sers du paquet PAPI. Je vous écris mon propre emballage papi pour initialiser et lire les compteurs du système. Le code est le suivant:

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);                                                                                                          
} 
Était-ce utile?

La solution

Je ne comprends pense pas que vous avez posté assez de code pour vraiment votre problème, mais il semble suspect que vous avez déclaré global is_center. Je suppose que vous utilisez dans plus d'un endroit, peut-être par plusieurs threads (localSearchSub mentionne, qui est votre fonction thread de travail).

Si is_center est lu ou écrit par plusieurs threads, vous voulez probablement le protéger avec un

scroll top