This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/14986] pthread mutex segfaults inside C++ class thread


http://sourceware.org/bugzilla/show_bug.cgi?id=14986

--- Comment #2 from mdblack98 <mdblack98 at yahoo dot com> 2012-12-28 16:00:47 UTC ---
Thanks for the fast response.
I just figured out if you change the thread function to a static declaration it
works.
But then you have to pass the mutex in as an argument to the thread of course
So this works with either using the Class format or just the function name as
the function argument

g++ -o pthread pthread.cpp -lpthread

#include <iostream>
#include <pthread.h>

using namespace std;

class MyThread {
private:
    pthread_t threadid;
    pthread_mutex_t mutex;
public:
    MyThread();
    ~MyThread();
    pthread_t get_threadid() {
        return threadid;
    };
    static void *myfunc(void*);
};

MyThread::MyThread()
{
    pthread_mutex_init(&mutex,NULL);
    cout << "Creating thread mutex=" << &mutex << endl;
   
pthread_create(&threadid,NULL,(void*(*)(void*))&MyThread::myfunc,(void*)&mutex);
    // this also works
    //pthread_create(&threadid,NULL,(void*(*)(void*))&myfunc,(void*)&mutex);
}

MyThread::~MyThread()
{
}


void *MyThread::myfunc(void*v)
{
    int n=0;
    pthread_mutex_t *mutexp = (pthread_mutex_t*)v;
    cout << "In thread" << endl;
    while (n < 10) {
        pthread_mutex_lock(mutexp);
        sleep(1);
        pthread_mutex_unlock(mutexp);
        cout << "Testing " << ++n << endl;
    }
    return NULL;
}

int main(int argc, char *argv[])
{
    MyThread thread;
    pthread_t threadid = thread.get_threadid();
    cout << argv[0] << " running" << endl;
    sleep(5);
    pthread_join(threadid,NULL);
    return 0;
}

-----Original Message-----
From: schwab@linux-m68k.org [mailto:sourceware-bugzilla@sourceware.org] 
Sent: Friday, December 28, 2012 9:44 AM
To: mdblack98@yahoo.com
Subject: [Bug libc/14986] pthread mutex segfaults inside C++ class thread

http://sourceware.org/bugzilla/show_bug.cgi?id=14986

Andreas Schwab <schwab@linux-m68k.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> 2012-12-28 15:43:43
UTC ---
You cannot pass a member function to pthread_create.

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]