Enter the world of multithreading in bada
We all are aware of the advantages offered by multithreading. It offers us different implications under different scenarios. You can gain increased performance in your application by giving it the power of multithreading. Say for example that you wish to create an application that shows photos that are downloaded from various social sites, but you really do not wish the end user to wait while the images are being downloaded. Great thought, but how will you implement this? Take another situation and suppose you are doing intense drawing with some complicated calculation, but this calculation takes away your processing and makes drawing ultimately slow. Now what? How do you overcome this problem?
To overcome these kinds of problems, bada provides you with multithreading.
The thread is the single basic unit of flow control inside a program. It performs a sequential execution of code. Multiple threads indicate simultaneous execution of code.
Samsung bada has two types of threads:
- Event-driven threads run based on events that execute in a loop until they receive an event notification to terminate. Event-driven threads allow asynchronous calls.
- Worker threads run linearly; they only execute the main body of the thread and exit. Asynchronous calls cannot be used, since the thread does not run an event-loop.

Event Driven Threads: The code snippet below demonstrates a timer thread.
TimerThread.h
class TimerThread : public ITimerEventListener, public Thread { public: TimerThread(); ~TimerThread(); // Initializes the instance of TimerThread result Construct(); // Called before the Run() method is called. // Can decide whether it can be run or not by returning true or false. // If this method returns false, the thread is terminated immediately. bool OnStart(void); // Called after the Run() method is called. void OnStop(void); // Called when the timer expires. void OnTimerExpired(Timer& timer); public: // Timer time out for the expire of timer. static const int TIMER_TIMEOUT = 100; private: Timer* __pTimer; // Pointer to the timer };
TimerThread.cpp
result TimerThread::Construct() { result r = E_SUCCESS; r = Thread::Construct(THREAD_TYPE_EVENT_DRIVEN); return r; } void TimerThread::OnTimerExpired(Timer& timer) { if (__pTimer) { __pTimer->Start(TIMER_TIMEOUT); Osp::Ui::Controls::Frame* pFrame = Application::GetInstance()-> GetAppFrame()->GetFrame(); MainForm* pMainForm = static_cast<MainForm*>(pFrame->GetControl(L"MainForm")); // Fires an event for the MainForm, this is one way for // intercommunication between an application and thread pMainForm->SendUserEvent(MainForm::ID_TIME_EXPIRED, null); } } bool TimerThread::OnStart(void) { __pTimer = new Timer; __pTimer->Construct(*this); __pTimer->Start(TIMER_TIMEOUT); return true; } void TimerThread::OnStop(void) { if (__pTimer) { __pTimer->Cancel(); delete __pTimer; __pTimer = null; } }
The class below shows how to use the TimerThread class.
// MainForm.h class MainForm : public Osp::Ui::Controls::Form { public: MainForm(){} ~MainForm(){ __pTimerThread->Join(); delete __pTimerThread; } protected: static const int ID_TIME_EXPIRED = 100; TimerThread* __pTimerThread; public: bool Initialize(void); bool Initializing(void); void OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList* pArgs); }; // MainForm.cpp bool MainForm::Initialize() { result r = E_SUCCESS; r = Form::Construct(FORM_STYLE_NORMAL); SetName(L"MainForm"); return true; } result MainForm::OnInitializing(void) { result r = E_SUCCESS; // Create an instance of TimerThread and start the thread. __pTimerThread = new TimerThread; r = __pTimerThread->Construct(); __pTimerThread->Start(); return r; } void MainForm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList* pArgs) { switch (requestId) { case ID_TIME_EXPIRED: // Handle timer event here // This event is fired by Timer thread, this shows intercommunication //between the application and thread. break; } }
Worker Thread: The code below shows how to create a worker thread in bada:
// WorkerThread.h class WorkerThread : public Thread { public: WorkerThread(void){ __threadExitflag = false;} ~WorkerThread(void){} result Construct(); public: Object* Run(); void SetThreadExitFlag(bool threadExitFlag); private: int OperationOfThread(); private: bool __threadExitflag; int __counter; }; // WorkerThread.cpp result WorkerThread::Construct() { __threadExitflag = false; Thread::Construct(); __counter = 0; return E_SUCCESS; } Object* WorkerThread::Run(void) { while(true) { if (__threadExitflag) break; else OperationOfThread(); } return null; } int WorkerThread:: OperationOfThread(void) { // Do necessary operations here return __counter++; } void WorkerThread::SetThreadExitFlag(bool threadExitFlag) { __threadExitflag = threadExitFlag; }
The code below shows how to use the worker thread:
void UserWorkerThread() { // Create object of worker thread WorkerThread* pWorkerThread = null; pWorkerThread = new WorkerThread(); pWorkerThread->Construct(); pWorkerThread->Start(); Thread::Sleep(10000); pWorkerThread->SetThreadExitFlag(true); pWorkerThread->Join(); delete pWorkerThread; }
When multiple threads run simultaneously, they may access the same set of resources during their execution. Shared access between these resources can lead to an inconsistent state. To prevent this condition, bada provides 3 synchronization objects.
- Mutex: This is a binary semaphore. Only one thread can acquire the Mutex at a given time.
- Semaphore: The semaphore allows N threads to acquire the semaphore simultaneously. This can be used for accessing shared resources that have a limitation of their access.
- Monitor: The monitor can declare a “critical section” by an Enter/Exit block. In this block, multiple threads can be entered, but just one thread can be run at a time. Wait/Notify is just a signal for switching the thread in a critical section.
The following demonstrates how to use a ‘Mutex’, which is one of the 3 synchronization objects. For more details on how to use other two, refer to the bada documentation and examples.
The code snippet below shows how to use synchronization objects in bada. Here count is a shared resource, and hence different threads need to acquire a mutex before updating their values, otherwise we all know what havoc it can create.
Osp::Base::Runtime::Mutex* pMutex = null; pMutex = new Mutex; result r = pMutex->Create(L"ABC"); static int count = 0; if (IsFailed(r) == false) { pMutex->Acquire(); count++; pMutex->Release(); }
Hopefully the above helped to give you better insight into using multithreading in bada to power up your application with threads.



8:35 am

