Contents. Timer Thread Synchronization

Similar documents
High Performance Computing Course Notes Shared Memory Parallel Programming

Concurrent Programming using Threads


Lecture 24: Java Threads,Java synchronized statement

Programming in Parallel COMP755

Motivation and definitions Processes Threads Synchronization constructs Speedup issues

Problem Set: Processes

Supporting async use-cases for interrupt_token

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Threading and Synchronization. Fahd Albinali

Asynchronous Events on Linux

Thinking and Processing in Parallel Wrap Your Head Around WF 4.0 Concurrency. Brian Noyes IDesign Inc (

CROWDMARK. Examination Midterm. Spring 2017 CS 350. Closed Book. Page 1 of 30. University of Waterloo CS350 Midterm Examination.

User Space Multithreading. Computer Science, University of Warwick

EMBEDDED SYSTEMS PROGRAMMING More About Languages

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Synchronization SPL/2010 SPL/20 1

CSE 120 Principles of Computer Operating Systems Fall Quarter, 2002 Halloween Midterm Exam. Instructor: Geoffrey M. Voelker

Problem Set: Processes

Threads are lightweight processes responsible for multitasking within a single application.

Threads Questions Important Questions

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

LSN 13 Linux Concurrency Mechanisms

Eclipse Scout Job API. since Eclipse Scout Neon

Multi-threading in Java. Jeff HUANG

1 de :02

C++ State Template Class Library STTCL Concept. Version 2.0 / Mar 17, 2013 Author: Günther Makulik

Concurrent Programming in C++ Venkat

CISC2200 Threads Spring 2015

Introduction to Locks. Intrinsic Locks

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

COMP6771 Advanced C++ Programming

Last class: Today: Thread Background. Thread Systems

Threads, Synchronization, and Scheduling. Eric Wu

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

CS193k, Stanford Handout #10. HW2b ThreadBank

Processes. Process Concept

Multithreading and Interactive Programs

Concurrent Programming

Multithreaded Programming

CST242 Concurrency Page 1

* What are the different states for a task in an OS?

A process. the stack

3. Multithreading. Chuanhai Liu. Department of Statistics, Purdue University /29

C# Syllabus. MS.NET Framework Introduction

CS153: Midterm (Winter 19)

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Java Threads. COMP 585 Noteset #2 1

ECE 598 Advanced Operating Systems Lecture 23

CMSC 132: Object-Oriented Programming II

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Lecture 21: Concurrency in Other Environments (Part 2)

Systèmes d Exploitation Avancés

Shared-Memory and Multithread Programming

Performance Throughput Utilization of system resources

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Chapter 4: Multithreaded

Definition Multithreading Models Threading Issues Pthreads (Unix)

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Chapter 3: Processes. Operating System Concepts 8th Edition,

7. MULTITHREDED PROGRAMMING

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Chapter 6 Process Synchronization

Virtual Machine Design

CMSC 330: Organization of Programming Languages

Concurrent Programming. Alexandre David

Threads & Networking

Table of Contents. Cilk

Exam Name: SUN CERTIFIED PROGRAMMER FOR

Midterm - Winter SE 350

LINUX INTERNALS & NETWORKING Weekend Workshop

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

Processes. Johan Montelius KTH

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

Programming with Shared Memory. Nguyễn Quang Hùng

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

CS510 Operating System Foundations. Jonathan Walpole

Programming Languages

Parallel Programming Languages COMP360

By: Abhishek Khare (SVIM - INDORE M.P)

Exercise Session Week 8

Multithreaded Programming

Synchronization I. Jo, Heeseung

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

CSE 333 SECTION 9. Threads

Threads and Parallelism in Java

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Amity School of Engineering

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Software Practice 3 Today s lecture Today s Task

Agenda. Highlight issues with multi threaded programming Introduce thread synchronization primitives Introduce thread safe collections

Transcription:

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