#ifndef _THREADWORK_H_ #define _THREADWORK_H_ #pragma once struct cdata { struct impl; struct impl *_impl; volatile long _refcount; cdata(); void* __stdcall operator new(size_t size); void __stdcall operator delete(void *p); }; class iexcute { public: virtual cdata* doproc(cdata*) = 0; }; class istream :virtual public iexcute { public: virtual ~istream(){} }; class ifilter :virtual public iexcute { public: virtual ~ifilter(){} }; class ireport :virtual public iexcute { public: virtual ~ireport(){} }; class threadworker { public: class impl; threadworker(); ~threadworker(); void setup( int /*0>=: 使用win线程池减少执行时间,<-100:不使用任何线程,其他值:filter,report中的函数使用新线程*/ ); void apply(istream *[] ,ifilter*[] ,ireport*[] ); // 可以多次使用这个函数加入新的处理步骤。 void submit( long flag = 0/*使用win线程时,对filter(n:flag>>8),report(n:flag>>16)向线程池提交方式进行调整, 当队列有缓存并且缓存数量不大于n时提交任务, stream(flag&0x1)线程是否不以向线程池提交任务的方式使用*/ ); void submit(cdata * = nullptr); private: impl * _impl; }; #endif /*_THREADWORK_H_*/ //cpp file #include "threadworker.h" #include <windows.h> #include <stddef.h> #include <condition_variable> #include <mutex> #include <stdlib.h> #include <stdio.h> #include <assert.h> #include <thread> #include <future> #include <mutex> #include <queue> #include <atomic> #include <iostream> #define TRACE_PIPE printf #define TRACE_PIPE #define USE_MMPOOL /*内存函数:Copyright*/ /* The author of this software is David R.Hanson. Copyright(c) 1994, 1995, 1996, 1997 by David R.Hanson.All Rights Reserved. Permission to use, copy, modify, and distribute this software for any purpose, subject to the provisions described below, without fee is hereby granted, provided that this entire notice is included in all copies of any software that is or includes a copy or modification of this software and in all copies of the supporting documentation for such software. THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED WARRANTY.IN PARTICULAR, THE AUTHOR DOES MAKE ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. David Hanson / drh@microsoft.com / http://www.research.microsoft.com/~drh/ $Id: CPYRIGHT, v 1.2 1997 / 11 / 04 22 : 31 : 40 drh Exp $ */ /* */ #define THRESHOLD 400 #define hash(p, t) (((unsigned long)(p)>>3) & (sizeof (t)/sizeof ((t)[0])-1)) #define NDESCRIPTORS 512 #define NALLOC ((4096 + sizeof (union align) - 1)/ (sizeof (union align)))*(sizeof (union align)) class newmem { union align { #ifdef MAXALIGN char pad[MAXALIGN]; #else int i; long l; long *lp; void *p; void(*fp)(void); float f; double d; long double ld; #endif }; class newarena { struct arena { arena * prev; char * avail; char * limit; }; union header { struct arena b; union align a; }; arena * _arena; arena * _freechunks; int _nfree; public: newarena() :_arena(new arena()) { _arena->prev = nullptr; _arena->limit = _arena->avail = nullptr; } ~newarena() { free(); delete _arena; } void* alloc(long nbytes) { nbytes = ((nbytes + sizeof(union align) - 1) / (sizeof(union align)))*(sizeof(union align)); while (nbytes > _arena->limit - _arena->avail) { arena* ptr; char *limit; if ((ptr = _freechunks) != NULL) { _freechunks = _freechunks->prev; _nfree--; limit = ptr->limit; } else { long m = sizeof(union header) + nbytes + 10 * 1024; ptr = reinterpret_cast<arena*> (malloc(m)); limit = (char *)ptr + m; } *ptr = *_arena; _arena->avail = (char *)((union header *)ptr + 1); _arena->limit = limit; _arena->prev = ptr; } _arena->avail += nbytes; return _arena->avail - nbytes; } void free() { while (_arena->prev) { struct arena tmp = *_arena->prev; if (_nfree < THRESHOLD) { _arena->prev->prev = _freechunks; _freechunks = _arena->prev; _nfree++; _freechunks->limit = _arena->limit; } else delete _arena->prev; *_arena = tmp; } assert(_arena->limit == NULL); assert(_arena->avail == NULL); } }_arena; struct descriptor { struct descriptor *free; struct descriptor *link; const void *ptr; long size; const char *file; int line; } *htab[2048]; struct descriptor freelist = { &freelist }; struct descriptor *avail; int nleft; std::mutex _mutex; void *resize(void *ptr, long nbytes) { struct descriptor *bp = 0; void *newptr; assert(ptr); assert(nbytes > 0); #ifdef MAXALIGN if ((((unsigned long)ptr) &~(MAXALIGN - 1)) != 0 #else if (((unsigned long)ptr) % (sizeof(union align)) != 0 #endif || (bp = find(ptr)) == NULL || bp->free) throw std::exception("Allocation Failed"); newptr = alloc(nbytes); memcpy(newptr, ptr, nbytes < bp->size ? nbytes : bp->size); free(ptr); return newptr; } void *calloc(long count, long nbytes) { void *ptr; assert(count > 0); assert(nbytes > 0); ptr = alloc(count*nbytes); memset(ptr, '