Haruyuki Mohri

毛利春幸のブログです。

Implement Parallel::For() in C++Builder 10 Seattle

New project in Console application.

This was parallel processing SQL insert using Parallel::For().

Program code.

template <class T>
struct TParallelProc : public TCppInterfacedObject<TProc__1<T> >
{
    void __fastcall Invoke(T Arg1){

        CoInitialize(NULL);
        try
        {
            std::shared_ptr<TADOQuery> ado(new TADOQuery(NULL) );
            String value = FormatDateTime(L"hh:nn:ss.zzz", Now());
            String str1 = "string" + IntToStr(Arg1);
            ado->ConnectionString = CONNECTION1;
            ado->SQL->Text  = Format("insert into t_name_list values('%s', getdate())", ARRAYOFCONST((str1)) );
            ado->ExecSQL();
            std::wcout << value.w_str() << L" " << str1.w_str() << std::endl;
        }
        catch(...)
        {}
        CoUninitialize();
    };
};

 int _tmain(int argc, _TCHAR* argv[])
{
    DelphiInterface<TProc__1<int> > pro = new TParallelProc<int>();
    TParallel::For(2,0, 10, pro);
    Sleep(500);

    return 0;
}

Execution Screen.

f:id:mojeld:20150904181146p:plain It seemed to have been 10processed in parallel.

Reference

Haruyuki Mohri Blog(Delphi) Delphi 10Seattle で並列処理(TParallel.For)で一気に1000行SQLインサート
Tutorial: Using the For Loop from the Parallel Programming Library - RAD Studio