2017-08-08 4 views
0

スレッドをdetachとして起動しました。 main関数からスレッドを閉じるには?スレッドデタッチを閉じるにはC++?

void My() 
{ 
    // actions 
} 


void main() 
{ 

std::thread thr7(receive); 
thr7.detach(); 

// close ? 

} 
+5

カウンタの質問:なぜあなたはそれを取り外しましたか?一度切り離されると、それは...まあ...切り離された – user463035818

+1

そして、あなたは "クローズ"の意味を詳しく教えていただけますか?終了すべきスレッドに信号を与えたいですか? –

+1

これを閉じることはできません。プログラム的にその役割を終了する必要があります(つまり、関数はその目的を達成する必要があります)。たぶん、 '停止'ブール値(セマフォー)かそれと類似したもので。 –

答えて

3

あなたが正しく理解していれば、スレッドに無限ループを終了して終了するように伝えたいですか?

単純なブール値のstd::atomicオブジェクトがすべて必要です。

ある値(たとえばtrue)に初期化し、スレッドループ中でその値を初期化します。スレッドを終了させたい場合は、値を(falseに)変更し、次にスレッドループが反復すると、それに気づき、ループを解除してクリーンアップと終了を続けます。

+0

: ボイドマイ(){ 一方(end_thr){ //何らかのコード他 } { はstd ::アトミック。 } } 右か? –

+0

... 'main()'を残す前にスレッドが終了していることを確認するために 'main()' '' join() '(スレッドをアトミ​​ックに設定した後)' – Persixty

+1

@Persixtyデタッチされたスレッドでは不可能です。 –

2

あなたは、いくつかの他の方法を使用しない限り、一度detachが呼び出され、その親から直接スレッドjoinまたはterminateを呼び出すことはできません。

#include <atomic> 
#include <chrono> 
#include <condition_variable> 
#include <iostream> 
#include <mutex> 
#include <string> 
#include <thread> 

std::mutex mu; 
std::condition_variable cv; 
bool finished = false; 

void threadFunc() 
{ 
    while(!finished) 
    { 

     std:: cout << "Thread doing work \n"; 
     std::this_thread::sleep_for(std::chrono::milliseconds(5)); 
    } 

    std::cout << "End of Thread \n"; 
} 

int main() 
{ 

    { 
     std::thread t1(threadFunc); 
     t1.detach(); // Call `detach` to prevent blocking this thread 

    } // Need to call `join` or `detach` before `thread` goes out of scope 

    for (int i = 0; i < 5; ++i){ 
     std::this_thread::sleep_for(std::chrono::milliseconds(20)); 
     std::cout << "Main doing stuff: \n"; 
    } 
    std::cout << "Terminating the thread\n"; 

    std::unique_lock<std::mutex> lock(mu); 
    finished = true; 
    cv.notify_all(); 
    std::cout << "End of Main\n"; 
    return 0; 
} 

あなたはときにスレッドを伝えるために共有変数を使用します。

は、あなたが求めているものを行うための簡単な方法を示しべき(シンプルで非常にmeaninfulない上)次のコードを見てみましょうその実行を終了する。

2

あなたはスレッドを制御することができ、このような何か:

std::atomic_bool running = false; // set to stop thread 
std::atomic_bool closed = false; // set by thread to indicate it ended 

void detached_thread_function() 
{ 
    running = true; 

    // acquire resources 

    while(running) 
    { 
     std::cout << "running" << '\n'; 
     std::this_thread::sleep_for(std::chrono::seconds(1)); 
    } 

    // release resources 

    // set after all resources released 
    closed = true; 
} 

int main() 
{ 
    std::thread(detached_thread_function).detach(); 

    std::this_thread::sleep_for(std::chrono::seconds(3)); 

    std::cout << "stopping detached thread" << '\n'; 

    running = false; // stop thread 

    while(!closed) // you could code a timeout here 
     std::this_thread::sleep_for(std::chrono::milliseconds(10)); 
    // or use a condition variable? 

    std::cout << "end program" << '\n'; 
} 

スレッドがその機能を終了する合図され、スレッドは、主な機能は、それが終了するには安全である知っているようにフラグを設定します。

スレッドが複数ある場合は、アトミックカウンタを使用してゼロになると終了できます。