Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:33:23+00:00 2026-06-17T05:33:23+00:00

I’m writing a C++/Qt multithreaded server. As it’s just a part of a bigger

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:33:24+00:00Added an answer on June 17, 2026 at 5:33 am

    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).

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.