[Qt-interest] QTcpServer in fortuneserver example

Sean Harmer sean.harmer at maps-technology.com
Wed Feb 2 14:40:25 CET 2011


Hi,

On Wednesday 02 February 2011 12:26:42 pmqt71 wrote:
> the fortuneserver in qt 4.6.2 uses QTcpServer and accepts client
> connections without using threads :
> 
> 
>    tcpServer = new QTcpServer(this);
>    if (!tcpServer->listen()) {
>        ...
>    }
> 
>    ...
>    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
>    ...
> 
>    void Server::sendFortune()
>    {
>        QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
>        ...
>        clientConnection->write(block);
>    }
> 
> 
> My question is: in this example, what QTcpServer does in case of concurrent
> client connections? Is it able to serialize them? The sendFortune method
> can access shared memory without protecting the critical section?

Since there is only one thread the calls to sendFortune() are serialised by 
definition. Do not confuse asynchronous operation of the socket classes with 
threading and serialisation of requests. 

You only need to introduce threads if there is too much for a single thread to 
handle in a reasonable time-frame. Only when you introduce additional threads 
do you have to worry about protecting against concurrent access to memory and 
other resources.

Cheers,

Sean



More information about the Qt-interest-old mailing list