how to emit a signal such that only one of the connected slots is triggered?
-
hello, I'm implementing a load balancer for a tcp server.
when ever I receive a tcp connection request, I pass the file descriptor of the socket to one of the worker threads to handle it. I don't need all worker threads to response to the same socket.
suppose I have one tcp server object that receives an incoming tcp socket, I have 5 worker QThreads, each has a slot, named handleSocket();
during initialization, I connected the signal receiveNewSocket() in the tcp server object to the handleSocket() slot of each worker thread.
so if I simply do emit receiveNewSocket(), all 5 worker threads will be triggered, but I actually only want one to handle the request.
what should I do?
I also want to be able to load balance between the worker thread, for example, I want to be able to select one of the 5 threads to handle my request.
Thanks,
-
@billconan said in how to emit a signal such that only one of the connected slots is triggered?:
what should I do?
I also want to be able to load balance between the worker thread, for example, I want to be able to select one of the 5 threads to handle my request.Keep a list of your active threads, and have your load balancing manually (e.g. keep a record of how many requests a thread is currently carrying). Then call the thread directly to handle the new connection - either use
QMetaObject::invokeMethod
or queue the call withQTimer::singleShot
.