Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can I create QTcpServer in another thread?
QtWS25 Last Chance

Can I create QTcpServer in another thread?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 249 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Pang
    wrote on last edited by
    #1

    I want to create qtcpserver in a separate netns. So I create it like this.

    QTcpServer *x11Server = nullptr;
    std::thread thread1([&]{
            if(setns(open("/var/run/UE", O_RDONLY), CLONE_NEWNET)){
                perror("setns");
                exit(-1);
            }
            x11Server = new QTcpServer;
            x11Server->listen(QHostAddress::Any, 6010);
        });
    connect(x11Server, &QTcpServer::newConnection, [=]{
    //todo
    });
    

    I put it in another thread to avoid setns influencing current main thread.
    In later code I will connect x11Server's sinal with something. Is this safe?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You definitely can create QTcpServer in another thread, it is safe and supported scenario. I am not sure if using std::thread for this purpose is good:

      • will it spin a new QEventLoop for that thread? I doubt it
      • connect() likely won't know that QueuedConnection has to be used - you need to remember to set it manually for all inter-thread connections (it's a good practice anyway)

      (Z(:^

      1 Reply Last reply
      4
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        Going from the user in this thread
        https://forum.qt.io/topic/59687/qthread-or-std-thread

        I would say using Signal&Slots from an std::thread is near impossible 🤷‍♂️


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        4
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Good to know, thanks.

          (Z(:^

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved