Gethostbyname non-bloquant

helgrindhelgrind Membre
15:50 modifié dans API AppKit #1
Bonjour,

j'utilise gethostbyname() pour résoudre un nom de domaine pouvoir ensuite me connecter à  un serveur en utilisant les sockets BSD.

Je lance donc tout ça dans un nouveau thread pour empêcher de bloquer l'interface.
J'aimerais pouvoir annuler la connexion à  tout moment.

Problème: si je ne suis pas connecté à  internet, gethostbyname() bloque un certain temps avant de me renvoyer l'erreur.
Comment puis-je faire pour l'annuler?

Réponses

  • helgrindhelgrind Membre
    15:50 modifié #2
    Ou plus généralement, comment tuer le thread depuis l'extérieur?
  • Philippe49Philippe49 Membre
    juillet 2008 modifié #3
    Dans Threading Programming Guide

    Terminating a Thread

    The recommended way to exit a thread is to let it exit its entry point routine normally. Although Cocoa, POSIX, and Multiprocessing Services offer routines for killing threads directly, the use of such routines is strongly discouraged. Killing a thread prevents that thread from cleaning up after itself. Memory allocated by the thread could potentially be leaked and any other resources currently in use by the thread might not be cleaned up properly, creating potential problems later.

    If you anticipate the need to terminate a thread in the middle of an operation, you should design your threads from the outset to respond to a cancel or exit message. For long-running operations, this might mean stopping work periodically and checking to see if such a message arrived. If a message does come in asking the thread to exit, the thread would then have the opportunity to perform any needed cleanup and exit gracefully; otherwise, it could simply go back to work and process the next chunk of data.

    One way to respond to cancel messages is to use a run loop input source to receive such messages. Listing 3-2 shows the structure of how this code might look in your thread's main entry routine. (The example shows the main loop portion only and does not include the steps for setting up an autorelease pool or configuring the actual work to do.) The example installs a custom input source on the run loop that presumably can be messaged from another one of your threads; for information on setting up input sources, see “Configuring Run Loop Sources.” After performing a portion of the total amount of work, the thread runs the run loop briefly to see if a message arrived on the input source. If not, the run loop exits immediately and the loop continues with the next chunk of work. Because the handler does not have direct access to the exitNow local variable, the exit condition is communicated through a key-value pair in the thread dictionary.


    Il suit un exemple.
Connectez-vous ou Inscrivez-vous pour répondre.