Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parallel programming - I need some clarifications

Okay , let me try to put it in sentences ...

Lets consider an example, where I create an async method and call it with await keyword, As far as my knowledge tells me,

  1. The main thread will be released
  2. In a separate thread, async method will start executing
  3. Once it is executed, The pointer will resume from last position It left in main thread.

Question 1 : Will it come back to main thread or it will be a new thread ?

Question 2: Does it make any difference if the async method is CPU bound or network bound ? If yes, what ?

The important question

Question 3 : Assuming that is was a CPU bound method, What did I achieve? I mean - main thread was released, but at the same time, another thread was used from thread pool. what's the point ?

like image 827
Bilal Fazlani Avatar asked Sep 26 '13 16:09

Bilal Fazlani


People also ask

What is parallel programming explain?

Parallel programming is the process of using a set of resources to solve a problem in less time by dividing the work. Using parallel programming in C is important to increase the performance of the software.

Why parallel programming is difficult?

Parallel programs are inherently more difficult to debug than sequential programs. Because there are a number of processes running simultaneously, it is harder to get a good view on the state and progress of a parallel computation than of a sequential one.

Is parallel programming hard book?

Parallel programming is not as hard as some say, and we hope that this book makes your parallel-programming projects easier and more fun. In short, where parallel programming once focused on science, research, and grand-challenge projects, it is quickly becoming an engineering discipline.

How do you achieve parallel programming?

How Does Parallel Programming Work? Parallel programming works by assigning tasks to different nodes or cores. In High Performance Computing (HPC) systems, a node is a self-contained unit of a computer system contains memory and processors running an operating system.


2 Answers

async does not start a new thread. Neither does await. I recommend you read my async intro post and follow up with the resources at the bottom.

async is not about parallel programming; it's about asynchronous programming. If you need parallel programming, then use the Task Parallel Library (e.g., PLINQ, Parallel, or - in very complex cases - raw Tasks).

For example, you could have an async method that does I/O-bound operations. There's no need for another thread in this scenario, and none will be created.

If you do have a CPU-bound method, then you can use Task.Run to create an awaitable Task that executes that method on a thread pool thread. For example, you could do something like await Task.Run(() => Parallel...); to treat some parallel processing as an asynchronous operation.

like image 174
Stephen Cleary Avatar answered Sep 22 '22 00:09

Stephen Cleary


  1. Execution of the caller and async method will be entirely on the current thread. async methods don't create a new thread and using async/await does not actually create additional threads. Instead, thread completions/callbacks are used with a synchronization context and suspending/giving control (think Node.js style programming). However, when control is issued to or returns to the await statement, it may end up being on a different completion thread (this depends on your application and some other factors).

  2. Yes, it will run slower if it is CPU or Network bound. Thus the await will take longer.

  3. The benefit is not in terms of threads believe it or not... Asynchronous programming does not necessarily mean multiple threads. The benefit is that you can continue doing other work that doesn't require the async result, before waiting for the async result... An example is a web server HTTP listener thread pool. If you have a pool of size 20 then your limit is 20 concurrent requests... If all of these requests spend 90% of their time waiting on database work, you could async/await the database work and the time during which you await the database result callback will be freed... The thread will return to the HTTP listener thread pool and another user can access your site while the original one waits for the DB work to be done, upping your total limit.

It's really about freeing up threads that wait on externally-bound and slow operations to do other things while those operations execute... Taking advantage of built-in thread pools.

like image 38
Haney Avatar answered Sep 23 '22 00:09

Haney