from https://gist.github.com/1130407
#! /Usr/bin/Python #-*-coding: UTF-8-*-"A simple thread pool. @ Author: Junaid p v @ license: gplv3 "from threading import thread, rlock, lockfrom time import sleepfrom functools import wrapsdef synchronous (tlockname ): "a decorator to place an instance based lock around a method from: http://code.activestate.com/recipes/577105-synchronization-decorator-for-class-methods/" def _ synched (func ): @ Wraps (func) def _ synchronizer (self, * ARGs, ** kwargs): tlock = self. _ getattribute _ (tlockname) tlock. acquire () Try: Return func (self, * ARGs, ** kwargs) Finally: tlock. release () return _ synchronizer return _ synchedclass threadpoolthread (thread): def _ init _ (self, pool): thread. _ init _ (Self) self. _ pool = pool self. start () def run (Self): Try: While true: task = self. _ pool. pop_task () If task = N One: break if task [1]! = None and task [2]! = None: task [0] (* task [1], ** task [2]) Elif task [1]! = None: task [0] (* task [1]) else: task [0] () Finally: # always inform about thread finish self. _ pool. thread_finished () Class threadpool (object): def _ init _ (self, thread_count): Self. _ tasks = [] self. task_lock = Lock () self. _ thread_count = thread_count self. _ threads = [] self. threads_lock = Lock () self. _ finished_threads_count = 0 self. finished_threads_count_lock = Lock () @ synchronous ('Task _ lock') def add_task (self, callable _, argS = none, kwds = none): Self. _ tasks. append (callable _, argS, kwds) @ synchronous ('Task _ lock') def pop_task (Self): If Len (self. _ tasks)> 0: return self. _ tasks. pop (0) else: Return none def start_workers (Self): Self. _ finished_threads_count = 0 self. _ threads = [] For I in range (self. _ thread_count): worker = threadpoolthread (Self) self. _ threads. append (worker) def wait (Self): "Wait for every worker threads to finish" While true: finished_threads_count = self. get_finished_threads_count () If finished_threads_count = self. _ thread_count: break sleep (1) @ synchronous ('finished _ threads_count_lock ') def thread_finished (Self): Self. _ finished_threads_count + = 1 @ synchronous ('finished _ threads_count_lock ') def get_finished_threads_count (Self): return self. _ finished_threads_count "example" If _ name _ = '_ main _': from threading import current_thread class sampletask: def _ init _ (self, name): Self. name = Name def call_me (self, count): Print "thread:", current_thread (). getname () For I in range (count): Print self. name, ': counting', I sleep (3) pool = threadpool (2) A = sampletask ("A") B = sampletask ("B ") C = sampletask ("C") d = sampletask ("D") E = sampletask ("e") pool. add_task (. call_me, (5,) pool. add_task (B. call_me, (7,) pool. add_task (C. call_me, (6,) pool. add_task (D. call_me, (4,) pool. add_task (E. call_me, (5,) pool. start_workers () pool. wait () print "Sleeping 5 seconds before next run... "Sleep (5) F = sampletask (" F ") G = sampletask (" G ") H = sampletask (" H ") pool. add_task (F. call_me, (5,) pool. add_task (G. call_me, (4,) pool. add_task (H. call_me, (3,) pool. start_workers () pool. wait ()
Understand how the thread pool is implemented and then rewrite it to C.