Osp::Base::Runtime
Contents Timer Thread Synchronization
Introduction Timers are provided to perform time based operations. The Runtime namespace contains classes for concurrent programming concepts like multithreading to improve the performance and optimized resource utilization. This also facilitates the thread synchronization mechanism. 3
1 Timer
Osp::Base::Runtime::Timer Timer class An object calls a specific function of the specified object when the given time duration has been elapsed Functions Construct( event_listener_object ) Start( timeout ) Cancel() ITimerEventListener Need to implement OnTimerExpired( Timer& ) 5
Example: Typical Timer Usage class Foo: public Osp::Base::Object, public Osp::Base::ITimerEventListener{... Timer timer; // Function to create timer for 10ms void StartTimer( void ) { timer.construct( *this ); timer.start( 10 ); } // The function invoked by the timer (listener fn) void OnTimerExpired( Timer& timer ) { // DoSomething timer.start( 10 ); // repeating }... }; 6
2 Thread
Thread Brief definition A thread is the basic unit of the flow of control inside a program. It is a single, sequential execution of code inside a program. Two types of thread Worker thread Executed and terminated Cannot use asynchronous calls Event-driven thread Activated by events Can use asynchronous calls (e.g. listeners) 8
Thread Programming IRunnable interface Base class of Thread A single task to run Need to implement Run This is meant only for worker threads Thread class Need to implement OnStart(), Run(), and OnStop() 9
Worker Thread Running Model SubThread Main Thread OnStart subthread.start Run OnStop 10
Event-Driven Thread Running Model Main Thread SubThread OnStart subthread.start subthread.senduserevent RunEventLoop Wait for Event Process Event subthread.stop OnStop 11
Example: Basic Threading (IRunnable) class ATask: public IRunnable, public Object {... void* Run( void ) { /* a thread job */ }... };... Thread* thread = new Thread; ATask* task = new ATask; thread->construct( *task ); thread->start(); // starting thread... thread->join(); // For safe deletion of thread call Join() delete thread; 12
Example: Worker Thread class AThread : public Thread {... // Constructor result Construct(void) { return Thread::Construct(THREAD_TYPE_WORKER); } // Test if the thread can run, and return bool OnStart(void) {... return true; } // finalize thread void OnStop(void) {... } // main thread body void* Run(void) { // Perform operations return null; } }; 13
Example: Worker Thread Caller... AThread* thread = new AThread; thread->construct(); thread->start();... thread->join(); delete thread; 14
Example: Timer Event-Driven Thread class AThread : public Thread, public ITimerEventListener {... Timer timer; // Constructing the thread as an event driven one result Construct(void) { return Thread::Construct(THREAD_TYPE_EVENT_DRIVEN); } // Registering a timer at on start bool OnStart(void) { timer.construct(*this); timer.start(10); return true; } void OnStop(void) { timer.cancel(); } // Asynchronous function void OnTimerExpired( Timer& timer ) { timer->start(10);... } }; 15
Example: Event-Driven Thread Caller... AThread* thread = new AThread; thread->construct(); thread->start();... // Send Exit event to the child thread thread->senduserevent(); thread->stop(); delete thread; 16
3 Synchronization
Synchronization Mutex Only one thread to access a shared resource Semaphore A limited number of threads to access a resource Monitor Critical section management 18
Example: Mutex on a Variable (count) int count = 0; Mutex* countmutex = null; class AThread: public Thread... {... // access count after preventing the other s access // if the other has access, it waits {... countmutex->acquire(); count++; countmutex->release();...} }; class BThread: public Thread... {... {... countmutex->acquire(); count++; countmutex->release();...} }; // In main thread countmutex = new Mutex; countmutex->create(); 19
Example: Semaphore(1/2) int count = 0; Semaphore* cntsemaphore = null; class AThread: public Thread... {... {... cntsemaphore->acquire(); count++; cntsemaphore->release();...} }; 20
Example: Semaphore(2/2) class BThread: public Thread...{ {...result r; // wait for semaphore if( ( r= cntsemaphore->acquire(10) ) == E_SUCCESS ){ count++; cntsemaphore->release(); } // the semaphore is unavailable for 10 else if( r==e_timeout ) { /* timeout!! */ }...} }; // In main thread cntsemaphore = new Semaphore; cntsemaphore->create(1); // 1 concurrent thread 21
Example: Job Ordering by Monitor Monitor* mon=...; class ThreadA :... { {...monitor->enter(); // job 1 monitor->wait(); // job 2: requires the result of job 3 monitor->exit(); }... } class ThreadB :... { {...monitor->enter(); // job 3 monitor->notify(); monitor->exit(); }... } job 1->3->2 or 3->1->2 22
Summary bada supports multi-threaded applications: Worker thread Event-driven thread Shared resources must be managed by means of Mutex, semaphore and monitor 23