/usr/include/ossim/parallel/ossimJobMultiThreadQueue.h is in libossim-dev 2.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #ifndef ossimJobMultiThreadQueue_HEADER
#define ossimJobMultiThreadQueue_HEADER
#include <ossim/parallel/ossimJobThreadQueue.h>
#include <mutex>
/**
* This allocates a thread pool used to listen on a shared job queue
*
* @code
* #include <ossim/base/Thread.h>
* #include <ossim/parallel/ossimJob.h>
* #include <ossim/parallel/ossimJobMultiThreadQueue.h>
* #include <ossim/parallel/ossimJobQueue.h>
* #include <memory>
* #include <iostream>
*
* class TestJob : public ossimJob
* {
* public:
* TestJob(){}
* protected:
* virtual void run()
* {
* ossim::Thread::sleepInSeconds(2);
* }
* };
* class MyCallback : public ossimJobCallback
* {
* public:
* MyCallback(){}
* virtual void started(std::shared_ptr<ossimJob> job)
* {
* std::cout << "Started job\n";
* ossimJobCallback::started(job);
* }
* virtual void finished(std::shared_ptr<ossimJob> job)
* {
* std::cout << "Finished job\n";
* ossimJobCallback::finished(job);
* }
* };
*
* int main(int argc, char *argv[])
* {
* int nThreads = 5;
* int nJobs = 10;
* std::shared_ptr<ossimJobQueue> jobQueue = std::make_shared<ossimJobQueue>();
* std::shared_ptr<ossimJobMultiThreadQueue> jobThreadQueue = std::make_shared<ossimJobMultiThreadQueue>(jobQueue, nThreads);
* for(int i = 0; i < nJobs; ++i)
* {
* std::shared_ptr<TestJob> job = std::make_shared<TestJob>();
* job->setCallback(std::make_shared<MyCallback>());
* jobQueue->add(job);
* }
*
* while(jobThreadQueue->hasJobsToProcess())
* {
* ossim::Thread::sleepInMilliSeconds(10);
* }
*
* std::cout << "Finished and cancelling thread queue\n";
* jobThreadQueue->cancel();
* jobThreadQueue->waitForCompletion();
*
* return 0;
* }
* @endcode
*/
class OSSIM_DLL ossimJobMultiThreadQueue
{
public:
typedef std::vector<std::shared_ptr<ossimJobThreadQueue> > ThreadQueueList;
/**
* allows one to create a pool of threads with a shared job queue
*/
ossimJobMultiThreadQueue(std::shared_ptr<ossimJobQueue> q=0,
ossim_uint32 nThreads=0);
/**
* Will cancel all threads and wait for completion and clear the thread queue
* list
*/
virtual ~ossimJobMultiThreadQueue();
/**
* @return the job queue
*/
std::shared_ptr<ossimJobQueue> getJobQueue();
/**
* @return the job queue
*/
const std::shared_ptr<ossimJobQueue> getJobQueue()const;
/**
* set the job queue to all threads
*
* @param q the job queue to set
*/
void setJobQueue(std::shared_ptr<ossimJobQueue> q);
/**
* Will set the number of threads
*/
void setNumberOfThreads(ossim_uint32 nThreads);
/**
* @return the number of threads
*/
ossim_uint32 getNumberOfThreads() const;
/**
* @return the number of threads that are busy
*/
ossim_uint32 numberOfBusyThreads()const;
/**
* @return true if all threads are busy and false otherwise
*/
bool areAllThreadsBusy()const;
/**
* @return true if it has jobs that it's processing
*/
bool hasJobsToProcess()const;
/**
* Allows one to cancel all threads
*/
void cancel();
/**
* Allows on to wait for all thread completions. Usually called after
* @see cancel
*/
void waitForCompletion();
protected:
mutable std::mutex m_mutex;
std::shared_ptr<ossimJobQueue> m_jobQueue;
ThreadQueueList m_threadQueueList;
};
#endif
|