English 中文(简体)
在Win32/MFC和POSIX进行清理之后是否做了准备?
原标题:Do threads clean-up after themselves in Win32/MFC and POSIX?
  • 时间:2009-09-28 20:39:27
  •  标签:

我正在利用C++和Boost开展多面方案。 我正在利用一个帮助者,热切地启动资源。 如果我抹掉了底线,而且所有对read线的提及都超出了范围,我是否泄露了任何资源? 或者,表面清扫本身(即它充斥了自己所需的系统资源)吗?

从我能看到的(以及我8年前的读物中回顾的)数字来看,不需要明确“底线”要求。

我希望read忙地实施,当有时间使用资源时,我将检查是否发生了错误。 粗略的法典范围可以看一看:

//Assume this won t get called frequently enough that next_resource won t get promoted
//before the thread finishes.
PromoteResource() {
   current_resource_ptr = next_resource_ptr;
   next_resource_ptr.reset(new Resource());
   callable = bind(Resource::Initialize, next_resource); //not correct syntax, but I hope it s clear
   boost::thread t(callable);
   t.start();
}

当然,我的理解是,正常的记忆处理问题依然存在(删除目标、处理不公等)。 我只需要确认,表面本身是“ak”。

Edit: 澄清问题,我希望确保,在技术上,这种秘密不泄漏:

void Run() {
   sleep(10 seconds);
}

void DoSomething(...) {
   thread t(Run);
   t.run();
} //thread detaches, will clean itself up--the thread itself isn t a  leak ?

在10秒之后,我就清理了某些事情,但我想绝对肯定。

最佳回答

read的 st子在离开时已经清理,但没有任何东西。 这意味着,它所分配的任何东西(例如,在原有的数据结构中)都将在离去时离开。

此外,任何本组织一级的物体(档案处理、袖珍等)都将被抛在旁(除非你再次使用在其左边关闭的包装物体)。

但是,经常制造/摧毁read子的方案,可能基本上可以释放它们所分配的一切,而这种方案正是保持方案者停电的唯一途径。

问题回答

如果I m不错,在Windows Xp,一个过程使用的所有资源在程序终止时将予以释放,但对于read来说,情况并非如此。

是的,资源一旦到期即自动释放。 这完全是正常和可接受的事,足以使背景变得暗淡。

在read子之后,你要么加入,要么放弃(在这种情况下,你不能再加入)。

这里引证的是,提升的热点说明在一定程度上解释了(但并非确切)。

When the boost::thread object that represents a thread of execution is destroyed the thread becomes detached. Once a thread is detached, it will continue executing until the invocation of the function or callable object supplied on construction has completed, or the program is terminated. A thread can also be detached by explicitly invoking the detach() member function on the boost::thread object. In this case, the boost::thread object ceases to represent the now-detached thread, and instead represents Not-a-Thread.

In order to wait for a thread of execution to finish, the join() or timed_join() member functions of the boost::thread object must be used. join() will block the calling thread until the thread represented by the boost::thread object has completed. If the thread of execution represented by the boost::thread object has already completed, or the boost::thread object represents Not-a-Thread, then join() returns immediately. timed_join() is similar, except that a call to timed_join() will also return if the thread being waited for does not complete when the specified time has elapsed.

在Win32,在文件编印中,一线直线的主要功能,即Threadproc,便清理了线索。 你在<代码>ThreadProc内分配的任何资源当然需要明确清理。





相关问题
热门标签