29
Threading model in Windows Store apps Mirco Vanini Microsoft® MVP Windows Embedded

Threading model in windows store apps

Embed Size (px)

DESCRIPTION

A overview about the Threading model in windows store apps

Citation preview

Page 1: Threading model in windows store apps

Threading model in Windows Store apps

Mirco Vanini

Microsoft® MVP Windows Embedded

Page 2: Threading model in windows store apps

Why threads?

Windows threading model

Objects and threading – Marshaling

Agile objects

Thread pool threads

Using the thread pool in Windows Runtime apps

Agenda

Page 3: Threading model in windows store apps

Service connected world

Responsive apps

Touch-based experience

Many cores

Why threads?

Page 4: Threading model in windows store apps

Kernel threads

Separate processes

Brokers

Async operations

How Windows parallelizes for you

Page 5: Threading model in windows store apps

UI objects live in a UI thread

Main UI thread lasts as long as app

Most other objects can be used on any thread

We pass out work to thread pool when needed

Windows threading model

Page 6: Threading model in windows store apps

Windows objects follow natural and familiar object rules

You control lifetime

You can use them on threads you take them to

Where you create them doesn’t constrain where you use or delete them

One of our biggest changes relative to COM

Windows threading model

Page 7: Threading model in windows store apps

Windows threading modelApp

Windows UI

Object

Main UI Thread

Windows

Object

Threadpool

App Code App Code App Code

Windows

Object

Page 8: Threading model in windows store apps

Three main types of object Thread bound – works only on the thread where it was created –

most UI

Thread flexible – works on any thread, uses locking if needed to control simultaneous access

Brokered – out of process

UI runs in single threaded environment that is not reentrant (“Application STA”)

Callbacks can only enter if they are related to an outgoing call

Most non-UI runs in any thread

Windows threading model

Page 9: Threading model in windows store apps

Brokered Objects

RuntimeBroker.exe

Windows Runtime Object

IInspectable

IUnknown

App

Pro

jecti

on

Pro

xy

Page 10: Threading model in windows store apps

User interface has special threading needs Don’t want OK to be pressed during OnCancel

UI objects are deliberately and naturally serialized Don’t do long running work on UI thread Instead, offload large work to thread pool with

async object

UI threads

Page 11: Threading model in windows store apps

Objects on UI threads generally don’t need locking to protect themselves

UI threads are not reentrant UI threads can’t call each other directly Rejected at call time in Windows 8.1 Use dispatcher or async object to avoid

UI threads

Page 12: Threading model in windows store apps

No long delays allowed on UI threads Most classic waiting primitives are inappropriate

Windows will terminate unresponsive apps

Instead, use UI-safe primitives C#: await

C++: create_task

JS: promise

UI threads

Page 13: Threading model in windows store apps

App primary UI always launched on main UI thread Contract UI (e.g. Share) launched on separate UI

thread

Main UI thread used for global state Other UI threads for documents, contracts Main UI thread stays alive until app dies

Main UI thread

Page 14: Threading model in windows store apps

XAML UI objects are thread-bound Use dispatcher to move UI work back to relevant UI

thread Whole XAML tree must host on a single thread XAML events will be delivered on the UI thread Async operations started on XAML threads have

results delivered back on UI thread DirectX situation similar

XAML environment threading

Page 15: Threading model in windows store apps

Basic WinRT protocols are threading-agnostic IUnknown, IInspectable manages the object and its

interfaces

WinRT protocols allow for some objects to have special threading requirements

WinRT captures all these concepts with marshaling

Objects and threading

Page 16: Threading model in windows store apps

Marshaling allows an object to be used from another thread or process

Most WinRT objects do not require marshaling Even references to out-of-process objects (proxies)

are agile in WinRT Objects decide how they are marshaled

IMarshal, INoMarshal control marshaling

Marshaling

Page 17: Threading model in windows store apps

Agile objects are objects that can work in any thread of a process without being marshalled

Agile objects are simple to deal with Most WinRT objects are agile Out-of-process proxies are agile Agile objects do not die if their original host thread

dies

Agile objects

Page 18: Threading model in windows store apps

Apartments are a COM concept that group and control thread and object lifetimes

Apartments exist in WinRT but have been made largely irrelevant by agility to reduce pain

Three types in Windows Store apps Application single-threaded apartment (ASTA) – UI threads

Multithreaded apartment (MTA) – Thread pool

Neutral threaded apartment (NTA) – Used by WinRT to help inter-process calls

Apartments

Page 19: Threading model in windows store apps

Where long-running work is done Allocated, scaled and scheduled by the operating

system WinRT async operations automatically happen here Always initialized for WinRT usage when created Objects may be called back on any thread

Thread pool threads

Page 20: Threading model in windows store apps

Your app can use the thread pool to accomplish work asynchronously in parallel threads

The thread pool offers more control than the asynchronous programming patterns Submit work items, control their priority, and cancel work

items

Schedule work items using timers and periodic timers

Set aside resources for critical work items

Run work items in response to named events and semaphores

Using the thread pool

Page 21: Threading model in windows store apps

Submitting a work

item to the thread

pool dem

o

Page 22: Threading model in windows store apps

Submit a work item

using a timer

dem

o

Page 23: Threading model in windows store apps

Create a periodic work

item

dem

o

Page 24: Threading model in windows store apps

Respond to named

events and

semaphores dem

o

Page 25: Threading model in windows store apps

Use the thread pool to do parallel work in your app

Use work items to accomplish extended tasks without blocking the UI thread

Create work items that are short-lived and independent. Work items run asynchronously and they can be submitted to the pool in any order from the queue

Dispatch updates to the UI thread with the Windows.UI.Core.CoreDispatcher

Use ThreadPoolTimer.CreateTimer instead of the Sleep function

Use the thread pool instead of creating your own thread management system. The thread pool runs at the OS level with advanced capability and it is already optimized

Using the thread pool – Do’s

Page 26: Threading model in windows store apps

Don't create periodic timers with a period value of <1 millisecond (including 0). This will cause the work item to behave as a single-shot timer

Don't submit periodic work items that take longer to complete than the amount of time you specified in the period parameter

Don't do any extensive work in the UI dispatch handler. The handler provided to the UI core dispatcher runs in the UI thread

Don't try to send UI updates (other than toasts and notifications) from a work item running in a background task. Instead, use background task progress and completion handlers

Don't try to create work item handlers that use the async keyword

Using the thread pool – Dont's

Page 27: Threading model in windows store apps

WinRT designed to make threading natural, simple, and familiar

Don’t block the UI thread Use async for long running operations Use the thread pool to accomplish work

asynchronously in parallel threads

Recap

Page 28: Threading model in windows store apps

Q&A

Page 29: Threading model in windows store apps

Contact

feedback

10

Bloghttp://mircovanini.blogspot.com

Email [email protected]

Web www.proxsoft.it

Twitter @MircoVanini