Git Product home page Git Product logo

simple-thread-pool's Introduction

Simple Thread Pool

Realize a simple thread pool by C++

Design

The thread pool class has four methods.

  • add_task(): This method is used to add tasks to thread pool. The arguments will be the function pointer of the task and the arguments of the task function.
void add_task(function_pointer, arguments){
	lock(mutex); // We have to lock the mutex first, since the thread pool is visiting by mutiple threads
	add the task; // The task will be implemented as a struct. The list of tasks will be implemented as a linked list.
	signal(cond); // Signal the condition variable to wake up the threads in thead pool.
	unlock(mutex);
}
  • initial(): This method is used to generate theads in thread pool.
void initial(){
	for(int i=0; i<max_thread_num; i++){
		thread(Thread_Pool::run);
	}
}
  • destroy(): This method is used to destroy thread pool and threads in it.
void destroy(){
	lock(mutex);
	set quit mark; // The quit mark would be check by very thead so they that they should be terminated.
	if(idle_thread){
		broadcast(cond); // Wake up all the threads.
	}
	if(runing_thread){
		wait(cond); //Wait for all threads to finish.
	}
	unlock(mutex);
	destroy(cond);
	destroy(mutex);
}
  • run(): This is the thread function.
void run(){
	while(true){
		lock(mutex);
		
		runing_thread++;
		idle_thread++;
		
		if(no task && !quit){
			wait(cond);
		}
		
		idle_thread--;
		
		if(!quit){
			unlock(mutex);
			get and do the task;
			lock(mutex);
		}
		
		runing_thread--;
		
		if(quit){
			if(!runing_thread){
				signal(cond);
			}
			unlock(mutex);
			break;
		}
		
		unlock(mutex);
	}
}

Usage

  • Use initial() method to initialize the pool.
Thread_Pool::initial(3);
  • Use add_task() method to add tasks to the pool.
void task(void *arg){
	....
	return;
}

int main(){
	.....
	int *arg = new int(10);
	Thread_Pool::add_task(task, arg);
	.....
}
  • Use destroy() method to destroy the pool.
Thread_Pool::destroy();
  • Attention! When the destroy() function is called, the pool will wait for the threads which are running to stop. However, the tasks which are still in the queue will be destroied instead of handled by threads. Or, you can call by destroy(true), then the pool will force destroy all threads and tasks without waiting.

simple-thread-pool's People

Contributors

tomatolike avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.