Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Fonctions  · 

QThread Class Reference

The QThread class provides platform-independent threads. More...

#include <qthread.h>

Inherits Qt.

List of all member functions.

Public Members

Static Public Members

Protected Members

  • virtual void run () = 0

Static Protected Members

  • void sleep ( unsigned long secs )
  • void msleep ( unsigned long msecs )
  • void usleep ( unsigned long usecs )

Detailed Description

The QThread class provides platform-independent threads.

A QThread represents a separate thread of control within the program; it shares data with all the other threads within the process but executes independently in the way that a separate program does on a multitasking operating system. Instead of starting in main(), QThreads begin executing in run(). You inherit run() to include your code. For example:

    class MyThread : public QThread {

    public:

        virtual void run();

    };

    void MyThread::run()
    {
        for( int count = 0; count < 20; count++ ) {
            sleep( 1 );
            qDebug( "Ping!" );
        }
    }

    int main()
    {
        MyThread a;
        MyThread b;
        a.start();
        b.start();
        a.wait();
        b.wait();
    }
    

This will start two threads, each of which writes Ping! 20 times to the screen and exits. The wait() calls at the end of main() are necessary because exiting main() ends the program, unceremoniously killing all other threads. Each MyThread stops executing when it reaches the end of MyThread::run(), just as an application does when it leaves main().

See also Thread Support in Qt, Environment Classes and Threading.


Member Function Documentation

QThread::QThread ()

Constructs a new thread. The thread does not begin executing until start() is called.

QThread::~QThread () [virtual]

QThread destructor.

Note that deleting a QThread object will not stop the execution of the thread it represents. Deleting a running QThread (i.e. finished() returns FALSE) will probably result in a program crash. You can wait() on a thread to make sure that it has finished.

Qt::HANDLE QThread::currentThread () [static]

This returns the thread handle of the currently executing thread.

Warning: The handle returned by this function is used for internal purposes and should not be used in any application code. On Windows, the returned value is a pseudo handle for the current thread, and it cannot be used for numerical comparison.

void QThread::exit () [static]

Ends the execution of the calling thread and wakes up any threads waiting for its termination.

bool QThread::finished () const

Returns TRUE is the thread is finished; otherwise returns FALSE.

void QThread::msleep ( unsigned long msecs ) [static protected]

System independent sleep. This causes the current thread to sleep for msecs milliseconds

void QThread::postEvent ( QObject * receiver, QEvent * event ) [static]

Provides a way of posting an event from a thread which is not the event thread to an object.

This is achieved as follows:

  • The event is put into a queue;
  • the event thread is woken up;
  • the event thread sends the event to the receiver object.
It is important to note that the event handler for the event, when called, will be called from the event thread and not from the thread calling QThread::postEvent().

Since QThread::postEvent() posts events into the event queue of QApplication, you must create a QApplication object before calling QThread::postEvent().

The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted.

See also QApplication::postEvent().

void QThread::run () [pure virtual protected]

This method is pure virtual, and must be implemented in derived classes in order to do useful work. Returning from this method will end the execution of the thread.

See also wait().

bool QThread::running () const

Returns TRUE if the thread is running; otherwise returns FALSE.

void QThread::sleep ( unsigned long secs ) [static protected]

System independent sleep. This causes the current thread to sleep for secs seconds.

void QThread::start ()

This begins the execution of the thread by calling run(), which should be reimplemented in a QThread subclass to contain your code. If you try to start a thread that is already running, this call will wait until the thread has finished, and then restart the thread.

void QThread::usleep ( unsigned long usecs ) [static protected]

System independent sleep. This causes the current thread to sleep for usecs microseconds

bool QThread::wait ( unsigned long time = ULONG_MAX )

This provides similar functionality to POSIX pthread_join. A thread calling this will block until either of these conditions is met:
  • The thread associated with this QThread object has finished execution (i.e. when it returns from run()). This function will return TRUE if the thread has finished. It also returns TRUE if the thread has not been started yet.
  • time milliseconds has elapsed. If time is ULONG_MAX (the default), then the wait will never timeout (the thread must return from run()). This function will return FALSE if the wait timed out.

This file is part of the Qt toolkit. Copyright © 1995-2002 Trolltech. All Rights Reserved.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 47
  4. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  5. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 12
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
Page suivante

Le blog Digia au hasard

Logo

Une nouvelle ère d'IHM 3D pour les automobiles

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 3.0
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web