PrevUpHomeNext

Nested asio::thread_pool std::bind: Bind its lifetime to an object


> Start
> Expected but not worked code
> Coroutines and asio::co_spawn are not the problem
> Solution: std::shared_ptr
> Back boost::asio::co_spawn and coroutines
> Back: Home

When I tried to bind boost::asio::thread_pool by-rvalue or passing-by-value object lifetime to a lifetime of customized class object and then pass the customized object to std::bind, I saw too many lines of errors, very headache. But finally, I found the solution. That's std::shared_ptr, and std::unique_ptr should work too.

Expected but not worked code

The rvalue or passing-by-value object to std::bind does not work, but I do not want to use a named object, which requires an additional standalone named object.

The original code, expected, but not worked.

#include <boost/asio.hpp>
#include <iostream>
#include <functional>

namespace wk
{
	class work
	{
	private:
		boost::asio::thread_pool __pool;
	public:
		work():
			__pool{17u}
		{
		}
	public:
		boost::asio::awaitable<void> run()
		{
			co_await this->join();
			co_return;
		}
	public:
		boost::asio::awaitable<void> join()
		{
			__pool.join();
			std::cout << "finished" << std::endl;
			co_return;
		}
	};
}

int main()
{
	boost::asio::thread_pool pool{11u};
	boost::asio::co_spawn(
		pool.get_executor(),
		std::bind(
			&wk::work::run,
			wk::work{}	// It does not work here.
		),
		[] (std::exception_ptr ptr)
		{
		}
	);
	pool.join();
}

Coroutines and asio::co_spawn are not the problem

I am pretty sure, coroutines and boost::asio::co_spawn are not the problem.

namespace wk
{
	class work
	{
	private:
		boost::asio::thread_pool __pool;
	public:
		work():
			__pool{17u}
		{
		}
	public:
		void run()
		{
			this->join();
		}
		void join()
		{
			__pool.join();
		}
	};
}

int main()
{
	boost::asio::thread_pool pool{11u};
	wk::work wk1{};
	auto fn1 = std::bind(&wk::work::run, wk1);	// not work
	auto fn2 = std::bind(&wk::work::run, wk::work{});	// not work
	auto fn3 = std::bind(&wk::work::run, &wk1);	// work
	pool.join();
}

The boost::asio::thread_pool does not allow object-copy and rvalue-object-passing.

Solution: std::shared_ptr

To make it work: bind boost::asio::thread_pool object to a customized object, and pass the object to std::bind, the customized object is unamed,

std::shared_ptr is the solution, and std::unique_ptr should work too.

std::shared_ptr creates an unnamed object and passes the address of the object to std::bind.

#include <boost/asio.hpp>
#include <iostream>
#include <functional>

namespace wk
{
	class work
	{
	private:
		boost::asio::thread_pool __pool;
	public:
		work():
			__pool{17u}
		{
		}
	public:
		void run()
		{
			this->join();
		}
		void join()
		{
			std::cout << "Finished." << std::endl;
			__pool.join();
		}
	};
}

int main()
{
	boost::asio::thread_pool pool{11u};
	// It works.
	auto fn = std::bind(&wk::work::run, std::make_shared<wk::work>());
	fn();
	pool.join();
}

Back boost::asio::co_spawn and coroutines

Back to boost::asio::co_spawn and coroutines, it works.

#include <boost/asio.hpp>
#include <iostream>
#include <functional>

namespace wk
{
	class work
	{
	private:
		boost::asio::thread_pool __pool;
	public:
		work():
			__pool{17u}
		{
		}
	public:
		boost::asio::awaitable<void> run()
		{
			co_await this->join();
			co_return;
		}
	private:
		boost::asio::awaitable<void> join()
		{
			__pool.join();
			std::cout << "Fnished 3." << std::endl;
			co_return;
		}
	};
}

int main()
{
	boost::asio::thread_pool pool{11u};
	boost::asio::co_spawn(
		pool.get_executor(),
		std::bind(
			&wk::work::run,
			std::make_shared<wk::work>()
		),
		[] (std::exception_ptr)
		{
		}
	);
	pool.join();
}

//////////////////////////////////////////////////////////////////////

Home

//////////////////////////////////////////////////////////////////////

Wed Apr 8 10:16:43 AM UTC 2026

//////////////////////////////////////////////////////////////////////

Helpful

Jfty

Wtgo

Role

+

Github:
https://github.com/cppfx/cpphtgt

+

Powered by:
B2 Build | boost quickbook

+

Donate

+

@cppfx.xyz


















PrevUpHomeNext