Results 1 to 11 of 11

Thread: Client-Server application using thread pool - implementation suggestions

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Posts
    7
    Thanks
    2

    Default Client-Server application using thread pool - implementation suggestions

    Hello all!

    I am trying to implement a server application using a thread pool. I'm using QTcpSocket to handle the connections between clients and server.

    I have a very rough sketch and I would like to have your opinions, on what I could/should change to reach best solution.

    So far I have a WorkerThread that should be in charge of processing clients requests. I also have a ThreadPool class that inherits QTcpServer and reimplements incomingConnection(int socket_descriptor).

    ThreadPool has a QList of WorkerThreads that are started in the constructor (ThreadPool(QObject parent = 0 ...).


    ThreadPool
    Qt Code:
    1. const unsigned int max_worker_threads;
    2.  
    3. class ThreadPool : public QTcpServer
    4. {
    5.  
    6. public:
    7. ThreadPool(QObject *parent = 0)
    8. : QTcpServer(parent),
    9. block_size(0),
    10. active_threads(0) {
    11.  
    12. foreach(WorkerThread worker, worker_threads) {
    13. worker.start();
    14. connect(&worker, SIGNAL(thread_finished(QTcpSocket *)),
    15. this, SLOT(worker_thread_finished(QTcpSocket *)));
    16. }
    17. }
    18.  
    19.  
    20. ~ThreadPool() {
    21.  
    22. }
    23.  
    24. //signals:
    25.  
    26. slots:
    27. void pass_job() {
    28.  
    29. QTcpSocket *_socket = static_cast<QTcpSocket *>(sender());
    30.  
    31. QDataStream in(_socket);
    32. in.setVersion(QDataStream::Qt_4_4);
    33.  
    34. if (block_size == 0) {
    35. if (_socket->bytesAvailable() < (int)sizeof(quint16))
    36. return;
    37. }
    38.  
    39. in >> block_size;
    40.  
    41. if (_socket->bytes_available() < block_size) {
    42. return;
    43. }
    44.  
    45. //there is enough data in the stream
    46.  
    47. //acquire lock
    48. //mutex.lock();
    49. if (active_threads < max_threads) {
    50. active_threads++;
    51. pending_requests.append(_socket);
    52. }
    53. else {
    54. //add "job" to the list
    55. pending_requests.append(_socket);
    56. }
    57. //release lock
    58. //mutex.unlock();
    59.  
    60. //wake on worker_thread to process the request
    61. wait.wakeOne();
    62.  
    63. }
    64.  
    65. void worker_thread_finished(QTcpSocket *socket) {
    66. //mutex.lock();
    67. active_threads--;
    68. pending_requests.removeAll(socket);
    69. //mutex.unlock();
    70. //responde to the next request
    71. serve_next_request();
    72. }
    73.  
    74.  
    75.  
    76. private:
    77.  
    78. void incomingConnection(int socket_descriptor) {
    79. QTcpSocket *socket = new QTcpSocket(socket_descriptor);
    80. client_list.append(socket);
    81. connect(socket, SIGNAL(readyRead()), this, SLOT(pass_job()));
    82. }
    83.  
    84. void serve_next_request() {
    85.  
    86. if (!pending_requests.isEmpty() && active_threads < max_worker_threads) {
    87. wait.wakeOne();
    88. }
    89. }
    90.  
    91.  
    92. QList<WorkerThread *> work_threads;
    93. QList<QTcpSocket *> client_list;
    94. QList<QTcpSocket *> pending_requests;
    95.  
    96. //QMutex mutex;
    97. quint16 block_size;
    98. unsigned int active_threads;
    99. ...
    100.  
    101. };
    To copy to clipboard, switch view to plain text mode 
    WorkerThread

    Qt Code:
    1. class WorkerThread : public QThread
    2. {
    3.  
    4. public:
    5. WorkerThread(QObject parent = 0) : QThread(parent) {
    6. //do initialization
    7.  
    8. }
    9.  
    10. ~WorkerThread() {
    11.  
    12. }
    13.  
    14. void run() {
    15. while(1) {
    16. mutex.lock();
    17. wait.wait(&mutex);
    18.  
    19. //read data from socket and process request
    20.  
    21. emit thread_finished();
    22. mutex.unlock();
    23. }
    24. }
    25.  
    26. signals:
    27. void thread_finished();
    28.  
    29.  
    30. private:
    31. QMutex mutex;
    32. //other variables
    33. }
    To copy to clipboard, switch view to plain text mode 
    (I know the code is very incomplete)
    Now I have this problem: How can I pass the socket to the working_thread in order for it to process that request??

    Any comments to my "implementation" are greatly appreciated!

    Thank you very much.
    Last edited by doctore; 22nd July 2009 at 13:24. Reason: Title change

Similar Threads

  1. Thread Ownership Problem
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 00:18
  2. QT + OpenGL + Thread => aaaahhhhh !
    By anthibug in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2008, 13:36
  3. Replies: 5
    Last Post: 17th January 2008, 21:49
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49

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.