Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.
For bugs related to Red Hat Enterprise Linux 5 product line. The current stable release is 5.10. For Red Hat Enterprise Linux 6 and above, please visit Red Hat JIRA https://issues.redhat.com/secure/CreateIssue!default.jspa?pid=12332745 to report new issues.

Bug 890060

Summary: pthread mutex segfaults inside C++ class
Product: Red Hat Enterprise Linux 5 Reporter: mdblack98 <mdblack98>
Component: gcc44Assignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: qe-baseos-tools-bugs
Severity: medium Docs Contact:
Priority: unspecified    
Version: 5.7CC: mpolacek
Target Milestone: rc   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2013-01-09 11:20:46 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
Source code to show bug none

Description mdblack98 2012-12-24 19:41:59 UTC
Created attachment 668578 [details]
Source code to show bug

Description of problem:
pthread segfault when mutex inside C++ class

Version-Release number of selected component (if applicable):
g++ (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)
NPTL 2.5

How reproducible:
Working version (mutex outside of class)
g++ -g -std=c++0x -o pthread pthread.cpp -lpthread
segfault occurs (mutex inside of class)
g++ -DBUG -g -std=c++0x -o pthread pthread.cpp -lpthread

Steps to Reproduce:
1.
2.
3.
  
Actual results:


Expected results:


Additional info:

Comment 1 Jakub Jelinek 2013-01-09 11:20:46 UTC
This is invalid, you can't pass a method to a function which takes a function callback, result of &method can be only used in ->* or .*.

Comment 2 mdblack98 2013-01-09 14:01:02 UTC
There is a solution to this...have to recast the passed in object
From http://blog.emptycrate.com/node/270
#include <iostream>
#include <vector>
#include <pthread.h>
#include <assert.h>

using namespace std;

class threaded_class
{
public:
    threaded_class()
        : m_stoprequested(false), m_running(false)
    {
        pthread_mutex_init(&m_mutex,NULL);
    }

    ~threaded_class()
    {
        pthread_mutex_destroy(&m_mutex);
    }

    // Create the thread and start work
    // Note 1
    void go()
    {
        assert(m_running == false);
        m_running = true;
        pthread_create(&m_thread, 0, (void*(*)(void*))&threaded_class::start_thread, this);
    }

    void stop() // Note 2
    {
        assert(m_running == true);
        m_running = false;
        m_stoprequested = true;
        pthread_join(m_thread, 0);
    }

    int get_fibonacci_value(int which)
    {
        pthread_mutex_lock(&m_mutex); // Note 3 
        int value=1;
        //int value = m_fibonacci_values.get(which); // Note 4 
        pthread_mutex_unlock(&m_mutex);
        return value;
    }
private:
    volatile bool m_stoprequested; // Note 5
    volatile bool m_running; 
    pthread_mutex_t m_mutex; // Variable declarations added 4/14/2010
    pthread_t m_thread;

    std::vector<int> m_fibonacci_values;

    // This is the static class function that serves as a C style function pointer
    // for the pthread_create call
    static void start_thread(void *obj)
    {
        //All we do here is call the do_work() function
        reinterpret_cast<threaded_class *>(obj)->do_work();
    }

    int fibonacci_number(int num)
    {
        switch(num)
        {
            case 0:
            case 1:
                return 1;
            default:
                return fibonacci_number(num-2) + fibonacci_number(num-1); // Correct 4/6/2010 based on comments
        };
    }

    // Compute and save fibonacci numbers as fast as possible
    void do_work()
    {
        int iteration = 0;
        while (!m_stoprequested)
        {
            int value = fibonacci_number(iteration);
            pthread_mutex_lock(&m_mutex);
            m_fibonacci_values.push_back(value);
            pthread_mutex_unlock(&m_mutex); // Note 6
        }
    }
};

int main()
{
  threaded_class myClass;
  cout << "Done" << endl;
}

Comment 3 Jakub Jelinek 2013-01-09 14:30:06 UTC
Of course there are many solutions, that doesn't change anything on the provided original testcase being invalid and thus not a bug on the gcc side.