Skip to content

析构函数里面一个点 #40

@Staok

Description

@Staok
	inline ~threadpool()
	{
		_run=false;
		_task_cv.notify_all(); // 唤醒所有线程执行
		for (thread& thread : _pool) {
			//thread.detach(); // 让线程“自生自灭”
			if (thread.joinable())
				thread.join(); // 等待任务结束, 前提:线程一定会执行完
		}
	}

_run=false; 这句话需要 条件变量的锁包着,参考原作者的代码:
https://github.com/progschj/ThreadPool/blob/master/ThreadPool.h#L90

另外,// thread.detach(); 这句话虽然注释了,但是可以去掉,以防其他人解掉注释,给线程池添加了一些死循环的函数,这样线程池里面的线程永远不能释放了,造成长期运行的资源浪费、资源挤占。原作者这里只用 join 是有原因的。

另外,类里面的变量不必加下划线前缀,规范使用是用 m 前缀即可,比如 _run 改为 mRun。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions