Haruyuki Mohri

毛利春幸のブログです。

tried the "std::thread". using a "TRESTClient" of "C++Builder"

I used the "std::thread" in C++Builder.

This version is "XE8".

Previous How to use "async" and "future<>" in XE8(Win64) - (Delphi, C++Builder)Haruyuki Mohri

I use them the "TTimer *" to watch the end of a "std::thread".
"TTimer *" is the main thread.

"std::thread" in the function inside, use a lambda.
I used a lambda, but there is no need to use a lambda.

I used the "std::mutex" for the variable does not clash

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AniIndicator1->Enabled = true;
    AniIndicator1->Visible = true;
    Timer1->Enabled = true;
    String url = Edit1->Text;
    String ua = L"mohri_test_";
    a1 = std::thread([url,ua, this]
    {
        Sleep(500);//this operation I wanted to check used "Sleep()".
        std::shared_ptr<TRESTClient> rc(new TRESTClient(this));
        std::shared_ptr<TRESTRequest> rr(new TRESTRequest(this));
        std::shared_ptr<TRESTResponse> res(new TRESTResponse(this));
        rc->BaseURL = url;
        rc->UserAgent = ua;
        rr->Client = rc.get();
        rr->Response = res.get();
        //It will lock in "std::mutex".                     
        lk.lock();
        try{
            rr->Execute();
            s = rr->Response->Content;
        }
        catch(EIdHTTPProtocolException &e){s = e.Message;}
        lk.unlock();
    });

}
//---------------------------------------------------------------------------


void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    Timer1->Enabled = false;
    //wait for the termination of "std::thread"
    a1.join();
    AniIndicator1->Enabled = false;
    AniIndicator1->Visible = false;
    lk.lock();
    Memo1->Lines->Text = s;
    lk.unlock();
}

run in this manner

f:id:mojeld:20150814145836p:plain

Of course and can be also run "Android/iOS".