Is there anything wrong in doing this way with threads?
-
Hi,
I've designed a class , which i'm considering that in future it can have different services(each services should be spawned as threads ). So, I've made services to be made available in slots. Inside my class I've used connect() , to connect the start of the thread with the slots.
I've read "here":http://labs.qt.nokia.com/2006/12/04/threading-without-the-headache/ that QThread should not be made subclass which do make some sense for me too(Bcoz I want to stick with OOPs way)... :).
please tell me if I'm doing wrong,
@
class A : public QObject{Q_OBJECT
public:
qint32 function_name();
qint32 create_thread(void *servicesFunPtr);private:
void connection_setup(QThread &cThread, void *servicesFunPtr);public slots:
void func1();
void func2();
// In future some more services add it here.( Services are relevant to this class..)
};void A::connection_setup(QThread &cThread, void *servicesFunPtr) {
connect(&cThread,SIGNAL(started()),this,SLOT(servicesFunPtr));
}qint32 A::create_thread(void *servicesFunPtr) {
QThread threadID;
// connect it...
connection_setup(threadID,fnPtr);this->moveToThread(&threadID);
threadID.start();
}qint32 A::function_name() {
create_thread(func1);
create_thread(func2);
.....
// I might use the events for each threads...
}void A::fun1() {
forever {
// bla //
}}
void A::fun2() {
forever {
// bla //
}
}
@ -
your main method will not work as you used it.
The method connection_setup wants a function pointer, and you give it a member function pointer of a method that is not static. Invoking a slot without an object pointer is not possible.
And if you want to go the OOP way, it would be better to have something like this in the interface:
@
class BaseService : public QObject{
Q_OBJECT
public:
BaseService(const char * method = 0);
bool startService(const char * method = 0);private:
const char* m_serviceMethod;
QThread m_thread;
};BaseService::BaseService(const char * method):
m_serviceMethod(method)
{
}bool BaseService::startService(const char * method)
{
if(m_thread.isRunning())
return false;if(0 != method) m_serviceMethod = method; // connect it... connect(&m_thread , SIGNAL ( started () ) , this , m_serviceMethod); this->moveToThread(&m_thread); m_thread.start();
}
@Using the QTHread object as local object inside create_thread is a bad idea, as you call the destructor directly after start is called.
From my POV it would be even better to use some "strategy pattern":http://en.wikipedia.org/wiki/Strategy_pattern for this.
The service implementation would be the strategy. Sou you would have to create a service interface, with, let's say a start method. All services derive from this base service interface class.@
class BaseService : public QObject{
Q_OBJECT
public:
BaseService();
virtual bool startService() = 0;
};
@Then you have a service starter class which gets the BaseService pointer to use. This service starter ijmplements the thread stuff. Using it this way, you can create services independant of the logic of running them, which means, you could also use the same service with a thread pool, QtConcurrent, QTHread or even as main loop.
-
Hi,
Tanx for the comment.
I'll get back to you once I figure out the real meaning(New to OOP's) of what you have suggested as your view :)
After going through your sample code, How is it possible each methods(services) add to the class will be spawned as threads.?
As shown below,
@
if(m_thread.isRunning())
return false;
@
Will only spawn only one thread( I want individual thread for each services). Or is that like after following your POV would be possible.I'm really stuck here!! :(
-
I would create one class per service.
otherwise you have to use a vector of threads internally.
From OOPs POV, it makes more sense to make one service = one class.That's why the second proposal looks better to me. One class = one service.
And running the servoce can then be done anywhere else (in a "ServiceStarter")