Making TSynEdit lines thread-safe for background processing

77 views Asked by At

In the last few weeks I've been very busy creating a custom inhouse code editor based on the fine TSynEdit component.

There's some background processing of the editor lines neede for validating the user input and highlighting potentially wrong syntax. This processing is triggered every 500 milliseconds by a TTimer on the main form.

The validator thread iterates over every line filling a

std::map<int, String>

with a line number / error text.

TSynEdit's OnSpecialLineColor event is looking for the corresponding line in the map and if IsEmpty() is false, the line's background becomes red.

To make the SynEdit sufficiently thread-safe without the need of using Synchronize() or some WinAPI message stuff, my idea was, to use some kind of code like this:

#include <memory>

#define BOOST_THREAD_USE_LIB
namespace boost { extern "C" void tss_cleanup_implemented(void) {}; }
#include <boost/thread.hpp>


class TMySynEdit : public TSynEdit
{
private:
    boost::mutex FMutexLines;

    TStrings* GetLines()
    {
        boost::lock_guard<boost::mutex> guard(FMutexLines);
        return TSynEdit::Lines;
    }

    void SetLines(TStrings* ALines)
    {
        boost::lock_guard<boost::mutex> guard(FMutexLines);
        TSynEdit::Lines = ALines;
    }

public:
    __fastcall TMySynEdit(TComponent* AOwner)
        :   TSynEdit(AOwner)
    {}

    virtual __fastcall ~TMySynEdit()
    {}

    __property TStrings* Lines = {read=GetLines, write=SetLines};
};

It overwrites the property Lines of TSynEdit with a thread-safe(?) one.

The timer-triggered thread is a parsing function running in a boost::thread.

My question now is: Is this an adequate solution, or am I missing something here?

1

There are 1 answers

0
FlKo On

Thanks to Remy, I will probably return to the non-thread version when I'm back at work.