在c ++中停止定期的线程事物

时间:2018-02-14 18:00:22

标签: c++ multithreading function intervals

所以我有这个函数,它的行为类似于JS中的setInterval函数。我发现它here。 我目前正在尝试更改它,以便它可以停止。我不完全理解这段代码的行为。

void setInterval(function<void(void)> func, unsigned int interval) {
  thread([func, interval]() {
    while (1) {
      auto x = chrono::steady_clock::now() + chrono::milliseconds(interval);
      func();
      this_thread::sleep_until(x);
    }
  }).detach();
}

我试过这样:

void setInterval(function<void(void)> func, unsigned int interval, bool &b) {
  thread([func, interval, *b]() {
    while (*b) {
      auto x = chrono::steady_clock::now() + chrono::milliseconds(interval);
      func();
      this_thread::sleep_until(x);
    }
  }).detach();
}

(这不会编译),并在主要调用它:

bool B;
setInterval(myFunction,1000,B);

我期待如果我将B变量更改为false,那么setInterval函数中的线程会停止,但我还没有设法达到我的目标。任何想法/建议?提前谢谢。

1 个答案:

答案 0 :(得分:0)

很抱歉,但我没有找到比这更简单的设计。

你可以,创建一个既拥有一个线程又拥有一个weak_ptr的类, 成为一个“持有者”,可调用者可以安全地看到它,因为可调用 即使对象被破坏,它仍然存在。你不想要一个悬垂的指针。

template<typename T>
struct IntervalRepeater {
    using CallableCopyable = T;
private:
    weak_ptr<IntervalRepeater<CallableCopyable>> holder;
    std::thread theThread;

    IntervalRepeater(unsigned int interval,
            CallableCopyable callable): callable(callable), interval(interval) {}

    void thread() {
        weak_ptr<IntervalRepeater<CallableCopyable>> holder = this->holder;
        theThread = std::thread([holder](){
            // Try to strongify the pointer, to make it survive this loop iteration,
            //    and ensure that this pointer is valid, if not valid, end the loop.
            while (shared_ptr<IntervalRepeater<CallableCopyable>> ptr = holder.lock()) {
                auto x = chrono::steady_clock::now() + chrono::milliseconds(ptr->interval);
                ptr->callable();
                this_thread::sleep_until(x);
            }
        });
    }

public:
    const CallableCopyable callable;
    const unsigned int interval;

    static shared_ptr<IntervalRepeater<T>> createIntervalRepeater(unsigned int interval,
            CallableCopyable callable) {
        std::shared_ptr<IntervalRepeater<CallableCopyable>> ret =
                shared_ptr<IntervalRepeater<CallableCopyable>>(
                        new IntervalRepeater<CallableCopyable>(interval, callable));
        ret->holder = ret;
        ret->thread();

        return ret;
    }

    ~IntervalRepeater() {
        // Detach the thread before it is released.
        theThread.detach();
    }

};

void beginItWaitThenDestruct() {
    auto repeater = IntervalRepeater<function<void()>>::createIntervalRepeater(
            1000, [](){ cout << "A second\n"; });
    std::this_thread::sleep_for(std::chrono::milliseconds(3700));
}

int main() {
    beginItWaitThenDestruct();
    // Wait for another 2.5 seconds, to test whether there is still an effect of the object
    //   or no.
    std::this_thread::sleep_for(std::chrono::milliseconds(2500));
    return 0;
}

C ++不是JavaScript,但C ++可以应用不同语言的大多数编程范例。