boost::asio::io_service io_service;
boost::asio::
io_service::work
work
(io_service)
;
io_service.
run
();
std::cout <<
"Yes, run() is returned!"
<< std::endl;
多线程中的应用
boost::asio::io_service io_service;
void workerThread() {
std::cout << "thread start" << std::endl;
io_service.run();
std::cout << "thread finish" << std::endl;
int main(){
std::shared_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(io_service));
for (int i = 1; i < 10; ++i) {
std::thread t(workerThread);
t.detach();
std::cin.get();
io_service.stop();
std::cout << "io_service stop..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "main exit" << std::endl;
dispatch和post
std::mutex mutex;
void workerThread(const std::shared_ptr<boost::asio::io_service>& io_service) {
std::lock_guard<std::mutex> lock_guard(mutex);
std::cout << "[" << std::this_thread::get_id() << "] thread start" << std::endl;
io_service->run();
std::lock_guard<std::mutex> lock_guard(mutex);
std::cout << "[" << std::this_thread::get_id() << "] thread finish" << std::endl;
void dispatch(int x) {
std::lock_guard<std::mutex> lock_guard(mutex);
std::cout << "[" << std::this_thread::get_id() << "] " << __func__ << " x = " << x << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
void post(int x) {
std::lock_guard<std::mutex> lock_guard(mutex);
std::cout << "[" << std::this_thread::get_id() << "] " << __func__ << " x = " << x << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
void run(const std::shared_ptr<boost::asio::io_service>& io_service) {
std::cout << "run executing..." << std::endl;
for (int x = 0; x < 3; ++x) {
io_service->post(std::bind(post, x));
io_service->dispatch(std::bind(dispatch, x));
std::this_thread::sleep_for(std::chrono::milliseconds(4000));
std::cout << "finish run executing..." << std::endl;
int main(){
std::shared_ptr<boost::asio::io_service> io_service(new boost::asio::io_service);
std::shared_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(*io_service));
std::lock_guard<std::mutex> lock_guard(mutex);
std::cout << "[" << std::this_thread::get_id() << "] This program will exit when all work has finished."
<< std::endl;
std::thread t(std::bind(workerThread, io_service));
io_service->dispatch(std::bind(run, io_service));
std::this_thread::sleep_for(std::chrono::seconds(6));
std::cout << "reset work..." << std::endl;
work.reset();
if (t.joinable()) {
t.join();
strand顺序化执行
std::mutex mutex;
void workerThread(const std::shared_ptr<boost::asio::io_service>& io_service) {
std::lock_guard<std::mutex> lock_guard(mutex);
std::cout << "[" << std::this_thread::get_id() << "] thread start" << std::endl;
io_service->run();
std::lock_guard<std::mutex> lock_guard(mutex);
std::cout << "[" << std::this_thread::get_id() << "] thread finish" << std::endl;
void printNum(int x) {
std::cout << "[" << std::this_thread::get_id() << "] x: " << x << std::endl;
int main(){
std::shared_ptr<boost::asio::io_service> io_service(new boost::asio::io_service);
std::shared_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(*io_service));
boost::asio::io_service::strand strand(*io_service);
std::lock_guard<std::mutex> lock_guard(mutex);
std::cout << "[" << std::this_thread::get_id() << "] This program will exit when all work has finished."
<< std::endl;
for (int x = 0; x < 10; ++x) {
std::thread t(std::bind(workerThread, io_service));
t.detach();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
strand.post(std::bind(printNum, 11));
strand.post(std::bind(printNum, 12));
strand.post(std::bind(printNum, 13));
strand.post(std::bind(printNum, 14));
strand.post(std::bind(printNum, 15));
io_service->post(std::bind(printNum, 1));
io_service->post(std::bind(printNum, 2));
io_service->post(std::bind(printNum, 3));
io_service->post(std::bind(printNum, 4));
io_service->post(std::bind(printNum, 5));
work.reset();
std::this_thread::sleep_for(std::chrono::seconds(5));
复制代码
- 7379
-
OrzR3
JavaScript
Vue.js
- 1786
-
youth君
Vue.js
Element
- 5712
-
linwu
JavaScript
Vue.js
- 6540
-
ssh_晨曦时梦见兮
JavaScript
Vue.js
- 494
-
zt_ever
JavaScript
Vue.js