1:创建线程类,继承tthread类,
class GeoSortThread : public TThread
{
private:
//--------------------------------------------------------------------
Struct_VocGeoInfo *VGInfo;
TGeoServer_GeoInfo *GeoInfo;
//--------------------------------------------------------------------
//ServerSocket控件
TServerSocket *ServerSocket;
//Xml控件
//TXMLDocument *XMLDocument;
//http控件
//THTTPRIO *HTTPRIO;
//--------------------------------------------------------------------
//临界区
//TCriticalSection *Section;
//信号量
HANDLE hMutex;
//循环队列(包括5项:缓存区,单元指针,生产索引,消费索引,缓存区数量)
Struct_VocGeoInfo pg_buff[SIZE_OF_BUFFER];
//Struct_VocGeoInfo *pg_buff;
int in;
int out;
int count;
int processCount;
int ThreadIndex;
//--------------------------------------------------------------------
//临时OVInfo 包括SIZE_OF_TASK条VGInfo
Struct_VocGeoInfo OVGInfo[SIZE_OF_TASK];
int OVGInfoCount;
void __fastcall Paint();
protected:
void __fastcall Execute();
public:
//--------------------------------------------------------------------
//生产
bool IsIdled;
void __fastcall Put(Struct_VocGeoInfo *VGInfo,int ThreadIndex);
void __fastcall Release();
void __fastcall Acquire();
__fastcall GeoSortThread(TServerSocket *ServerSocket1,int ThreadID,bool CreateSuspended);
};
这个线程中可以再调用别的类的成员和函数,但这个类要作为线程类的私有成员入上TGeoServer_GeoInfo *GeoInfo;
这样几个线程操作的类中的内存都是内存的拷贝
2:多线程中数据的处理
比较好的方式是采用循环队列的方式来处理
using namespace std;
const unsigned short SIZE_OF_BUFFER = 10; //缓冲区长度