Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何销毁std::thread显式创建的线程?_C++_Windows_Multithreading - Fatal编程技术网

C++ 如何销毁std::thread显式创建的线程?

C++ 如何销毁std::thread显式创建的线程?,c++,windows,multithreading,C++,Windows,Multithreading,我希望两个线程最初都在无限循环中运行,但过了一段时间后,我希望在不等待第一个线程完成的情况下终止第一个线程,第二个线程应该正常运行。我如何实现它? 当我运行下面的代码时,它给出了调试错误 void f1(int i) { while (1) { printf("Executng Thread %d : %d\n", i, j); Sleep(10); } } int main() { std::th

我希望两个线程最初都在无限循环中运行,但过了一段时间后,我希望在不等待第一个线程完成的情况下终止第一个线程,第二个线程应该正常运行。我如何实现它? 当我运行下面的代码时,它给出了调试错误

void f1(int i)
{
    while (1)
     {
             printf("Executng Thread  %d : %d\n", i, j);
            Sleep(10);
    }

}


int main()
{   
    std::thread t1(f1, 1);  
    std::thread t2(f1, 2);

    Sleep(100);
     t1.~thread();  
    while (1)
    {

        Sleep(10);
    }
    return 0;
}

程序按照一组控制流执行一组确定的指令。您不能随意中断此控制流,仍然对程序行为进行推理

就像你不能说“运行我的程序,但不能超过10秒”而程序不知道这一点并产生有意义的输出一样,你不能在线程不知道它并产生有意义的程序状态的情况下杀死线程。简而言之,“线程取消需要合作”

有一种简单的方法可以让线程协作:使用共享标志

#include <atomic>
#include <chrono>
#include <thread>

std::atomic<bool> thread_1_must_end(false);

void foo(int thread_id)
{
    while (true)
    {
        if (thread_id == 1 && thread_1_must_end.load()) { break; }
        // do stuff
    }
}

int main()
{
    using std::literals::chrono_literals;

    std::thread t1(foo, 1), t2(foo, 2);
    std::this_thread::sleep_for(500ms);

    thread_1_must_end.store(true);
    t1.join();  // 1

    // ...

}
#包括
#包括
#包括
std::原子线程1必须结束(false);
void foo(int-thread\u-id)
{
while(true)
{
if(thread_id==1&&thread_1_必须_end.load()){break;}
//做事
}
}
int main()
{
使用std::literals::chrono_literals;
螺纹t1(foo,1),t2(foo,2);
std::此线程::睡眠时间(500ms);
线程1必须结束.store(true);
t1.join();//1
// ...
}

请注意,此过程仍然是合作的。
join()。对此没有时间保证。如果线程函数忽略该标志,则无法与其通信。您有责任为所有线程函数提供足够的工具,以便在结束时进行学习。

程序按照一组控制流执行一组确定的指令。您不能随意中断此控制流,仍然对程序行为进行推理

就像你不能说“运行我的程序,但不能超过10秒”而程序不知道这一点并产生有意义的输出一样,你不能在线程不知道它并产生有意义的程序状态的情况下杀死线程。简而言之,“线程取消需要合作”

有一种简单的方法可以让线程协作:使用共享标志

#include <atomic>
#include <chrono>
#include <thread>

std::atomic<bool> thread_1_must_end(false);

void foo(int thread_id)
{
    while (true)
    {
        if (thread_id == 1 && thread_1_must_end.load()) { break; }
        // do stuff
    }
}

int main()
{
    using std::literals::chrono_literals;

    std::thread t1(foo, 1), t2(foo, 2);
    std::this_thread::sleep_for(500ms);

    thread_1_must_end.store(true);
    t1.join();  // 1

    // ...

}
#包括
#包括
#包括
std::原子线程1必须结束(false);
void foo(int-thread\u-id)
{
while(true)
{
if(thread_id==1&&thread_1_必须_end.load()){break;}
//做事
}
}
int main()
{
使用std::literals::chrono_literals;
螺纹t1(foo,1),t2(foo,2);
std::此线程::睡眠时间(500ms);
线程1必须结束.store(true);
t1.join();//1
// ...
}

请注意,此过程仍然是合作的。
join()。对此没有时间保证。如果线程函数忽略该标志,则无法与其通信。您有责任为所有线程函数提供足够的工具,以便在结束时学习。

您可以通过拔下计算机插头来实现“杀死某些东西”。如果您希望能够对程序的行为进行决定性的推理,那么您需要实现一个适当的控制流,而不仅仅是四处杀戮。@KerrekSB那么您就必须在单独的计算机上运行每个线程。可能会很简单吗@约瑟夫曼斯菲尔德:我认为,一旦你的程序是非确定性的,你就不能真正区分一个线程被杀死和所有线程被杀死(通常)。因此,拔掉插头确实达到了预期的效果,并且仅仅使用语言规则无法与实现此效果的任何其他方式区分开来:-)您可以通过从计算机上拔掉插头来实现“杀死某些东西”。如果您希望能够对程序的行为进行决定性的推理,那么您需要实现一个适当的控制流,而不仅仅是四处杀戮。@KerrekSB那么您就必须在单独的计算机上运行每个线程。可能会很简单吗@约瑟夫曼斯菲尔德:我认为,一旦你的程序是非确定性的,你就不能真正区分一个线程被杀死和所有线程被杀死(通常)。因此,拔出插头实际上确实达到了预期的效果,并且仅使用语言规则无法与实现此效果的任何其他方式区分开来:-)我有一个问题,“join()操作(标记为//1)会阻塞,直到线程函数循环并检查标志”。在标志为false且另一个线程退出无限循环之前,它不会阻止线程吗?@Vinzenz:我不确定我是否遵循了…:-S两个线程同时运行同一个线程函数。在这个例子中,我甚至没有处理线程2。听起来第一个线程只会“等待”t1线程,直到它“循环并检查标志”。不管该标志是真是假,它都将继续异步到t1线程。我不熟悉std:atomic,所以我不知道线程是否是这样运行的。@Vinzenz:thread1在看到
thread\u 1\u must\u end
等于
true
时退出(通过断开循环)。这就是
t1.join()
返回的时间。对,我也是这样学的。我仍然没有意识到这一点,因为您通常使用线程来运行异步或并行的东西。。。所以我总是分离线程我有一个问题,“join()操作(标记为//1)阻塞,直到线程函数循环并检查标志”。在标志为false且另一个线程退出无限循环之前,它不会阻止线程吗?@Vinzenz:我不确定我是否遵循了…:-S两个线程同时运行同一个线程函数。在这个例子中,我甚至没有处理线程2。听起来第一个线程只会“等待”t1线程,直到它“循环”