Unmanaged ThreadPool(c++)
And what about unmanaged thread pool
For threading I will use pthreads lib(to make compatible code with all platforms that support POSIX standarts).
First of all template synchronized queue( %) ):
#pragma once
#include<queue>
#include"./pthreads/include/semaphore.h"
#pragma comment(lib, "./pthreads/lib/pthreadvc2.lib")
template <class T>
class BlockedQueue
{
private:
sem_t _semaphore;
std::queue<T> _queue;
public:
T& Dequeue();
void Enqueue(T&);
BlockedQueue(void);
~BlockedQueue(void);
};
template <class T>
BlockedQueue<T>::BlockedQueue(void)
{
sem_init(&_semaphore,0,-1);
}
template <class T>
BlockedQueue<T>::~BlockedQueue(void)
{
sem_destroy(&_semaphore);
}
template <class T>
void BlockedQueue<T>::Enqueue(T& value){
_queue.push(value);
sem_post(&_semaphore);
}
template <class T>
T& BlockedQueue<T>::Dequeue(){
sem_wait(&_semaphore);
T& value = _queue.front();
_queue.pop();
return value;
}
Same as C# implementation.
And a simple ThreadPool :
#pragma once
#include "BlockedQueue.h"
#include "pthread.h"
#include <vector>
typedef void (*Action)();
class ThreadsPool
{
private:
std::vector<pthread_t> _threads;
BlockedQueue<Action> _queue;
friend void* ThreadFunc(void*);
void TaskExecutor();
void StopAllThreads();
public:
void QueueWorkItem(Action);
ThreadsPool(int threadCount);
~ThreadsPool(void);
};
#include "ThreadsPool.h"
#include "pthread.h"
#include <stdexcept>
void* ThreadFunc(void * state)
{
try
{
ThreadsPool *poolRef = dynamic_cast<ThreadsPool* >((ThreadsPool*)state);
if(!poolRef)
return NULL;
poolRef->TaskExecutor();
return NULL;
}
catch(...){
pthread_exit(NULL);
throw;
}
pthread_exit(NULL);
return NULL;
}
ThreadsPool::ThreadsPool(int threadsCount)
{
if(threadsCount<1)
throw std::out_of_range("threadsCount");
for (int i = 0;i<threadsCount;i++)
{
pthread_t thread;
int result= pthread_create(&thread,NULL,ThreadFunc,this);
if(result)
{
StopAllThreads();
throw std::runtime_error("error creating thread");
}
_threads.push_back(thread);
}
}
void ThreadsPool::StopAllThreads()
{
for (std::vector<pthread_t>::const_iterator i=_threads.begin();i<_threads.end();++i)
{
pthread_cancel(*i);
}
}
ThreadsPool::~ThreadsPool(void)
{
}
void ThreadsPool::QueueWorkItem(Action action)
{
_queue.Enqueue(action);
}
void ThreadsPool::TaskExecutor()
{
while(1)
try
{
_queue.Dequeue()();
}
catch (...)
{
}
}
All simple but:friend void* ThreadFunc(void*); break all OOP
And user code for this library(I made smale dll with ThreadPool and 1 function:
extern “C” {
MYDLL_API void QueueWorkItem(Action action);
}
):
For c++:
// ThreadPoolTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "../ThreadPool/common.h"
#include <iostream>
#include <windows.h>
#pragma comment(lib,"../lib/threadpool.lib")
void f(){
while(1){
std::cout<<"1111"<<std::endl;
Sleep(1000);
}
}
void f1(){
while(1)
{
std::cout<<"2222"<<std::endl;
Sleep(1500);
}
}
void f2(){
while(1)
{
std::cout<<"3333"<<std::endl;
Sleep(2500);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
QueueWorkItem(f);
QueueWorkItem(f1);
QueueWorkItem(f2);
int i;
std::cin>>i;
return 0;
}
And C# code:
Prototype for export function from dll:
[DllImport(@"ThreadPool.dll")]
extern static void QueueWorkItem(Action action);
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace threadLibTestCS
{
class Program
{
static void f()
{
while (true)
{
Console.WriteLine("1111");
Thread.Sleep(1000);
}
}
static void f1()
{
while (true)
{
Console.WriteLine("2222");
Thread.Sleep(1500);
}
}
static void f2()
{
while (true)
{
Console.WriteLine("3333");
Thread.Sleep(2500);
}
}
[DllImport(@"ThreadPool.dll")]
extern static void QueueWorkItem(Action action);
static void Main(string[] args)
{
QueueWorkItem(f);
QueueWorkItem(f1);
QueueWorkItem(f2);
Console.ReadLine();
}
}
}
Advertisement
No trackbacks yet.