view CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/mash/ThreadPool.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
line wrap: on
line source
// Copyright © 2015, Battelle National Biodefense Institute (BNBI);
// all rights reserved. Authored by: Brian Ondov, Todd Treangen,
// Sergey Koren, and Adam Phillippy
//
// See the LICENSE.txt file included with this software for license information.

#ifndef ThreadPool_h
#define ThreadPool_h

#include <pthread.h>
#include <queue>

template <class TypeInput, class TypeOutput>
class ThreadPool
{
public:
    
    ThreadPool(TypeOutput * (* functionNew)(TypeInput *), unsigned int threadCountNew);
    ~ThreadPool();
    
    bool outputAvailable() const;
    TypeOutput * popOutputWhenAvailable(); // output must be deleted by calling function
    bool running() const;
    void runWhenThreadAvailable(TypeInput * input); // thread deletes input when finished
    void runWhenThreadAvailable(TypeInput * input, TypeOutput * (* functionNew)(TypeInput *)); // thread deletes input when finished
    
private:
    
    struct OutputQueueNode
    {
        // used to preserve input order when outputting
        
        OutputQueueNode * prev;
        OutputQueueNode * next;
        
        TypeOutput * output;
        bool ready;
    };
    
    unsigned int threadCount;
    
    pthread_t * threads;
    
    static void * thread(void *);
    
    TypeOutput * (* function)(TypeInput *);
    TypeInput * inputCurrent;
    OutputQueueNode * outputQueueNodeCurrent;
    
    pthread_mutex_t * mutexInput;
    pthread_mutex_t * mutexOutput;
    
    pthread_cond_t * condInput;
    pthread_cond_t * condOutput;
    
    OutputQueueNode * outputQueueHead;
    OutputQueueNode * outputQueueTail;
    
    bool finished;
    friend void * thread(void *);
};


#include "ThreadPool.hxx"

#endif