我已经达到了我的项目中的一个要点,它需要在可以写入的资源上的线程之间进行通信,因此同步是必须的.但是,除了基本级别之外,我并不真正理解同步.
考虑此链接中的最后一个示例:http://www.bogotobogo.com/cplusplus/C11/7_C11_Thread_Sharing_Memory.php
#include <iostream>
#include <thread>
#include <list>
#include <algorithm>
#include <mutex>
using namespace std;
// a global variable
std::list<int>myList;
// a global instance of std::mutex to protect global variable
std::mutex myMutex;
void addToList(int max, int interval)
{
// the access to this function is mutually exclusive
std::lock_guard<std::mutex> guard(myMutex);
for (int i = 0; i < max; i++) {
if( (i % interval) == 0) myList.push_back(i);
}
}
void printList()
{
// the access to this function …Run Code Online (Sandbox Code Playgroud) 我试图使用抽象类来表示子类型的公共基础.但是,无论我做什么,它(看起来像是链接器)都会对vtable和未定义的引用保持呻吟.根据错误消息判断,问题必须以某种方式与析构函数相关.Wierdldy足够,它一直在谈论一个
"未定义引用'AbstractBase :: ~AbstractBase()'"
在child.cpp中没有任何意义.
就像上次一样,我实际上无法显示我的代码,所以这里有一个实际上做同样事情的例子:
首先是抽象类"AbstractBase.h":
#ifndef ABSTRACTBASE
#define ABSTRACTBASE
class AbstractBase
{
public:
virtual ~AbstractBase() = 0;
}
#endif
Run Code Online (Sandbox Code Playgroud)
使用abstractbase的子节点"child.h":
#ifndef CHILD
#define CHILD
class child : public AbstractBase
{
public:
~child() override;
}
#endif
Run Code Online (Sandbox Code Playgroud)
"child.cpp"中的实现:
#include "child.h"
child::~child()
Run Code Online (Sandbox Code Playgroud)
显然有更多的功能,但实质上这就是我的真正的类的析构函数的外观.
在网上搜索了在C++中使用抽象类的方法之后,我即将放弃.据我从这些消息来源可以看出,这是做到这一点的方法.您声明您的摘要类的析构函数是虚拟的,因此对它的任何调用都将包含该子项.孩子的析构函数只是标记为覆盖.它应该没有任何其他东西.
我错过了一些真正重要的东西吗?
PS:加入MCVE:
class AbstractBase
{
public:
virtual ~AbstractBase() = 0;
};
class child : public AbstractBase
{
public:
void dostuff()
{
//stuff
}
~child() override
{}
}
int main (argc, char *argv[])
{
child* ptr = new …Run Code Online (Sandbox Code Playgroud)