Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

use of TMultiReadExclusiveWriteSynchronizer

john111smithjohn111smith Member Posts: 15
please consider the following code:


//////////////////////////////////////////
procedure MyThreadNum5(Data: Pointer)
var
i:integer;
begin
1 lockvar.BeginRead;
2 try
3 i:=NumOfRxData;
4 finally
5 lockvar.EndRead;
6 end;


....


end;
//////////////////////////////////////////


the above thread freezes on line #1; while in the main vcl thread of
application, the codes between beginRead/endRead and beginWrite/
endWrite are executed.
i have 7 occurrences of NumOfRxData global variable in units of
Delphi2006 project.
all of them, are protected by closed beginRead/endRead and beginWrite/
endWrite commands.


All of global variables , are defined in the main (first) unit form of
application.


//////////////////////////////////////////
Unit MainUnit;
....


var
FormMainUnit: TFormMainUnit;
LockVar:TMultiReadExclusiveWriteSynchronizer; //todo:remove
... (all global variables of application)


implementation


...


initialization
RegisterClass(TFormMainUnit);
LockVar:=TMultiReadExclusiveWriteSynchronizer.Create;


finalization
FreeAndNil (LockVar);


end.


//////////////////////////////////////////


What is problem!?

Comments

  • zibadianzibadian Member Posts: 6,349
    How did you debug this, using the debug functions or using notification messages?
    If you're using the debug functions, then the thread might be prioritized to a lower priority due to the presence of the debug functions thread. A lower priority could allow the mainthread to be allowed several writes before this thread gets his read.
    If you've used notification then you'll see that the thread will eventually (should be within a few seconds) get its read clearance.
    [b]Beware: the variable i might be invalid after the EndRead(), because after EndRead() any other thread might perform a write and thereby change the NumOfRxData value.[/b]
  • john111smithjohn111smith Member Posts: 15
    : How did you debug this, using the debug functions or using
    : notification messages?
    : If you're using the debug functions, then the thread might be
    : prioritized to a lower priority due to the presence of the debug
    : functions thread. A lower priority could allow the mainthread to be
    : allowed several writes before this thread gets his read.
    : If you've used notification then you'll see that the thread will
    : eventually (should be within a few seconds) get its read clearance.
    : [b]Beware: the variable i might be invalid after the EndRead(),
    : because after EndRead() any other thread might perform a write and
    : thereby change the NumOfRxData value.[/b]

    tanx for comments.
    I insert breakpoint on line#1.
    After press F9 key, the thread of application only run till breakpoint of line#1. if press F8 or F9 again, application freeze on it. if i remove breakpoint of line#1 and insert breakpoint for line#2, anymore application can run till that point.

    Even out of delphi2006 IDE, when I run application directly, the result of executin of the proposed thread, do not seen.
Sign In or Register to comment.