//
//本文在c++ builder 6.0平台调试通过 #include <vcl.h> #include <fstream> #include <syncobjs.hpp>// TCriticalSection 需要这个头文件 #include <inifiles.hpp> #include <MyThread1.h> #include <MyThread2.h> #include <iostream> ? ? ? // std::cout using namespace std; volatile int Counter(0); // 定义一个全局变量,当做计数器用于累加
int _tmain(int argc, _TCHAR* argv[]) {
? ? TCriticalSection ?* Mtx=new TCriticalSection() ; ? ? const int num=1; ? ? MyThread1 * myThreadArray[num]; ? ? for (int i=0;i<num;i++) ? ? { ? ? ? ? myThreadArray[i]=new MyThread1(&Counter,Mtx,false); //写线程 ? ? ? ?// myThreadArray[i]->Resume();//恢复线程 ? ? }
? ? MyThread2 * myThread2=new MyThread2(&Counter,Mtx,false);//读线程 ? ? for (int i=0;i<num;i++) ? ? { ? ? ? myThreadArray[i]->WaitFor(); ? ? }
? ? //myThread1->WaitFor(); ? ? myThread2->WaitFor(); ? ? for (int i=0;i<num;i++) ? ? { ? ? ? ? delete myThreadArray[i]; ? ? } ? ? //delete myThread1; ? ? delete myThread2; ? ? delete Mtx;
? ? system("pause"); ? ? return 0;
}
//---------------------------------------------------------------------------
#include <vcl.h> #pragma hdrstop #include "Unit1.cpp" #include "MyThread1.h" #include <iostream> ? ? ? // std::cout #include <syncobjs.hpp>// TCriticalSection 需要这个头文件 #include <fstream> #include <inifiles.hpp> #pragma package(smart_init) //--------------------------------------------------------------------------- ? using namespace std; __fastcall MyThread1::MyThread1(volatile int * pCounter,TCriticalSection ?* Mtx,bool CreateSuspended) ? ? ? ? : pcounter(pCounter),mtx(Mtx),TThread(CreateSuspended) { } //--------------------------------------------------------------------------- void __fastcall MyThread1::Execute() { ? ? ? // while(!Terminated) ? ? ? ?{ ? ? ? ? writeCoordinate(); ? ? ? ? } } //--------------------------------------------------------------------------- void __fastcall MyThread1:: writeCoordinate() { ? //while ((*pcounter)<10)//退出循环条件,counter=10 ,根据情况改条件 ? ?// { ? ? mtx->Acquire(); ? //互斥锁上锁 ? ? TIniFile *pIni = new TIniFile("d:\\test2.ini");// ? ? pIni->WriteString("坐标","x","1"); ? ? pIni->WriteString("坐标","y","1"); ? ? pIni->WriteString("坐标","z","1"); ? ? TDateTime dt=Now(); ? ? unsigned short year,month,day; ? ? unsigned short hour,min,sec,msec; ? ? dt.DecodeDate(&year,&month,&day); ? ? dt.DecodeTime(&hour, &min,&sec, &msec); ? ? char ?time[50]; ? ? sprintf(time,"%d.%d.%d %d:%d:%d %d",year,month,day,hour,min,sec,msec); ? ? pIni->WriteString("坐标","time",time); ? ?// (*pcounter)=(*pcounter)+1; ? ?++(*pcounter); ? ? delete pIni; ? ? mtx->Release(); ?//互斥锁解锁 ? ? //Sleep(1); ? ?// }
//--------------------------------------------------------------------------- #include <vcl.h> #ifndef MyThread1H #define MyThread1H #include <syncobjs.hpp>// TCriticalSection 需要这个头文件 //--------------------------------------------------------------------------- #include <Classes.hpp> //--------------------------------------------------------------------------- class MyThread1 : public TThread { ? ? ? ? ? ? private: ? ? ?TCriticalSection ?* mtx; ? ? ?volatile int * pcounter; protected: ? ? ? ? void __fastcall Execute(); ? ? ? ? void __fastcall ?writeCoordinate(); public: ? ? ? ? __fastcall MyThread1(volatile int * pCounter,TCriticalSection ?* Mtx,bool CreateSuspended); }; //---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h> #pragma hdrstop
#include "MyThread2.h" #include <iostream> ? ? ? // std::cout #include <syncobjs.hpp>// TCriticalSection 需要这个头文件 #include <fstream> #include <inifiles.hpp> #pragma package(smart_init) //--------------------------------------------------------------------------- ? using namespace std; ? volatile int counter(0); // 定义一个全局变量,当做计数器用于累加 __fastcall MyThread2::MyThread2( volatile int * pCounter,TCriticalSection ?* Mtx,bool CreateSuspended) ? ? ? ? ?: pcounter(pCounter),mtx(Mtx),TThread(CreateSuspended),temp(0) { ? ? // } //--------------------------------------------------------------------------- void __fastcall MyThread2::Execute() { ? ? ? ? //---- Place thread code here ---- ? ? ? ?// while (!Terminated) ? ? ? ? { ? ? ? ? ? ?readCoordinate();
? ? ? ? } } //--------------------------------------------------------------------------- void __fastcall MyThread2::readCoordinate() { ? ?// while ((*pcounter)<3) //退出循环条件,counter=10,根据情况改条件 ? ? { ? ? ? mtx->Acquire(); ? //互斥锁上锁 ? ? ? TIniFile *pIni = new TIniFile("d:\\test2.ini");// ? ? ? char x[50]={0};char y[50]={0};char z[50]={0};char time[50]={0}; ? ? ?// AnsiString x,y,z,time; ? ? ? sprintf(x,"%s",pIni->ReadString("坐标","x","未知")); ? ? ? sprintf(y,"%s",pIni->ReadString("坐标","y","未知")); ? ? ? sprintf(z,"%s",pIni->ReadString("坐标","z","未知")); ? ? ? sprintf(time,"%s",pIni->ReadString("坐标","time","未知")); ? ? ?// if(*pcounter>temp) ? ? ?// { ? ? ? ? ? cout<<*pcounter<<endl; ? ? ? ? ? printf(x);printf("\n") ; ? ? ? ? ? printf(y);printf("\n") ; ? ? ? ? ? printf(z);printf("\n") ; ? ? ? ? ? printf(time);printf("\n"); ? ? ? ? ? cout<<"---"<<endl; ? ? ? ? ? temp = *pcounter; ? ? // }
? ? ? delete pIni; ? ? ? ?mtx->Release(); ?//互斥锁解锁 ? ? ?// ?Sleep(2); ? ? }
}
//---------------------------------------------------------------------------
#ifndef MyThread2H #define MyThread2H //--------------------------------------------------------------------------- #include <syncobjs.hpp>// TCriticalSection 需要这个头文件 #include <MyThread1.h> #include <Classes.hpp> //--------------------------------------------------------------------------- class MyThread2 : public TThread { ? ? ? ? ? ? private: ? ? ? TCriticalSection ?* mtx; ? ? ? ?volatile int * pcounter; ? ? ? ?int temp; protected: ? ? ? ? void __fastcall Execute(); ? ? ? ? ?void __fastcall MyThread2::readCoordinate(); public: ? ? ? ? __fastcall MyThread2( volatile int * pCounter,TCriticalSection ?* Mtx,bool CreateSuspended); }; //---------------------------------------------------------------------------
#endif
|