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

  • Home
  • SEARCH
  • 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 6974219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:12:32+00:00 2026-05-27T17:12:32+00:00

I have the following code snippet of a small Thread class I am trying

  • 0

I have the following code snippet of a small Thread class I am trying to implement:

declaration:

template <typename T>
class Thread{
public:
    Thread(T*, void (T::*)());
    void start();
private:
    friend void* createThread(void*);
    T* arg;  //the object
    void (T::*function)(); //the ptr to function in that object of type T
    pthread_t thread;
};

And below is a snippet of the definition.

template <typename T>
void* createThread(void *arg){
    //get back the pair..
    std::pair<T*, void (T::*)()>* p = (std::pair<T*, void (T::*)()>*)arg;

    //open up the pair
    T& t = *p->first;
    void (T::*fn)() = p->second;

    //TEST
    Temp ttt;
    ttt.a=100;
    (ttt.*fn)(); //segfaults here..

    (t.*fn)(); //and even here
}

template <typename T>
void Thread<T>::start(){
    //pair of pointer to object, and the pointer-to-member-function
    std::pair<T*, void (T::*)()> p(arg,function);
    pthread_create(&thread, NULL, createThread<T>, (void*)&p);
}

In the above code, Temp is a class with a function and a field ‘a’. And I get the thread running by the following code:

Temp t;
t.a=11;
Thread<Temp> tt(&t, &Temp::function);

tt.start();

Any idea why the code segfaults? I recollect that pointer-to-member-function does not really go well with casting to void* and back. Is that the case in here (since I am not doing that directly)?

Any pointers/suggestions will be highly appreciated.

Thanks! 🙂

  • 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-27T17:12:33+00:00Added an answer on May 27, 2026 at 5:12 pm

    It is segfaulting because your temporary pair std::pair<T*, void (T::*)()> p(arg,function); is falling off the scope before your createThread function is called.

    Store a copy of your pair in a heap memory and pass that.
    Then delete the copy inside the createThread function.

    EDIT

    As an aside, this would probably be better represented using std::functions. Exactly same idea(code even looks similar), but does not force you to rewrite your code for additional arguments. Look at pthread member function of a class with arguments.

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

Sidebar

Related Questions

I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have the following code snippet. public static void main(String[] args) { short a
I have following code snippet: addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) {
I have following code snippet that i use to compile class at the run
I have the following code snippet. I'm trying to list all the files in
I have the following code snippet I'm trying to get the text from: <span
I have following code snippet but I am getting wrong output. class Program {
I have the following code snippet: void foo(double a) {} namespace bar_space { struct
I have the following code snippet. $items['A'] = Test; $items['B'] = Test; $items['C'] =
I have the following code snippet where some strings are initialized in the if

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.