Bonjour,

Je travaille actuellement sur une application multithread qui utilise un QSocket pour communiquer. Je rencontre certains plantages assez aléatoires que je n'arrive pas expliquer. En relisant la doc de Qt3, je suis tombé sur la partie Threads et QObject :

Threads and QObject subclasses

The QObject class itself is reentrant. However, certain rules apply when creating and using QObjects in a thread that is not the GUI thread.
1. None of the QObject based classes included in the Qt library are reentrant. This includes all widgets (e.g. QWidget and subclasses), OS kernel classes (e.g. QProcess, QAccel, QTimer), and all networking classes (e.g. QSocket, QDns).
2. QObject and all of its subclasses are not thread-safe. This includes the entire event delivery system. It is important to remember that the GUI thread may be delivering events to your QObject subclass while you are accessing the object from another thread. If you are using QObject in a thread that is not the GUI thread, and you are handling events sent to this object, you must protect all access to your data with a mutex; otherwise you may experience crashes or other undesired behavior.
3. As a corollary to the above, deleting a QObject while pending events are waiting to be delivered can cause a crash. You must not delete the QObject directly from a thread that is not the GUI thread. Use the QObject::deleteLater() method instead, which will cause the event loop to delete the object after all pending events have been delivered to the object.
Je fais des appels aux fonctions d'écriture sur le socket à l'intérieur de la fonction run() de mon thread. Le programme plante justement à cet endroit et çà a l'air lié au problème de réentrance décrit dans le point 1 !

Savez-vous si l'utilisation du mutex de QApplication pourrait m'aider à résoudre ce problème :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
void Test::run()
{
  ...
  qApp->lock();             // est-ce que l'accès au socket est thread-safe ? 
  socket->writeBlock(data, len); 
  qApp->unlock();
  ...
}
Je utilise cette méthode assez couramment pour certains affichages rapides dans l'interface graphique mais je ne suis pas persuadé qu'il s'applique aussi facilement pour la classe QSocket étant donné que le transfert sur le réseau est asynchrone :
Autrement dit, lorsque je sors de la fonction writeBlock, et que je libère le mutex, il se peut que le socket soit toujours utilisé au sein de mon thread, non ?

Autre question : savez-vous si les mêmes contraintes de reentrance s'appliquent dans Qt4 ?

Merci.