OmniThreadLibrary forum
News: SMF - Just Installed!
 
*
Welcome, Guest. Please login or register. May 17, 2012, 06:18:43 PM


Login with username, password and session length


Pages: [1]   Go Down

Author Topic: Critical Section...am I missing something?  (Read 148 times)

style-sheets

  • Newbie
  • *
  • Posts: 25
    • View Profile
Critical Section...am I missing something?
« on: February 05, 2012, 09:57:18 PM »

Hi!

I read that the TOmniCS is basically a wrapper around TCriticalSection, I'm using it with pipelines, the code is:

Code: [Select]
// ---------------------------------------------------------------- //
procedure my_routine();
var
    AFile : TOmniValue;
begin
        if Pipeline = nil then
           Pipeline  := Parallel.Pipeline.NumTasks(10).Stage(StageProc).Run;

        AFile.CreateNamed(['FileURL', FileURL, 'FolderGUID', FolderGUID]);
        Pipeline.Input.Add(AFile);
end;
// ---------------------------------------------------------------- //
procedure StageProc(const Input, Output : IOmniBlockingCollection; const Task : IOmniTask);
begin
        Lock.Acquire;    // <-- [ Lock ] is a TOmniCS that's declared & used globally all across my app.
        try
           // Do some critical DB stuff
        finally
               Lock.Release;
        end;    // try/finally
end;
// ---------------------------------------------------------------- //

But clearly Lock doesn't happen and I'm getting exceptions because of that.

My question is: how can I change this code to make locking happen?

Thanks in advance!
Khaled.
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Critical Section...am I missing something?
« Reply #1 on: February 06, 2012, 01:48:54 AM »

You code fragment looks fine, the problem must lie somewhere else in the original code.
Logged

style-sheets

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Critical Section...am I missing something?
« Reply #2 on: February 06, 2012, 06:30:02 PM »

Man multithreading debugging is hard  :'(

Despite the fact that critical section wraps only 2 lines of code, admittedly I'm a bit lost (plus the exceptions do happen sporadically).

Anyway, with TThread I could set thread names in order to make it easier for debugging (in addition to log tracing), I wonder if it's possible to change tasks name here, ie.

Code: [Select]
procedure StageProc(const Input, Output : IOmniBlockingCollection; const Task : IOmniTask);
begin
        Task.Name := Value.AsArray['FileName'].AsString;
        ...
end;

Of course Task is a constant in this case, and so I'm even more blind when I try to debug & see what's wrong :(
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Critical Section...am I missing something?
« Reply #3 on: February 07, 2012, 01:47:50 AM »

Just call SetThreadName from the OtlCommon unit.
Logged

style-sheets

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Critical Section...am I missing something?
« Reply #4 on: February 07, 2012, 06:12:19 AM »

Thank you Primoz!
Logged
Pages: [1]   Go Up
 
 

Powered by MySQL Powered by PHP Powered by SMF 2.0.2 | SMF © 2006-2009, Simple Machines LLC

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM