Goroutines 8kb and windows OS thread 1 mb

As a Windows user, I know that OS threads consume ~ 1 MB of memory because of By default, Windows allocates 1 MB of memory for each thread’s user-mode stack.How golang~ uses ~ 8 KB of memory for each goroutine, if the OS stream is much more voracious. Are there goroutinetypes of virtual threads?

+2
source share
3 answers

Goroutines are not threads, they (from spec ):

... an independent concurrent control flow or goroutine in the same address space.

An effective way defines them as:

goroutines, -, , .. . : , goroutines . , , . , , ( ) .

Goroutines . goroutines ( ) , , (, - ), .

, goroutines , runtime.GOMAXPROCS(). runtime:

GOMAXPROCS , GO . , Go; GOMAXPROCS.

, goroutines 1 .

+5

1 MiB , . ( , ~ 8 kiB).

, goroutines . , Python. goroutine - , , , ; ( ), .

:

loop forever
 take job from queue
 execute job
end loop

, execute job . , , ( - ). , . , - , , .

( :)). - - Python - , ( "tasklets" ), . , .

#, , Task s. , goroutines, - , Task (), "". , 1000 , 1000 ; Task (, -, , ..). Task - , goroutines - . :)

EDIT:

, #:

async Task<string> GetData()
{
  var html = await HttpClient.GetAsync("http://www.google.com");

  var parsedStructure = Parse(html);
  var dbData = await DataLayer.GetSomeStuffAsync(parsedStructure.ElementId);

  return dbData.First().Description;
}

GetData - ​​ , . , , ""; , , . , , await -. Task s, - , , - - "", Task .

goroutines , Go . . ( Go - ) , goroutine, , , , goroutine, "" , ; , "" , , , # await. - # Go's, .

, , Windows - "" . , ​​, , .

+2

goroutines - , . , go. .

0

Source: https://habr.com/ru/post/1690249/


All Articles