Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-02

Synchronize troubles (attach forgotten, sorry)

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

JoshyFun

Hello lazarus-list,

I'm working in a program that needs multiple threads and one will synchronize with main thread to display a log. I do not wish to continue the caller thread meanwhile the Synchronize has not been finished, so I had added a set of events to handle this. My program locks up just after start, and the offended method is in the Synchronize feature. So I start to rewrite the same idea around the multithread example code keeping only the things that I need, and to ask to you if this is a Synchronize bug/problem or a non-wanted programming technique, and I must replace the code with a different approach (if possible).

The main problem is here, in the execute method:

-----------------------------------------
procedure TMyThread.Execute;
begin
  fStatusText := 'TMyThread Starting...';
  Synchronize(@Showstatus);
  fStatusText := 'TMyThread Running...';
  while (not Terminated) and (true {any condition required}) do begin
    m_Event.WaitFor(1000); //It should be INFINITE...
    m_Event.ResetEvent;
    if NewStatus  fStatusText then begin
      m_Lock.Enter;
      fStatusText := newStatus;
      Synchronize(@Showstatus);
      m_Lock.Leave;
    end;
    m_EventDone.SetEvent;
  end;
end;
-----------------------------------------

The idea is to modify "NewStatus" from an outside procedure (CriticalSection protected) and activate m_Event to let the Execute know that it must launch the syncronize, then it will wait for "m_EventDone" to be signaled before continue.

Attached is the Multithread example mainunit.pas modified with my changes to raise the bug/problem, simply replace the example file with this one.

System: WinXP SP2
Lazarus: 0.9.24 and 0.9.25 #13510 (packages from the web) FPC: As shipped with lazarus 2.2.0 and 2.2.1
CPU: Pentium Hyperthreading (tested without hyper too).

Best regards, JoshyFun -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: multithread_synchro_problem.pas.txt Url: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20080221/f5f16cb3/attachment-0001.txt

Reply by email

Micha Nelissen
to JoshyFun
JoshyFun wrote 5 lines
Hello lazarus-list, I'm working in a program that needs multiple threads and one will synchronize with main thread to display a log. I do not wish to continue the caller thread meanwhile the Synchronize has not been

Synchronize is blocking by itself already.

multithread example code keeping only the things that I need, and to ask to you if this is a Synchronize bug/problem or a non-wanted

You've just illustrated a perfect example of a deadlock ;-).

Synchronize blocks until SetString completes, but that doesn't complete until m_EventDone is set, but that only happens when Synchronize completes, which it never does.

Micha

Reply by email

JoshyFun
to Micha Nelissen

Hello Micha,

you wrote
> I'm working in a program that needs multiple threads and one will > synchronize with main thread to display a log. I do not wish to > continue the caller thread meanwhile the Synchronize has not been

MN> Synchronize is blocking by itself already.

Yes... I had seen it also :)

> multithread example code keeping only the things that I need, and to > ask to you if this is a Synchronize bug/problem or a non-wanted

MN> You've just illustrated a perfect example of a deadlock ;-).

Yes sure :) The problem is that it can not be solved in the same way, so some code must be added (by my side) to prevent the call to the affected thread when the current thread is the same as the main thread, I'm thinking in a derived TThread class that handle the whole block inside the tipical "while not terminated do" in a method and nother function decide if the data must be handled by the new thread or by the current thread (if it is the main one), for sure I must elaborate a bit more.

MN> Synchronize blocks until SetString completes, but that doesn't complete MN> until m_EventDone is set, but that only happens when Synchronize MN> completes, which it never does.

Yes, I saw it (after 3-4 hours) after thinking how the message queue is being handled and how is locking my code. I spend near one day to find it :( Sometimes is better to take a coke and return a few hours later.

Thank you for your help.

Best regards, JoshyFun

Reply by email