Unsolved movetothread in threadpool
-
How to move an object to a specific thread of a threadpool ?
[Duplicate of https://forum.qt.io/topic/90726/movetothread-with-qthreadpool ~kshegunov]
-
Hi,
Are you trying to move a thread to another thread ?
-
@SGaist i have corrected the question
-
I'm not sure if we can specify the threads in threadpool, but I've just started to learn all this so...i'm not an expert.
-
The idea behind QThreadPool is to use a QRunnable subclass object that does the job you want in another thread. You don't access the QThread objects used directly.
So what are you trying to achieve ?
-
@SGaist thank you.
i want to use particular thread from Qthreadpool.
for Ex:
worker->moveToThread(mythread);here,as we are moving object in "mythread" same way i want to move object to the thread which is one of the Qthreadpool's thread.
-
@JadeN001 said in movetothread in threadpool:
i want to use particular thread from Qthreadpool.
but why? Why do you care which thread from the pool is used?
-
@jsulm i want to move two objects in one thread
-
Why do you need two different objects moved to the same thread ?
-
If you want to choose a specific thread to use, then create your own
QThread
. Don't useQThreadPool
.QThreadPool
is designed for situations where you don't care which thread is being used. -
you can do
movetoThread(QThread::currentThread);
in your runnable::run();
-
@JKSH can i use
connect(socket, SIGNAL(readyRead()),this, SLOT(readyread()));
In run() of qrunnable class. -
@JadeN001 said in movetothread in threadpool:
@JKSH can i use
connect(socket, SIGNAL(readyRead()),this, SLOT(readyread()));
In run() of qrunnable class.I haven't done this before so I'm not 100% sure. It might be possible, but only if:
- Your QRunnable also runs its own event loop, AND
- You set your
QRunnable
's thread affinity correctly usingQObject::moveToThread()
.
This feels messy/hacky to me though. Is there a reason why you don't want to use a worker
QObject
subclass plus aQThread
instead? -
@JKSH i have already done my task using qthread.but i want make it possible by doing same thing using qthreadpool because in qthread i have made vector of limited threads.I think that it might me possible with qthreadpool.
As i know qrunnable doesnt have its own qeventloop.
You set your QRunnable's thread affinity correctly using QObject::moveToThread().
for this what i experiment is :
In run() of subclass QRunnable:void run() { worker *w=new worker(); w->moveToThread(QThread::currentThread()); w->addclients(socketDescriptor); }
it is correct or not i don't know but it works.
-