Quantcast
Channel: ahttpserver Wiki & Documentation Rss Feed
Viewing all articles
Browse latest Browse all 20

Updated Wiki: ThreadPool examples

$
0
0

ThreadPool examples

#include "aconnect/types.hpp"
#include "aconnect/thread_pool.hpp"
#include "aconnect/util.hpp"

boost::detail::atomic_count COUNTER (0l);

void threadProc (int n)
{
	usingnamespace aconnect;
	util::sleep (rand() * 200/  RAND_MAX );
	std::cout << string ("threadProc: " + boost::lexical_cast<string> (n) + "\n" );

	++COUNTER;
}

int main (int argc, char* args[]) 
{
	usingnamespace aconnect;

	ThreadPool<int> pool(threadProc, 10);
	
	constint Count = 30;
	for (int n=0; n < Count; ++n) {
		aconnect::util::sleep (20);
		
		pool.process(n);
	}
	
	aconnect::util::sleep (1000);
	pool.wait();

	if (COUNTER == Count)
		std::cout << "All work done"<< std::endl;
	else
		std::cout << "Error occured, there are unprocessed items (((."<< std::endl;
}

Usage of ThreadPool is very clear - just create ThreadPool object and call process method for each item to process. Work items have to satisfy all constraints for items in std::vector (should have public copy-constructor and so on).

Class interface

template <typename P,  typename Container = std::queue<typename P> >
	class ThreadPool
	{
	public:
		typedef boost::function1<void, const typename P&> worker_thread_proc;

		ThreadPool (worker_thread_proc worker_proc = 0, int maxWorkersCount = 50, IStoppable *controller = 0, Logger *logger = 0);
		
		void process (P item);
		void stop (bool joinAll);
		void wait ();
	}

Additional using samples see in ThreadPool UT.

Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>