How to create a subclass of thread Class properly in C++(subclass of std::thread)
Solution 1
The error about "uses 'private' to inherit" when you try to call 'p.join()' is because you have written:
class Parallel:thread
When you should have written:
class Parallel : public thread
The default is private inheritance, which means all methods of the base class become private on the inheriting class. You have to specify that the inheritance should be public if you want them to be accessible.
Solution 2
In Java functions are not first class citizens, so everything related to functions is achieved via classes and inheritance, like in the implementation of the Observer pattern of Swing: You create a class which implements the interface of the handler of the event (You have to implement an entire class only to provide a function).
The same occurs with threads: Because you cannot pass a function to the thread (Because Java doesn't have the concept of "function"), you have to derive from the thread class implementing your own function to execute, or pass a class that implement the Runnable
interface which provides the function to be executed (Exactly as in the Observer
case).
In C++ functions are first class citizens (In the form of function pointers, lambdas, functors, etc), so the mechanism here is to pass the function to be executed to the thread.
If what you have is a OO dessign, consider to fit the thread through composition (Store the thread of execution as a member of the class) instead of inheriting from it.
Note: Java 8
Only as a sidenote, Java 8 implements lambdas, and provides function references (Hooray!) through them. But it implements lambdas using interfaces because it appers that in Java functions never will be first class citizens. The problem with interfaces is it breaks completely the generallity of lambdas: A lambda can be used in any context which matches its signature. Thats not true with Java lambdas: You have to implement the same interface.
Related videos on Youtube
Buddhika Chaturanga
Enthusiastic in Everything. I am not too complex not too simplest.
Updated on September 15, 2022Comments
-
Buddhika Chaturanga over 1 year
I am trying to create a class
Parallel
which is a subclass ofstd::thread
,therefore my class is defined inParallel.h
,but the main method defined in separate filemain.cpp
in same project(in visual studio).When I create an instance ofParallel
and executejoin()
function inmain()
method as below code segment: I am new to C++, here is the "Parallel.h"-#include<thread> using namespace std; namespace Para{ class Parallel:thread { public: static void run(){ } Parallel(void) { } virtual ~Parallel(void) { } inline static void start(Parallel* p){ // (*p).join(); } virtual void Parallel::start(thread& t){ } static void parallelize(Parallel& p1,Parallel& p2){ } inline virtual Parallel* operator=(thread* t){ return static_cast<Parallel*>(t); } }
//in main.cpp
void main(){ Parallel p; p.join(); thread t(print); t.join(); system("Pause"); }
Problem is how to define a proper subclass of a thread class having a overloaded constructor taking function name as a parameter,also when defined
p.join()
compiler given following errors in VS2012:Error 2 error C2247: 'std::thread::join' not accessible because 'Para::Parallel' uses 'private' to inherit from 'std::thread' C:\Users\Gamer\Desktop\PROJECQ\VC++@OMAQ\CQ47\CQ47\main.cpp 11
3 IntelliSense: function "std::thread::join" (declared at line 209 of "H:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\thread") is inaccessible c:\Users\Gamer\Desktop\PROJECQ\VC++@OMAQ\CQ47\CQ47\main.cpp 11
-
Manu343726 over 10 yearsInheriting from Standard Library classes is generally a bad idea. Why do you need to do that?
-
josesuero over 10 years@BuddhikaChaturanga In Java you need to create a Thread subclass because of how the Thread class is designed. In C++ you do not need to do so, and it is the wrong way to do it. In C++, the correct way to create a thread is not to subclass
std::thread
, but simply to pass a function or function object of your choosing tostd::thread
's constructor. -
nosid over 10 yearsWhy don't you use composition instead of inheritance, i.e. using
std::thread
as a member variable of your class?
-