Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++ C++;如何杀死或;刷新“;一根线?_C++_Multithreading - Fatal编程技术网

C++ C++;如何杀死或;刷新“;一根线?

C++ C++;如何杀死或;刷新“;一根线?,c++,multithreading,C++,Multithreading,假设我们有两个函数,“waitForShortestTime”和“addTime” 但是我想知道如何首先杀死当前线程。您考虑过使用条件变量吗?看 不要终止线程,而是向它发送一个信号,让它醒来,然后采取适当的行动 我高度推荐您购买并阅读Actudio中的强> C++并发,因为它对使用标准C++编写多线程代码提供了很多解释。 因此,每次您需要在任何现有事件之前向进程添加事件时,都可以发出停止该线程的信号,等待它停止并启动另一个线程 或者,您也可以简单地启动另一个线程,为新事件使用较短的计时器,并结合

假设我们有两个函数,“waitForShortestTime”和“addTime”


但是我想知道如何首先杀死当前线程。

您考虑过使用条件变量吗?看

不要终止线程,而是向它发送一个信号,让它醒来,然后采取适当的行动

<>我高度推荐您购买并阅读Actudio<<强> >中的<>强> C++并发,因为它对使用标准C++编写多线程代码提供了很多解释。 因此,每次您需要在任何现有事件之前向进程添加事件时,都可以发出停止该线程的信号,等待它停止并启动另一个线程

或者,您也可以简单地启动另一个线程,为新事件使用较短的计时器,并结合最大等待时间,这是一个很好的折衷方案,既可以在之前添加事件,也可以减少太多唤醒

例如,假设大多数作业在接下来的30分钟或更长时间内完成,而在接下来的5分钟内很少有新作业。然后,您可以告诉线程等待5分钟,当它醒来时,它会检查是否应该在接下来的5分钟结束之前安排任何作业,如果是,则使用较小的等待时间

或者,如果所需的精度不高(比如说,您需要在计划时间的一分钟或两分钟内进行检查),您只需始终等待最多该时间(1或2分钟),然后检查您现在是否有事情要做

实际上,也没有必要终止线程。您将其唤醒,让线程检查是否有事情要做,如果没有,下一次处理项目的时间是什么,并将其用于等待时间

该事件还可用于退出应用程序


有时,使用最大延迟有助于确保如果某个事件以某种方式丢失,数据将在一定时间内得到处理,从而使应用程序更充分地证明了这一点。此外,您还可以进行一些运行状况检查,例如,经过很长时间的延迟后,您仍然没有处理上一个作业(例如,您有一个导致无限循环的bug)。

这将完成您需要执行的操作。它目前需要fmt库,但这很容易更改。它使用一个条件变量来完成以下任务:

#include <fmt/core.h>
#include <iostream>
#include <thread>
#include <queue>
#include <vector>
#include <condition_variable>
#include <chrono>
#include <utility>
#include <string>
#include <mutex>

using timed_msg_t = ::std::pair<::std::chrono::system_clock::time_point,
                                 ::std::string>;

class time_smaller
{
 public:
   bool operator ()(timed_msg_t const &a, timed_msg_t const &b)
   {
      return b.first < a.first;
   }
};

using timer_queue = ::std::priority_queue<timed_msg_t,
                                          ::std::vector<timed_msg_t>,
                                          time_smaller>;

using ::std::mutex;
using ::std::condition_variable;
using ::std::thread;
using ::std::unique_lock;

static auto const start = ::std::chrono::system_clock::now();

void timer_queue_thread(mutex &queue_mutex,
                        condition_variable &queue_condition,
                        timer_queue &q)
{
   unique_lock<mutex> qlock(queue_mutex);
   while (!q.empty()) {
      auto now = ::std::chrono::system_clock::now();
      auto const &top = q.top();
      if (top.first <= now) {
         double milliseconds_from_start = (now - start) / ::std::chrono::milliseconds(1);
         ::fmt::print("At time: {}ms - \"{}\"\n", milliseconds_from_start, top.second);
         q.pop();
      } else {
         queue_condition.wait_until(qlock, top.first);
      }
   }
}

