Results 1 to 2 of 2

Thread: Multithreading issue

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Multithreading issue

    hi everyone, i'm creating a tcp/ip server for a chat app. i want to use the threadpool to dedicate threads to connected clients but because QRunnable does not support signals and slots i have no clue how to implement the server. the portion of the code that needs to listen to the incoming connections is in the run function of my QRunnable object (i.e. myTask). The problem comes when i have to connect the "ReadyRead()" signal to a slot that is reponsible for reading data from the socket (Mind you, this slot runs on a separaten thread from the code in the "run" function of QRunnable). Because of this if i try connecting signals and slots inside the run function my server crashes when a client tries connecting to it. below is my run function without the connect function to connect the signals and slots. please assist with this if you knows how to solve this problem please share.
    Qt Code:
    1. void MyTask::run()
    2. {
    3. if(!socketDescriptor) return;
    4. QTcpSocket sock;
    5. sock.setSocketDescriptor(socketDescriptor);
    6.  
    7. sock.write("Hello Client");
    8. sock.flush();
    9. sock.waitForBytesWritten(5000);
    10. sock.close();
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading issue

    Aside from the obvious question why you want to do threading here at all, are you sure you want to limit the number of clients to the number of threads in the pool?

    Anyway, your run() function is very similar in context to main(), so, just like in main(), create a receiver object, connect to its slots and then either start the thread's event loop.
    Or use the synchronous approach and call waitForReadyRead().

    But you should really rethink the need to make this so complicated. Unless you are doing heavy processing of data sent from or to the client there is no need for threads. Which in the case of a chat client doesn't sound very likely.

    Cheers,
    _

Similar Threads

  1. Multithreading
    By havij000 in forum Newbie
    Replies: 22
    Last Post: 21st August 2013, 13:35
  2. When to use Multithreading?
    By "BumbleBee" in forum General Programming
    Replies: 5
    Last Post: 30th April 2011, 18:21
  3. regarding multithreading
    By mohanakrishnan in forum Qt Programming
    Replies: 19
    Last Post: 9th December 2009, 08:21
  4. multithreading
    By mickey in forum General Programming
    Replies: 2
    Last Post: 5th June 2008, 22:01
  5. MultiThreading in Qt
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 7th November 2006, 19:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.