I’m writing a C++/Qt multithreaded server. As it’s just a part of a bigger application, I have to write all the data I receive into a queue. Than someone’s other part processes this information and gives a response, also putting it into a queue.
I’d like to know if it’s possible to put this response to a certain socket in a certain thread, because the only idea I came up with so far is to send a signal that a response arrives which wakes all threads and they check if it’s a message for them (by checking socket descriptor).
my code:
thread.h
#pragma once
#include "socket.hpp"
#include <QThread>
class Thread : public QThread
{
Q_OBJECT
public:
Thread(int socketId, QObject *parent = 0);
void run();
signals:
void error(QTcpSocket::SocketError socketerror);
private:
int socketDescriptor;
};
thread.cpp
#include "thread.h"
#include <iostream>
Thread::Thread(int socketId, QObject *parent) :
QThread(parent), socketDescriptor(socketId)
{
}
void Thread::run()
{
Socket *tcpSocket = new Socket(socketDescriptor, this);
connect(tcpSocket, SIGNAL(connectionClosed()), SLOT(quit()));
exec();
}
socket.h
#pragma once
#include <QTcpSocket>
#include <boost/shared_ptr.hpp>
class Message;
class Socket : public QObject
{
Q_OBJECT
public:
Socket(int socketDescriptor, QObject* = 0);
~Socket();
bool isActive();
void emmiter();
signals:
void connectionClosed();
private slots:
void readClient();
void discardClient();
public slots:
void sendResponse(boost::shared_ptr<Message>);
private:
void disconnect();
quint16 nextBlockSize;
QTcpSocket* socket;
};
socket.cpp
void ClientSocket::sendResponse(boost::shared_ptr<Message> msg)
{
if(this->socket->socketDescriptor() == msg->conn.socket)
{
//actuall sending is not important
}
}
this ^ above is how I do it know, but i know it’s not good especially if I have many threads and they all check socketDescriptor(). Also I have to create a Meta Object from my Message class so that it can be use d in signal slot mechanism.
I’d like to kind of send a signal to one socket only. How can I do it ? Put socket object in a thread, and invoke a method by giving id’s to thread ? Any ideas ?
No, that’s not how you do it. A thread is like an employee. When you go to Burger King, you don’t ask for Jane. If you see Jeff at the counter, you don’t walk past him and go rummaging in the back for Jane to take your order. If she’s busy with a customer, but Jeff is available, you don’t wait for Jane. You let the first cashier who is available take your order.
When you receive data on a socket, have a thread, any thread, process that information. It doesn’t matter which. You don’t need to force some one particular thread to do it.
Think about threads as little as possible. They’re just the vehicles that do the work. You should focus on what work you want done and the code to do it. If you have to think about the mechanics of assigning the work to workers excessively, you are doing something wrong.
If you have have cashiers (who can’t cook) and cooks (who can’t take orders), you’ll always have to look around for the right employee to get the job done. But all threads are fundamentally equal, and equally capable of doing any job, so you only have this nightmare if you force yourself into it.