Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does asynchronous actually work under the hood..?

i've been researching a lot of multithreading, callback, dispatch queue, in synchronous and asynchronous way... The more i research, the more i got confused and frustrated to the point that i feel i can't seem to understand it ever.. please someone can lead me to right direction to start.. most of information that i found so far was all about what is does and advantage stuff.. what i really wish to know is that how the function returns immediately when asynchronous with callback and on one threaded. [here]'s(http://nathansjslessons.appspot.com/lesson?id=1085) what i got this information from

The function **returns immediately** before the file is read and schedules the read to happen       
sometime in the future. Once the data is ready, the callback function is called on the    
data.

Here's an example of how you would use a regular blocking read function to get the contents of a file var readFile = function() {

var data;
data = read('file.txt');
dosomething('contect' + data);
}

Here's the same example using an asynchronous readAsync function.

var readFileAsynch = function () {
     var func = function (x) {
          // i can do something with data
         dosomthing('content'+data);
     }
     **readAsynch('file.txt',func);** 
      dosomemorestuff();
     };

from what i know is if you use another thread than main thread than i thought that's the way of doing asynchronous then if you have only one thread like javascript then how would asynchronous really work..?

And, when it comes to dispatch queues in objective c is it right to think that queue is a just array of pointer to blocks or functions and a thread is in charge of managing this queue within application..?

I'm really sorry that my question is very blur but so am i .. hope anyone who can provides some source code or implementation that i can read to understand what is really going on. I'm tired of just reading some thing like "using thread is very expensive" ... but in what way..? Or,, i don't need to know about it..?

edit: so how does readAsynch('file.txt',func); act differently than other function so that it is called as asynch..? and how come it can execute dosomemorestuff right away without waiting for readAsynch function right above unless (i think) when you invoke readAsynch, it's done by another thread..?

like image 362
denis_choe Avatar asked Apr 10 '14 14:04

denis_choe


People also ask

How asynchronous programming works under the hood?

Under the hood, it's just syntactic sugar using generators and yield statements to “pause” execution. In other words, async functions can “pull out” the value of a Promise even though it's nested inside a callback function, giving us the ability to assign it to a variable!

How setTimeout works asynchronously under the hood?

every function that is asynchronous like Ajax, setTimeout, event handlers, etc.., joins the Events table and waits for the time for execution to come for example setTimeout waits 3000 ms to run or event handler waits until Click event happens and then run.

How async await works internally?

The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete. await can only be used inside an async method.

How do asynchronous functions work in JavaScript?

Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result.


1 Answers

In quite a few asynchronous environments, you have just one thread working away on stuff. Let's call that stuff the 'foreground'. The stuff that gets worked on is scheduled, by some sort of scheduler. A queue of function pointers probably forms the guts of that scheduler. Function pointers are pretty much the same as callbacks.

When the foreground wants to do something time-consuming, e.g. query a database, or read a file, then the foreground makes the request to the underlying OS or library, and leaves a callback to be called when that time-consuming thing is done. (That processing goes on in another thread, possibly in another process, or in the kernel, which is ever so asynchronous.) The request has some sort of ID associated with it, so that when the task is done, the correct call back gets invoked with the correct results.

While that request is going on, the foreground can continue execute whatever comes next. And if a block of execution is done, the foreground will return to the scheduler. The scheduler will pick the next task from the queue. And one of those tasks will be to run some callback function, passing in the right data, for some slow operation that's just finished.

like image 152
Chrisky Avatar answered Oct 18 '22 21:10

Chrisky