English 中文(简体)
C++11 虚拟成员功能的read
原标题:C++11 thread doesn t work with virtual member function

我试图找一个班子,read子将叫“Tick()”的虚拟成员功能。 然后,我试图打上一个班子,超越了基数:Tick()。

but when execute, the program just call the base class s Tick instead of override one. any solutions?

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

using namespace std;

class Runnable {
 public:
  Runnable() : running_(ATOMIC_VAR_INIT(false)) {

   }
  ~Runnable() { 
    if (running_)
      thread_.join();
  }
  void Stop() { 
    if (std::atomic_exchange(&running_, false))
      thread_.join();
  }
  void Start() {
    if (!std::atomic_exchange(&running_, true)) {
      thread_ = std::thread(&Runnable::Thread, this);
    }
  }
  virtual void Tick() {
    cout << "parent" << endl;
  };
  std::atomic<bool> running_;

 private:
  std::thread thread_;
  static void Thread(Runnable *self) {
    while(self->running_) {
      self->Tick();
      std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
  }
};

class Fn : public Runnable {
 public:
  void Tick() {
    cout << "children" << endl;
  }
};

int main (int argc, char const* argv[])
{
  Fn fn;
  fn.Start();
  return 0;
}

产出:

parent
最佳回答

在你完成使用该物体之前,你可以让物体脱离范围! <代码>return 0; at the end of main causes f to go out of scope. 因此,到您开始打电话到<条码>>>>>时,该物体甚至没有任何其他保障。

(~Runnable中的逻辑完全破碎。) 在路过晚的时候,该物体至少已经部分销毁。

问题回答

The approach of using inheritance with the parent serving as control for the thread and the children implementing the functions is a bad idea in general. The common problems with this approach come from construction and destruction:

  • 如果胎面是从母(控制)的建筑商开始的,那么,在构造完成之前,胎面可能开始运行,在全部物体完全构造之前,透镜可能称作虚拟功能。

  • 如果 parent子在母子的 des子上被阻断,那么当控制与胎面合并时,read子正在对已经不复存在的物体实施一种方法。

在你的特殊情况下,你正在打第二个案子。 方案开始执行,<代码>main 第二版已经开始。 此时主线与新发射的正线之间有分歧,如果新路面较快(例如,由于开始透镜是一种昂贵的操作),它将称作成员方法<代码>Tick,该方法将发送给最后压倒性代码

但是,如果主线较快,它将退出<代码>-main的范围,并开始销毁该物体,它将彻底销毁<代码>。 Fn Object and during Construction of the Runnable it will join the thread. 如果主线已经足够快,它将在二读之前进入<代码>join,并等待第二版在now <><>>>上打。 可操作:Tick。 请注意:未界定的<> /em>,而且由于第二版正在接触正在销毁的物体,因此没有得到保证。

Also, there are other possible orderings, like for example, the second thread could dispatch to Fn::Tick before the main thread starts destruction, but might not complete the function before the main thread destroys the Fn sub object, in which case your second thread would be calling a member function on a dead object.

You should rather follow the approach in the C++ standard: separate the control from the logic, fully construct the object that will be run and pass it to the thread during construction. Note that this is the case of Java s Runnable, which is recommended over extending the Thread class. Note that from a design point of view this separation makes sense: the thread object manages the execution, and the runnable is the code to execute. A thread is not a ticker, but rather what controls the execution of the ticker. And in your code Runnable is not something that can be run, but rather something that runs other objects that happen to derive from it.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?