C++ Builder TThread class and collection items manipulations

128 views Asked by At

I wonder if my source code is well designed?

In the main thread, I declare and use a collection. To fill this collection, a long processing is necessary, and I want to do this processing in a secondary thread.

So, I create a thread using the TThread class proposed by C++Builder. In the constructor of this class, I pass a pointer to my collection, and the Execute() function of the thread uses this pointer to feed the collection.

I specify that the collection is not used at all during this processing.

Is this the right way to do this?

threadconfig.h

class TThreadGetConfig : public TThread
{
private:
    pSDK FSDK;
    pControllersCollection FSubControllers;

protected:
    void __fastcall Execute();

public:
    __fastcall TThreadGetConfig(pSDK paramSDK, pControllersCollection paramSubControllers);
};

threadconfig.cpp

__fastcall TThreadGetConfig::TThreadGetConfig(pSDK paramSDK, pControllersCollection paramSubControllers)
: TThread(true)
{
    FSDK = paramSDK;
    FSubControllers = paramSubControllers;

    FreeOnTerminate = true;
}

void __fastcall TThreadGetConfig::Execute()
{
    ReturnValue = (GetSiteConfig() ? 0:1);
    Terminate();
}

bool __fastcall TThreadGetConfig::GetSiteConfig()
{
    std::vector<TSDK_ConfigData>::iterator ptr_subctrl;
   ...

    try
    {
        if (!SDK->Connected)
            throw Exception("sdk error");

        //get data from sdk calls
        ...

        for (ptr_subctrl = subcontrollers.begin(); ptr_subctrl < subcontrollers.end(); ptr_subctrl++)
        {
            pControllerItem subctrl_item = (pControllerItem)SubControllers->Add();
            subctrl_item->Data->Id = ptr_subctrl->id;
            subctrl_item->Data->Serial = ptr_subctrl->serial;
            ...
        }

        return true;
    }
    catch(...)
    {
    }

    return false;
}

main.h

class TfrmMain : public TForm
{
__published:
   ...
private:
   TThreadGetConfig *thrdGetConfig;
    pSDK FSDK;
    pControllersCollection FSubControllers;
}

main.cpp

__fastcall TfrmMain::TfrmMain(TComponent* Owner)
    : TForm(Owner)
{
    FSubControllers = new TControllersCollection(NULL);
    FSDK = new TSDK();
}

void __fastcall TfrmMain::StartThread()
{
    thrdGetConfig = new TThreadGetConfig(
            this->SDK,
            this->SubControllers);

    thrdGetConfig->Resume();
}
0

There are 0 answers