Saturday 15 September 2012

multithreading - What's the difference between async methods and threads? -



multithreading - What's the difference between async methods and threads? -

can explain what's difference between async methods , creating thread in vala? , should utilize 1 or in code?

threads independently schedulable while async methods run in main thread, is, coroutines. example, if block reading socket in separate thread, application can go on running, if in async method, application block.

threads , coroutines (async methods) can solve of same problems, doing different things. in gtk+ (and many other gui systems) 1 thread can manipulate gui objects , events gui objects happen in thread (usually called event dispatch thread). if thread wants access gui items, either needs a) go through locking procedure or b) send message edt. generalises inter-thread communication: communicate between threads or utilize shared resources, there needs locking , communication.

coroutines executed in edt “just event”. consider situation have window download button. when user clicks button, edt start click handler associated button. if code effort download file, gui freeze until file finished downloading. if coroutine started, button's handler start async method open socket , told isn't ready yet. set socket , callback edt's loop (glib.mainloop). button's handler finish , edt sit down , wait event x display or socket, phone call right callback handle it. allows gui events processed in interleaved way socket events. however, 1 handler can working @ time, handlers need able finish or application unresponsive.

using coroutines becomes giant mess of callbacks, async methods hide callbacks looks straight-line code though isn't.

if task waiting, coroutines right choice. if task busy working, undoubtedly need go in thread. courotines can't exceed more 1 cpu's work, while threads can run in parallel on multiple cpus. glib's coroutines can't mixed threads: it's not sane seek have async methods operate independently in 2 threads: edt gets utilize async methods.

multithreading asynchronous vala

No comments:

Post a Comment