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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:30:15+00:00 2026-06-13T08:30:15+00:00

Hi I have the following C++ code, I have MyThread class in the thread.h

  • 0

Hi I have the following C++ code,

I have MyThread class in the thread.h file

class MyThread
{ 
    public:
       void start();
       virtual void* task() = 0;
};

I have worker class in the worker.h file

class Worker : public MyThread
{
    virtual ~Worker();
    virtual void* task();   // I have implementation in worker.cpp
};

Have used pthread_create() in the start() in thread.cpp file for creating a thread and I want to make the task() routine defined in the worker.cpp as the start routine . So how can I do that ? What should I pass as the parameter to the start routine in pthread_create ?

I have tried this but it gives an error:

pthread_create(&(this->threadId),&(this->attr),(void *)task,NULL);                        
  • 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-06-13T08:30:16+00:00Added an answer on June 13, 2026 at 8:30 am

    You can’t use member function as a thread function. You can only use free functions or static member functions, but you can pass arbitrary void* argument to that function and you can pass pointer to this as that argument, that allows you to call member function inside that function:

    class MyThread
    { 
        public:
           void start();
           virtual void* task() = 0;
    
           static void * thread_routine(void* pthis)
           {
                MyThread* t = static_cast<MyThread*>(pthis);
                t->task();
                return 0;
           }
    };
    

    Then you launch your thread like this

    void MyThread::start()
    {
        pthread_create(&(this->threadId),&(this->attr),thread_routine, this);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following utility: class Worker { public void DoWorkAsync(Action callback) { Action
So I have the following code: import java.lang.Thread; import java.lang.Integer; class MyThread extends Thread
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
Ok, I have the following code. public class MyProgressBar extends JPanel implements MyData, Serializable
I have following code: public abstract class Operand<T> { public T Value { get;
I have following code public partial class MainWindow : Window { public MainWindow() {
I have following code that displays an Image with letters, public class MainActivity extends
I have the following code but it's output is not what I expected: public
I have following code: class EntityBase (object) : __entity__ = None def __init__ (self)
Have following code: public interface ITest { string St1 { get; } } public

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.