Languages

Menu
Sites
Language
Muli-Thread For HTTP Downloading

In our app, we are using threads (not main app thread) to handle the dowloading files in achive file (put on server) as screenshot:

Assuming that the network connection is open, the code snippet for downloading should be:

//Downloader.cpp
result Downloader::RequestFileInZip(int fileIndex/*, int numFileNeedToDownload*/)
{

    //The 1st thread

    pHttpClient1 = new HttpClient();
    pHttpClient1->Construct();
    result r = pHttpClient1->SendPartialGetRequest(FileOffsetInZip[fileIndex], FileOffsetInZip[fileIndex] + FileLengthInZip[fileIndex]);

    //The 2nd thread

    pHttpClient2 = new HttpClient();
    pHttpClient2->Construct();
    result r = pHttpClient2->SendPartialGetRequest(FileOffsetInZip[fileIndex + 1], FileOffsetInZip[fileIndex + 1] + FileLengthInZip[fileIndex + 1]);
    return r;
}

/*..................................................*/

//HttpClient.cpp

result HttpClient::Construct()
{
    return Thread::Construct(THREAD_TYPE_EVENT_DRIVEN);
}

result HttpClient::SendPartialGetRequest(int startPosition, int endPosition)
{
    this->startPosition = startPosition;
    this->endPosition = endPosition;
    return Start();
}

void HttpClient::OnStop(void) {}

bool HttpClient::OnStart(void)
{
    StartDownloading(this->startPosition, this->endPosition);
    return true;
}

result HttpClient::StartDownloading(int startPosition, int endPosition)

{

   //Open HTTP Session

   //Open HTTP Transaction

   //Add HTTP Transaction Listener

   //Handle HTTP Request

   //Submit HTTP

}

void HttpClient::OnTransactionReadyToRead(Tizen::Net::Http::HttpSession& httpSession, Tizen::Net::Http::HttpTransaction& httpTransaction, int availableBodyLen)

{

   //Saving file from HTTP Response

}

 

Our problem is:

   1. The app freezes:

  • The 1st thread downloads file 1 in some bytes.
  • The 2nd thread WOULD NOT able to construct the http session.

   2. If we ONLY use one thread, the 1st for instance, all 2 files will be downloaded successfully.

 

Our questions are:

    - How to fix the above #1 issue?

    - We did search muti-thread app in Tizen as well as its sibling - Bada, but there are some samples with one main thread + another thread; Does anyone recommend us other samples pose actual multi-threads?

Edited by: Brock Boland on 17 Mar, 2014 Reason: Paragraph tags added automatically from tizen_format_fix module.

Responses

7 Replies
wil smith
I doubt your Thread construction way, you are using a deprecated method. Better use the Tizen::Base::Runtime::EventDrivenThread class for event driven threads.
Hi, Thanks for your answer. Regardless of using either Tizen::Base::Runtime::EventDrivenThread or Tizen::Base::Runtime::Thread, I got the following errors after downloading for a while: 05-07 10:44:12.670 : ERROR / Tizen::Base::Runtime ( 3895 : 4430 ) : result Tizen::Base::Runtime::_EventDispatcher::Construct(GMainContext*) (86). > [E_SYSTEM] Failed to open eventfd. 05-07 10:44:12.675 : ERROR / Tizen::Net::Http ( 3895 : 4430 ) : result Tizen::Net::Http::_HttpMultipleConnectionInfo::Construct(CURLM*, int, Tizen::Net::Http::NetHttpCookieFlag) (92). > [E_SYSTEM] GetCurrentEventDispatcher() is null. 05-07 10:44:12.675 : ERROR / Tizen::Net::Http ( 3895 : 4430 ) : result Tizen::Net::Http::_HttpSessionImpl::Construct(const Tizen::Net::NetConnection&, Tizen::Net::Http::NetHttpSessionMode, const Tizen::Base::String*, const Tizen::Base::String&, const Tizen::Net::Http::HttpHeader*, Tizen::Net::Http::NetHttpCookieFlag) (632). > [E_SYSTEM] Propagating. 05-07 10:44:12.675 : ERROR / Tizen::Net::Http ( 3895 : 4430 ) : result Tizen::Net::Http::HttpSession::Construct(const Tizen::Net::NetConnection&, Tizen::Net::Http::NetHttpSessionMode, const Tizen::Base::String*, const Tizen::Base::String&, const Tizen::Net::Http::HttpHeader*, Tizen::Net::Http::NetHttpCookieFlag) (93). > [E_SYSTEM] Propagating. Could you help?
Just note that I started only one thread, then joined it. According to the above log message, the app always freezes at the 488th turn.
wil smith
Hmm... it looks a rare case to me. If you can share a sample app for reproducing the issue it would be easier to analyze...
Here you go: http://ota.sai.gameloft.com/gmz/sai/MultiDownload.jar Note: 1. I tested on 5 real devices, and even on Emulator, I got the same problem. 2. In HttpClient.cpp file, you can see I test the multi-threads with either HTTP downloading or Unzipping Are you from the Tizen dev team? Could you add my IM for further contact: Skype: minhitkhtn MSN: minhitkhtn@hotmail.com I'm available on Skype now.
Hello everybody. Have you got it working? I seem to encounter the same problem working with EventDrivenThread. I'm working on the game which uses OpenGL. All user actions cause different sounds to be played. It was a good idea to play sounds on another thread in order not to overwhelm the main thread on which the rendering is performed. But here I got in the same problem as minhdangphamthe wrote, after 8 times of playing sounds the application just freezes. There are no exceptions or errors, even more if I tap on the button the application responds to this tap and performs some processing. I've spent plenty of time founding problems but no luck. Then I decided to make a simpler project and test it. Here's what I did: 1. I took the sample named "GlesCube" which is shipped with the Tizen SDK. It used OpenGL and that what I needed. 2. Implemented a SoundPlayer which was an extension of the EventDrivenThread. The implementation was as simple as possible. OnStart - create an instance of Tizen::Media::Player OnUserEventReceivedN - play a sound passed as a parameter 3. Integrated this implementation into the GlesCube OnTimerExpired - here the rendering is performed. On each 50th time of rendering I tried to send an event to the SoundPlayer to play a sound. And again after 8 attempts to play a sound the application freezes. I tried it on the Emulator and the device as well. Here's my code: https://www.dropbox.com/s/44cu34cxm2366rm/GlesCube.zip It seems that there is a problem with the Event system. I added a piece of code which recreates the Tizen::Base::Runtime::Timer after each 5th attempt to play a sound and then it worked without freezing. Also I thought that maybe I just didn't make cleanup properly. I created another project. There in the loop an instance of EventDrivenThread is created at the beginning, and then destroyed at the end of the loop. Every time only two threads are working. And after creating an instance of the EventDrivenThread for 491st time the application breaks. Here's the code: https://www.dropbox.com/s/nd3u4aysxlm7p0d/ThreadTest.zip It will be very appreciated if someone from Tizen development team has a look at it and explains what is going on. Is it a wrong usage of EventDrivenThread or a bug? Please let me know if something gets clear, my email: bakunmv@gmail.com. Thanks in advance.
Hi again, I managed to fix the issue by preventing recreating the thread instance many times (my case is over 488 times). In detail, I still construct the event-thread type and let it alive until it download all thousands of data files.