Pregunta

I'm trying to simulate a multi-threading environment, with each thread calling the Speak function of the Kevin class. However, there's a segmentation fault (after/during) the pthread_mutex_lock function and I couldn't find out why. The line after pthread_mutex_lock will never execute. Some says it could be a declaration problem of pthread_mutex_t, but I had it globally declared just after my include statements. EDIT: it's running on redhat linux 64bit using gcc version 4.1.2

#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


pthread_mutex_t mutexsum;

class Kevin
{
public:
    Kevin();
    static void* Speak(void* value);
};

Kevin::Kevin()
{
    cout << "new instance of Kevin is created\n";
}

void* Kevin::Speak(void* value)
{
        cout<<"before lock"<<endl;
        pthread_mutex_lock (&mutexsum);
    cout << "Name: Kevin" << *((int*)value) << "\n" << "Seconds since epoch:" << "\nThread id:" << pthread_self() << endl;
        pthread_mutex_unlock (&mutexsum);
}

int main (int argc, char *argv[])
{
cout << "HI1" << endl;
int threadsNumb = atoi(argv[1]);
pthread_t callThd[threadsNumb];
long i;
void *status;
pthread_attr_t attr;

/* Assign storage and initialize values */


pthread_mutex_init(&mutexsum, NULL);

cout << "HI2" << endl;
/* Create threads to perform the dotproduct  */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

cout << "HI3" << endl;
for(i=0;i<threadsNumb;i++)
  {
cout << "HI4" << endl;
   pthread_create(&callThd[i], &attr, Kevin::Speak, (void *)i);
   }

   cout <<"HI6"<<endl;
pthread_attr_destroy(&attr);
/* Wait on the other threads */

   cout <<"HI7"<<endl;
for(i=0;i<threadsNumb;i++) {

    cout <<"HI8"<<endl;
    pthread_join(callThd[i], NULL);

    cout <<"HI9"<<endl;
  }

pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}

The sample outputs with the 1st argument(thread number) 8 :

$ ./a.out 8
HI1
HI2
HI3
HI4
HI4
HI4
HI4
HI4
HI4
HI4
HI4
before lock
before lock
before lockbefore lock
before lock
before lock
Segmentation fault (core dumped)
$ ./a.out 8
HI1
HI2
HI3
HI4
HI4
HI4
HI4
HI4
HI4
HI4
HI4
HI6
HI7
HI8
before lock
Segmentation fault (core dumped)
¿Fue útil?

Solución

It is not the mutex call, it is the cout after that. You reference the value as if it is a pointer while it is actually a long. Try the below changes:

void* Kevin::Speak(void* value)
{
    long i = (long) value; // cast from pointer to long
    cout<<"before lock"<<endl;
    pthread_mutex_lock (&mutexsum);
    cout << "Name: Kevin" << i << "\n" << "Seconds since epoch:" << "\nThread id:" << pthread_self() << endl;
    pthread_mutex_unlock (&mutexsum);
}

Otros consejos

pthread_create(&callThd[i], &attr, Kevin::Speak, (void *)&i) // should be variable pointer
cout << "Name: Kevin" << *((long*)value) ... // int => long

Unlucky, even modified like this, This program is still wrong. Because:

The variable i is shared by all speak threads and the main thread, but the main thread uses i with no mutex lock.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top