从另一个线程c ++杀死线程

时间:2018-02-23 08:30:19

标签: 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) {
        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()";


}

关键是我要从t3(getinput)中删除t1(填充)和t2(进程)。是否有方法可以做到,如果有可能请发帖和示例。

3 个答案:

答案 0 :(得分:1)

使线程退出的常用方法是使用(atomic)标志,线程检查它是否应该退出。然后在外部设置此标志,线程将注意到它并自然退出。

这样的东西
#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
}

答案 1 :(得分:1)

您必须定义退出条件并在访问之前锁定容器。当然,您可以使用适当的锁定来构建一个自己的集合作为现有包装,从而使其成为线程安全的。

以下是锁定和退出条件的示例:

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;
}

答案 2 :(得分:0)

std::atomic<bool> exit_flag{false};
...
void Fill() {
   for (int i = 0; i < 500; i++) {
      if (exit_flag) return;
      ...
   }
}

void Proces() {
   while (true) {
      if (exit_flag) return;
      ...
   }
}

void getinput() {
   while (true) {
      ...
      if ( /* the user enters an integer i want to kill all the threads */ )
         exit_flag = true;
   }
}