int main()
{
   timer_queue tq;
   mutex queue_mutex;
   condition_variable queue_condition;
   {
      unique_lock<mutex> qlock(queue_mutex);
      tq.emplace(start + 60 * ::std::chrono::seconds(1), "Starting event.");
   }
   ::std::thread timer_processing{timer_queue_thread, ::std::ref(queue_mutex), ::std::ref(queue_condition), ::std::ref(tq)};
   while (true) {
      int seconds;
      ::std::string msg;
      ::std::cout << "Enter a time and message:\n";
      ::std::cin >> seconds;
      getline(::std::cin, msg);
      {
         unique_lock<mutex> qlock(queue_mutex);
         auto const qtime = ::std::chrono::system_clock::now() + seconds * ::std::chrono::seconds(1);
         tq.emplace(qtime, msg);
         queue_condition.notify_one();
      }
   }
   return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用timed_msg_t=::std::pair;
上课时间
{
公众:
布尔运算符()(定时消息常量和a、定时消息常量和b)
{
返回b.first<代码> >不能在标准C++线程库中“杀死”。一个执行线程,在某种程度上。C++没有这样的工作。只是把短休眠放入一个循环中,测试当前时间中的一个易失性变量,并从另一个线程中更新易失性变量。条件变量有一个A<代码> WaITi直到使用了一个超时参数的方法。这将满足你的需要。我仍然质疑YO。您的设计,但可以利用它们使设计工作。@user207421-易失性变量与线程的混合非常差,因为它们在核心之间的CPU缓存一致性方面发挥得非常差。请改用
::std::atomic
。我没有意识到条件变量有超时。条件变量是正确的答案。不会打开到o但从长远来看,许多线程可能会导致问题?如何阻止新线程失控?我认为join是一种方法,但它会停止当前线程,并且不允许用户继续使用程序的主功能,这对用户不友好。好吧,主线程不必等待太多时间h、 将该项添加到队列并向处理线程发送信号。然后该线程将采取适当的操作。
Schedule mySchedule;
thread t(&Schedule::lookForShortTime, mySchedule); //thread is set up.
//user input
mySchedule.addTime(time); //new time is added by user.
//kill thread
thread t(&Schedule::lookForShortTime, mySchedule); //new thread to consider new time.
#include <fmt/core.h>
#include <iostream>
#include <thread>
#include <queue>
#include <vector>
#include <condition_variable>
#include <chrono>
#include <utility>
#include <string>
#include <mutex>

using timed_msg_t = ::std::pair<::std::chrono::system_clock::time_point,
                                 ::std::string>;

class time_smaller
{
 public:
   bool operator ()(timed_msg_t const &a, timed_msg_t const &b)
   {
      return b.first < a.first;
   }
};

using timer_queue = ::std::priority_queue<timed_msg_t,
                                          ::std::vector<timed_msg_t>,
                                          time_smaller>;

using ::std::mutex;
using ::std::condition_variable;
using ::std::thread;
using ::std::unique_lock;

static auto const start = ::std::chrono::system_clock::now();

void timer_queue_thread(mutex &queue_mutex,
                        condition_variable &queue_condition,
                        timer_queue &q)
{
   unique_lock<mutex> qlock(queue_mutex);
   while (!q.empty()) {
      auto now = ::std::chrono::system_clock::now();
      auto const &top = q.top();
      if (top.first <= now) {
         double milliseconds_from_start = (now - start) / ::std::chrono::milliseconds(1);
         ::fmt::print("At time: {}ms - \"{}\"\n", milliseconds_from_start, top.second);
         q.pop();
      } else {
         queue_condition.wait_until(qlock, top.first);
      }
   }
}

int main()
{
   timer_queue tq;
   mutex queue_mutex;
   condition_variable queue_condition;
   {
      unique_lock<mutex> qlock(queue_mutex);
      tq.emplace(start + 60 * ::std::chrono::seconds(1), "Starting event.");
   }
   ::std::thread timer_processing{timer_queue_thread, ::std::ref(queue_mutex), ::std::ref(queue_condition), ::std::ref(tq)};
   while (true) {
      int seconds;
      ::std::string msg;
      ::std::cout << "Enter a time and message:\n";
      ::std::cin >> seconds;
      getline(::std::cin, msg);
      {
         unique_lock<mutex> qlock(queue_mutex);
         auto const qtime = ::std::chrono::system_clock::now() + seconds * ::std::chrono::seconds(1);
         tq.emplace(qtime, msg);
         queue_condition.notify_one();
      }
   }
   return 0;
}