【发布时间】:2014-06-13 15:46:21
【问题描述】:
我有一个类层次结构:
struct A 和 struct B,存储在 A 的实例中。
我运行 A 的一个方法,它运行 B 的一个方法,它与下载的连接信号异步下载一些东西到 B 的插槽。
之后我不再使用 A 和 B 实例。它们保存在向量中。
我需要的是从对象 B 获取有关下载完成的信息(来自 B 的插槽运行方法 A 以通知它并将其数据保存到 A)。通知后,不再需要B的实例(它存储了很多数据,所以我需要清除它)。但是没有其他线程知道什么时候应该完成!
调用槽B的线程不能清除B对象,因为delete this的危险。即使该线程设置了一些互斥体(可以放置在 A 中),而另一个一直在等待该互斥体的线程将删除它 - 这也是危险的,因为插槽的线程可能仍在运行。
那么,如何在 B 通知槽内安全地删除 B 实例?
我尝试在这里创建代码示例(B - 下载器,A - 存储):
struct Storage
{
Downloader *d; // createrd in ctor
Data data; // only value, not pointer (for some reasons).
//The same in 'Downloader'
int downloadedFiles; // 0 by ctor
void run() // think, main()
{
d->download(); // there is an array in my program.
//Here is one Downloader*, because it is example
}
void finishedDownload()
{
++downloadedFiles;
data = a->data;
// delete d; // wish, it would be done like that :(
// But the thread will be back to a->download()
}
}
struct Downloader
{
Data data;
Internet internet;
Storage *parent;
void download()
{
internet.set('http://someurl.dat');
connect( &internet, SIGNAL(downloaded()), this, SLOT(downloaded()) );
internet.download(&data); // async
}
public slots :
void downloaded()
{
parent->finishedDownload();
// And there is the finish of the thread, which was created by SIGNAL of Interget,
//but I need to delete 'Data data' from this struct.. How?
}
};
【问题讨论】:
-
只需在
run方法中创建下载器作为局部变量并在那里获取数据。而且这里不需要信号/槽。 -
@Lol4t0,从互联网下载是异步的,所以我需要使用插槽/信号。
-
如果它是异步的,则摆脱线程。
-
Internet类是什么? -
德米特里萨佐诺夫,我是that downloader
标签: c++ qt qt-signals