Run thread with derived method from base class without using templates

Related searches

I'd like to achieve non-templated class just like commented one. Uncommented class will results in an compilation error error: invalid use of non-static member function 'virtual void BaseThread::worker()

#include <iostream>
#include <thread>

/*
class BaseThread
{
public:
    void start()
    {
// error: invalid use of non-static member function 'virtual void BaseThread::worker()'
        thread_ = std::thread(worker);
    }

private:
    virtual void worker() = 0;

    std::thread thread_;
};
*/

template <typename Derived>
class BaseThread
{
public:
    void start()
    {
        thread_ = std::thread(Derived::worker);
    }

private:
    virtual void worker() = 0;

    std::thread thread_;
};

class DerivedThread : public BaseThread<DerivedThread>
{ 
private:
    void worker()
    {
        std::cout << "Derived\n";
    }
};

int main()
{
    DerivedThread dt;
}

The point is pretty simple: You pass a member function to a thread object, but not an object which that function could be called on.

The template only seemingly solves this problem, as you don't instantiate it. As soon as you do so, the same problem will re-appear.

You can fix that by passing an instance as parameter:

thread_ = std::thread(&BaseThread::worker, this);

Additionally, you have to take a member function pointer via appropriate operator and class scope resolution.

Side note: As you have a virtual function, you should consider adding a virtual destructor as well, otherwise deleting a derived class via pointer to base class will fail miserably.

Don't forget to join the thread in the destructor (or wherever that appears appropriate to you) either.

Calling derived class function from base class with templates without , If you really really really want to do it: static_cast<_T*>(this)->f2();. As people have mentioned, this is the curiously recuring template pattern! base (C# Reference) 07/20/2015; 2 minutes to read +4; In this article. The base keyword is used to access members of the base class from within a derived class:. Call a method on the base class that has been overridden by another method.


Why does this not compile? The point is that you simply didn't get the syntax right, that's all. You can not call a memberfunction without an object. A simple way to pass these to std::thread like this:

std::thread th{&ThreadBase::worker, this}

However, you have picked an unfortunate abstraction, which is to create a "thread" class in the first place, which requires you to derive from. Don't do that, because it mixes two concerns, which are the code being called and the thread. To some extent, this is similar to creating a file baseclass which you derive from for different file formats. The indicator for this flawed approach is that in both cases, the instance of the thread or file is not a thread or file! Think about that, an instance of std::thread is not a thread and an instance of std::fstream is not a file. Rather, these objects are objects that you can use to interact with a thread or a file, similar to how a database client is used to interact with a database.

In summary, instead of writing a class that is derived from your BaseThread, just write a class that does something. Don't let this class worry about whether its code is called in a thread. If you want to do that, just use std::thread which you already did under the hood.

Calling base class function when base is a template, Thread: Calling base class function when base is a template One of them needs to call into the base class version of the function. class Derived : public Foo<string,Object> { public: virtual void Is there a way to call the base without forcing me to type out the entire template and it's types (the bold� Base class object will call base version of the function and derived class's object will call the derived version of the function. Function Call Binding using Base class Pointer But when we use a Base class's pointer or reference to hold Derived class's object, then Function call Binding gives some unexpected results.


Simpler and non-templated implementation could be this.

#include <thread>
#include <iostream>
#include <functional>

class BaseThread
{
public:
    virtual ~BaseThread() { thread_.join(); }
    void start()
    {
        thread_ = std::thread( &BaseThread::worker , this );        
    }

protected:

    virtual void worker() = 0;

private:

    std::thread thread_;
};

class DerivedThread : public BaseThread
{

protected:

    void worker() override
    {
        std::cout << "Derived\n";
    }
};

int main()
{

    DerivedThread dt;
    DerivedThread dt2;

    dt.start();
    dt2.start();

    std::this_thread::sleep_for( std::chrono::seconds { 5 } );
}

Calling a base class virtual function from a derived class, You can call a base class version of a virtual function from a derived Parent inherits method() from Grandparent , and does not override it. When a base class declares a method as virtual, a derived class can override the method with its own implementation. If a base class declares a member as abstract, that method must be overridden in any non-abstract class that directly inherits from that class. If a derived class is itself abstract, it inherits abstract members without


Call it via static member function

class BaseThread
{
public:
    void start()
    {
        thread_ = std::thread(sworker, this);
    }

private:
    virtual void worker() = 0;
    static void sworker(BaseThread *t) {
        t->worker();
    }
    std::thread thread_;
};

Pointers to Member Functions, C++ FAQ, Is the type of “pointer-to-member-function” different from “pointer-to-function”? Because a member function is meaningless without an object to invoke it on, you can't The system call that starts a thread, for example, might require you to pass a function Then instead of three functions, you create three derived classes:. As per overriding principle, though bd was of type Base, when it is initialized with object Derived, it should have invoked the method of the Derived class. Here comes the role of override keyword. If we add the virtual keyword before the base class method and override keyword before the derived class method, we can solve the above problem.


C++ is Lazy: CRTP, The key is that Base has Derived as template argument. Although the method func2 (line 8) of the class, Lazy is only declared but not defined, the I use in the function template execute (line 29 - 32) static polymorphism. Thread-Safe Initialization of a Singleton (217430 hits) � C++ Core Guidelines:� Examples. The following example demonstrates simple threading functionality. // [C++] // Compile using /clr option. using namespace System; using namespace System::Threading; // Simple threading scenario: Start a Shared method running // on a second thread. public ref class ThreadExample { public: // The ThreadProc method is called when the thread starts.


It calls base class and member constructors in the order of declaration. If the class is derived from virtual base classes, it initializes the object's virtual base pointers. If the class has or inherits virtual functions, it initializes the object's virtual function pointers.


The derived class invokes the methods defined in SharedFragments. The application calls the TextTransform() method of the derived class, but does not transform the base class SharedFragments. Both the base and derived classes are runtime text templates; that is, the Custom Tool property is set to TextTemplatingFilePreprocessor. SharedFragments.tt: