C++ 从另一个线程终止线程c++;
我是多线程新手,我需要你的帮助。 考虑下面的代码:C++ 从另一个线程终止线程c++;,c++,multithreading,C++,Multithreading,我是多线程新手,我需要你的帮助。 考虑下面的代码: vector <int> vec; int j = 0; void Fill() { for (int i = 0; i < 500; i++) { Sleep(500); vec.push_back(i); } } void Proces() { int count = 0; int n=-1; while (true) {
vector <int> vec;
int j = 0;
void Fill()
{
for (int i = 0; i < 500; i++)
{
Sleep(500);
vec.push_back(i);
}
}
void Proces()
{
int count = 0;
int n=-1;
while (true) {
Sleep(250);
if (!vec.empty())
{
if (n != vec.back()) {
n = vec.back();
cout << n;
count++;
}
}
if (count == 101)break;
}
}
void getinput()
{
while (true) {
int k=0;
cin >> k;
//if the user enters an integer i want to kill all the threads
}
}
int main()
{
thread t1(Fill);
thread t2(Proces);
thread t3(getinput);
t1.join();
t2.join();
t3.join();
cout << "From main()";
}
vec;
int j=0;
填空()
{
对于(int i=0;i<500;i++)
{
睡眠(500);
向量推回(i);
}
}
无效过程()
{
整数计数=0;
int n=-1;
while(true){
睡眠(250);
如果(!vec.empty())
{
如果(n!=vec.back()){
n=向量返回();
cout>k;
//如果用户输入一个整数,我想终止所有线程
}
}
int main()
{
螺纹t1(填充);
线程t2(进程);
线程t3(getinput);
t1.join();
t2.连接();
t3.join();
coutstd::原子出口标志{false};
...
填空(){
对于(int i=0;i<500;i++){
如果(退出标志)返回;
...
}
}
无效过程(){
while(true){
如果(退出标志)返回;
...
}
}
void getinput(){
while(true){
...
如果(/*用户输入一个整数,我想终止所有线程*/)
退出标志=真;
}
}
使线程退出的一种常见方法是使用()标志,线程检查该标志是否应该退出。然后在外部设置该标志,线程将注意到它并自然退出
差不多
#include <thread>
#include <atomic>
#include <iostream>
#include <chrono>
// Flag telling the thread to continue or exit
std::atomic<bool> exit_thread_flag{false};
void thread_function()
{
// Loop while flag if not set
while (!exit_thread_flag)
{
std::cout << "Hello from thread\n";
std::this_thread::sleep_for(std::chrono::seconds(1)); // Sleep for one second
}
}
int main()
{
std::thread t{thread_function}; // Create and start the thread
std::this_thread::sleep_for(std::chrono::seconds(5)); // Sleep for five seconds
exit_thread_flag = true; // Tell thread to exit
t.join(); // Wait for thread to exit
}
#包括
#包括
#包括
#包括
//指示线程继续或退出的标志
原子退出线程标志{false};
void thread_函数()
{
//如果未设置,则循环while标志
而(!退出线程标志)
{
std::cout在访问容器之前,您必须定义一个退出条件并锁定容器。当然,您可以使用适当的锁定在现有容器周围构建自己的集合作为包装器,从而使其线程安全
以下是锁定和退出条件的示例:
class Test
{
public:
Test()
: exitCondition(false)
{
work = std::thread([this]() { DoWork(); });
}
~Test()
{
if (work.joinable())
work.join();
}
void Add(int i)
{
mutex.lock();
things.push_back(i);
mutex.unlock();
}
void RequestStop(bool waitForExit = false)
{
exitCondition.exchange(true);
if (waitForExit)
work.join();
}
private:
void DoWork()
{
while (!exitCondition)
{
mutex.lock();
if (!things.empty())
{
for (auto itr = things.begin(); itr != things.end();)
itr = things.erase(itr);
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
mutex.unlock();
}
}
private:
std::vector<int> things;
std::thread work;
std::atomic<bool> exitCondition;
std::mutex mutex;
};
int wmain(int, wchar_t**)
{
Test t;
t.Add(1);
t.Add(2);
t.Add(3);
t.RequestStop(true);
return 0;
}
类测试
{
公众:
测试()
:exitCondition(false)
{
work=std::thread([this](){DoWork();});
}
~Test()
{
if(work.joinable())
work.join();
}
无效添加(int i)
{
mutex.lock();
事物。推回(i);
mutex.unlock();
}
void RequestStop(bool waitForExit=false)
{
exitCondition.exchange(true);
如果(waitForExit)
work.join();
}
私人:
无效销钉()
{
而(!exitCondition)
{
mutex.lock();
如果(!things.empty())
{
for(auto-itr=things.begin();itr!=things.end();)
itr=事物。擦除(itr);
}
std::this_线程::sleep_for(std::chrono::毫秒(1));
mutex.unlock();
}
}
私人:
向量事物;
螺纹加工;
std:原子存在条件;
std::互斥互斥;
};
国际气象局(国际气象局,世界气象局**)
{
试验t;
t、 增加(1);
t、 增加(2);
t、 增加(3);
t、 请求停止(true);
返回0;
}
在没有同步的情况下(通过std::mutex
),您不能以这种方式从多个线程使用std::vector
。至于“killing”,您可以使用一个std::atomic
变量,该变量将在t3
中设置,并在t1
和t2
中检查。只需“killing”线程通常是坏的。更好的解决方案是让线程退出,通过一个原子标志,线程会定期检查。@DanielLangr问题是,如果有输入,我必须检查向量,如果有,我必须做些什么it@Someprogrammerdude如何请求线程退出?@J.Rusev这不会改变任何事情.std::vector
通常不是线程安全的。谢谢你的例子!帮了我很多忙。而不是在“kill”周围加上吓人的引号,你应该强化这样一个观念,即线程总是需要彼此合作。你不应该考虑杀死一个线程:你应该考虑礼貌地要求线程清理并停止。@jameslarge好主意,改写了答案。如果标记在recv
中被阻塞,线程怎么能注意到它是否改变了?@CristianTraìna Use非阻塞套接字和套接字轮询?
class Test
{
public:
Test()
: exitCondition(false)
{
work = std::thread([this]() { DoWork(); });
}
~Test()
{
if (work.joinable())
work.join();
}
void Add(int i)
{
mutex.lock();
things.push_back(i);
mutex.unlock();
}
void RequestStop(bool waitForExit = false)
{
exitCondition.exchange(true);
if (waitForExit)
work.join();
}
private:
void DoWork()
{
while (!exitCondition)
{
mutex.lock();
if (!things.empty())
{
for (auto itr = things.begin(); itr != things.end();)
itr = things.erase(itr);
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
mutex.unlock();
}
}
private:
std::vector<int> things;
std::thread work;
std::atomic<bool> exitCondition;
std::mutex mutex;
};
int wmain(int, wchar_t**)
{
Test t;
t.Add(1);
t.Add(2);
t.Add(3);
t.RequestStop(true);
return 0;
}