2012-05-10 29 views
10

Creo un thread in una funzione e in un'altra funzione, voglio interrompere questo thread. ho cercato in questo modo:Come interrompere/interrompere un boost :: thread?

class Server 
{ 
private: 
    boost::thread* mPtrThread; 
... 

public: 
    void createNewThread() 
    { 
     boost::thread t(...); 
     mPtrThread = &t; 
    } 


    void stopThread() 
    { 
     mPtrThread->interrupt(); 
    } 
} 

Ma non è work.How riuscivo a smettere il filo?

risposta

20

Se si desidera utilizzare interrupt(), è necessario definire interruption points. Il thread verrà interrotto dopo aver chiamato interrupt() non appena raggiunge uno dei punti di interruzione.

+0

Grazie mille! – wtm

17

Prima di tutto, in createNewThread() si dichiara una boost::thread t in un locale ambito e assegnare suo puntatore al membro della classe mPtrThread. Dopo le finiture createNewThread(), t viene distrutto e mPtrThread manterrà un puntatore illegale.

Preferisco usare qualcosa come mPtrThread = new boost::thread(...);

Si potrebbe anche voler leggere this article per ulteriori informazioni sul multithreading in Boost.

+1

Davvero un bell'articolo, aiuta molto. Grazie ~ – wtm

+0

Dovresti anche usare un puntatore intelligente per assicurarti che il comando boost :: thread venga cancellato. – allyourcode

Problemi correlati