more about asynchronous programming Model(APM)

T

Tony Johansson

Hi!

In the book I read it says
"Asyncronous programming is simply allowing some portion of code to be
executed on separate threads"

If I check the the Rendezvous model wait-until-done and polling they don't
use any separate thread that I can see.
The last model which is the callback do use a separate thread.

So perhaps is the book is meaning the callback model when the book says
Asyncronous programming is simply allowing some prtion of code to be
executed on separate threads


//Tony
 
W

Willem van Rumpt

Tony said:
Hi!

In the book I read it says
"Asyncronous programming is simply allowing some portion of code to be
executed on separate threads"

If I check the the Rendezvous model wait-until-done and polling they don't
use any separate thread that I can see.
The last model which is the callback do use a separate thread.

So perhaps is the book is meaning the callback model when the book says
Asyncronous programming is simply allowing some prtion of code to be
executed on separate threads


//Tony

Both use a separate thread *, only in the wait-until-done scenario, you
block the main thread until the operation is done, and in the
rendez-vous model, you don't block.

* Or to be more precise: /can use/. There are some heuristics for
specific asynchronous operations that check if it would be faster to
complete the operation synchronously.
 
T

Tony Johansson

Willem van Rumpt said:
Both use a separate thread *, only in the wait-until-done scenario, you
block the main thread until the operation is done, and in the rendez-vous
model, you don't block.

* Or to be more precise: /can use/. There are some heuristics for specific
asynchronous operations that check if it would be faster to complete the
operation synchronously.


No the main thread is not blocked as you say.
If I run this program which is using the default main thread I can execute
statement in between the BeginRead and
the EndRead so that means that the main thread is not blocked.
Perhaps I missunderstod you but the main thread is not blocked in this
simple example.

static void Main()
{
byte[] buffer = new byte[1000];
string filename = "test.txt";
FileStream fstrm = new FileStream(filename, FileMode.Open,
FileAccess.Read,
FileShare.Read, 1024, FileOptions.Asynchronous);
//
IAsyncResult result = fstrm.BeginRead(buffer, 0, buffer.Length, null,
null);
int numBytes = fstrm.EndRead(result);
fstrm.Close();
}
..
//Tony
 
W

Willem van Rumpt

Tony said:
No the main thread is not blocked as you say.
If I run this program which is using the default main thread I can execute
statement in between the BeginRead and
the EndRead so that means that the main thread is not blocked.
Perhaps I missunderstod you but the main thread is not blocked in this
simple example.

static void Main()
{
byte[] buffer = new byte[1000];
string filename = "test.txt";
FileStream fstrm = new FileStream(filename, FileMode.Open,
FileAccess.Read,
FileShare.Read, 1024, FileOptions.Asynchronous);
//
IAsyncResult result = fstrm.BeginRead(buffer, 0, buffer.Length, null,
null);
int numBytes = fstrm.EndRead(result);
fstrm.Close();
}
.
//Tony

Assuming the operation hasn't completed when you call EndRead, the main
thread *will* block on that statement until operation completion. If it
doesn't block, it simply means the operation has completed in the
meantime (or was actually executed synchronously).

Of course, simply calling BeginRead doesn't block, that's the whole
point of asynch operations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top