|
정말 감사드립니다. 유영인님. ^^
저도 좀 늦게 답변을 보았네요. 애궁. ㅠㅠ
많이 도움이 되었습니다. 열심히 공부할께요.
그럼 좋은 하루되시구요, 안녕히 계세요.
초보자 드림.
유영인.Chris 님이 쓰신 글 :
: TMultiReadExclusiveWriteSynchronizer 사용 예제 입니다. Global Memory를 읽고 쓰고 하는 그런 예제입니다.
:
: 몸이 좀 안좋아서 늦게 봤네요.. 아마 이름을 안써주셨으면.. 다른 분들의 더 좋은 답변을 더 빨리 들으실 수 있으셨을 텐데.. ^^
:
:
: First of all you create an instance of the class in the thread that "owns"
: the some data (memory). This data is (potentially) accessed by multiple
: threads, typically one of which is writing while the others are only
: reading. You protect every access to this variable in semantically
: appropriate BeginRead/EndRead or (if you are writing) BeginWrite/EndWrite.
:
: Let's assume that the main thread (some form) owns an integer and that
: multiple threads are interested in reading that integer:
:
: class TYourForm : public TForm
: {
: private:
: // This is the shared data for which
: // the threads will "compete"
: int FSharedData;
:
: // Property accessor methods
: int __fastcall GetSharedData(void);
: void __fastcall SetShareData(const int);
:
: public:
: __property int SharedData =
: { read = GetSharedData, write = SetSharedData };
:
: public:
: // This is the serialization object
: // I assume that FSync is instantiated and freed properly
: TMultiReadExclusiveWriteSynchronizer* FSync;
:
: void RunThreads(void);
: void WriteToSharedData(void);
:
: ....
: };
:
: Further let's have one or more threads that mindlessly read that variable:
:
: class TSomeThread : public TThread
: {
: private:
: TYourForm* FDataForm;
: public:
: __fastcall TSomeThread(TYourForm* AForm) : TThread(true)
: { FDataForm = AForm; }
:
: virtual void __fastcall Execute();
: };
:
: void __fastcall TSomeThread::Execute()
: {
: int TheReadData;
:
: int i = 100000000;
: while (i > 0)
: {
: i--;
: // Notice here that we use the *property*
: // as the data accessor; in the form's
: // declaration you should have seen the
: // accessor methods not implemented - yet;
: // spared for the Grande Finale.
: TheReadData = FDataForm->SharedData;
:
: Sleep(random(4));
: }
: }
:
:
: Now we kick of a few threads:
:
: void TYourForm::RunThreads()
: {
: TThread* AThread;
:
: for (int i = 0; i < 100; ++i)
: {
: AThread = new TSomeThread(this);
: AThread->Resume();
: }
: }
:
: void TYourForm::WriteToSharedData()
: {
: // Now some external code just kicks off this
: // method here and we write some data into
: // the shared data:
: SharedData = random(42);
:
: Sleep(random(4));
: };
:
:
: And now, after everything is set up, we can demonstrate the use of the
: TMultiReadExclusiveWriteSynchronizer class in the property accessor
: methods:
:
: int __fastcall TYourForm::GetSharedData()
: {
: int Result;
:
: // Any number of threads may read this variable;
: // note that the try/finally protection is somewhat
: // pointless in this specific case, but generally
: // there should be some protection.
: FSync->BeginRead();
: try
: {
: Result = FSharedData;
: }
: __finally
: {
: FSync->EndRead();
: }
:
: return Result;
: }
:
: void __fastcall TYourForm::SetShareData(const int Value);
: {
: // Only one thread may be writing to the variable:
: FSync->BeginWrite();
: try
: {
: FSharedData = Value;
: }
: __finally
: {
: FSync->EndWrite();
: }
: }
:
:
: BTW
:
:
: 초보자 님이 쓰신 글 :
: : 안녕하세요. C++Builder를 공부하는 사람이예요.
: :
: : 다름이 아니라, 멀티 쓰레드 쪽을 보고 있는데 제목에서 처럼
: :
: : TMultiReadExclusiveWriteSynchronizer 란 클래스를 사용하면
: :
: : 메모리의 읽기/쓰기를 제어할 수 있다고 책에 나와 있는데
: :
: : 예제가 없어서 이해하기가 참 힘듭니다. 일반 쓰레드까지는
: :
: : 이해했는데 이건 참 난해합니다. ㅠㅠ
: :
: : 유영인님께서 도와주시면 정말 감사드리겠습니다. ㅠㅠ
: :
: : 간단한 예제라도 부탁드리면 안될까요?
: :
: : 알려주시면 정말 감사드릴께요.
: :
: : 그럼 좋은 답변이 나오길 기원할께요.
: :
: : 안녕히 계세요.
|