TCriticalSection not working

Posted on 2006-05-10
Last Modified: 2013-11-17

I made a little test program to test the working of critical sections, but it doesn't give the result I want.
I have 2 forms with each a button on.  When clicking buIncrease, the program counts from 0 to 100.  When it is counting and the couter is at 50 (eg) , i click on the button on the second form, wich calls the same function of when clicking the first button.  The result is that the label laThread1Status displays 0 to 50 and then counts from 0 to 100 (which is done by the second call), and when it reaches 100, it restarts counting from 50 to 100 (which is the first call that is resumed).
The criticalsection should make sure that the first call first has to be completed, no..?

void __fastcall TForm1::buIncreaseClick(TObject *Sender)
        int max = 100;
        csList->Acquire();   //TCriticalSection
        for(int i=0;i<max;i++)
                laThread1Status->Caption = "running: "+IntToStr(i);
        laThread1Status->Caption = "finished";

void __fastcall TForm2::Button2Click(TObject *Sender)

Question by:cleaverX

    Author Comment

    The critical section does not protect against "concurrent" access from within the same thread...
    This has more to do with events I think...
    how can i force a function to finish before this same function is called by an event...?
    LVL 14

    Expert Comment

    what is the declaration and initialisation of csList?
    LVL 12

    Accepted Solution

    >The critical section does not protect against "concurrent" access from within the same thread.
    cleaverX is correct.  Builder programs have a main VCL thread that the events run in.  Since both events are comming from the same thread, TCriticalSection will allow the second Acquire to complete before the first Release is called.

    The solution to your problem is to use classes decended from TThread.  That way your synchronization will work fine.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now