⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tutorial.cpp

📁 threadpool is a cross-platform C++ thread pool library
💻 CPP
字号:
/*! \file * \brief threadpool tutorial. * * This file contains a tutorial for the threadpool library.  * * Copyright (c) 2005-2006 Philipp Henkel * * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * http://threadpool.sourceforge.net * */#include "threadpool.hpp"#include <iostream>#include <sstream>#include <boost/thread/mutex.hpp>#include <boost/bind.hpp>using namespace std;using namespace threadpool;using namespace boost;//// Helpersboost::mutex m_io_monitor;void print(string text){  boost::mutex::scoped_lock lock(m_io_monitor);  cout << text;}template<class T>string to_string(const T& value){  ostringstream ost;  ost << value;  ost.flush();  return ost.str();}//// An example task functionsvoid task_1(){  print("  task_1()\n");}void task_2(){  print("  task_2()\n");}void task_3(){  print("  task_3()\n");}void task_with_parameter(int value){  print("  task_with_parameter(" + to_string(value) + ")\n");}//// A demonstration of the thread_pool classint main (int , char * const []) {  print("\nWelcome to the threadpool tutorial!\n");  {	// Section 1    print("\n**************************************\n");    print("Section 1: Quick start\n");	smart_pool<fifo_pool> tp(5);    print("  Add tasks ...\n");    tp->schedule(&task_1);    tp->schedule(&task_2);    tp->schedule(&task_3);    tp->schedule(boost::bind(task_with_parameter, 4));    print("  Wait until all tasks are finished ...\n");    tp->join();    print("  Tasks finished!\n");		  }	  {	// Section 2    print("\n**************************************\n");    print("Section 2: Controlling threads\n");    smart_pool<lifo_pool> tp(0);    print("  Add tasks ...\n");	    tp->schedule(&task_1);    tp->schedule(&task_2);    tp->schedule(&task_3);    // tp.pool().join();  This would be a deadlock as there are no threads which process the tasks.    print("  Add some threads ...\n");	    tp->resize(10);    print("  Wait until all tasks are finished ...\n");    tp->join();    print("  Tasks finished!\n");	  }		  {	// Section 3:    print("\n**************************************\n");    print("Section 3: Prioritizing tasks\n");    smart_pool<prio_pool> tp(0);    print("  Add prioritized tasks ...\n");	    tp->schedule(prio_thread_func(1, &task_1));    tp->schedule(prio_thread_func(10,&task_2));    tp->schedule(prio_thread_func(5,&task_3));    // Tasks are ordered according to their priority: task_2, task_4, task_3, task_1    print("  Thread added\n");	    tp->resize(10);    print("  Wait until all tasks are finished ...\n");    tp->join();    print("  Tasks finished!\n");	  }		  {	// Section 4:    print("\n**************************************\n");    print("Section 4: Advanced thread pool instantiation\n");    // use threadpool::pool directly, no pool adaptor is involved    typedef pool<boost::function0<void>, fifo_scheduler<boost::function0<void> > > pool_type;    boost::shared_ptr< pool_type > tp = pool_type::create_pool(5);			    // same pool as threadpool::fifo_pool(5)    print("  Add tasks ...\n");    tp->schedule(&task_1);    tp->schedule(&task_2);    tp->schedule(&task_3);    print("  Wait until all tasks are finished ...\n");    tp->join();    print("  Tasks finished!\n");  }			  print("\n**************************************\n");  print("Tutorial finished!\n");  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -