由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Kotlin 的 coroutine 不是由新建一个 thread 实现的吗?
相关主题
Typescript是不是实际上反 functional programming 的?多线程编程前景如何?
C10M除了socket以外,还牵涉其他方面的技巧c++下如何实现多线程?
coroutine or thread[合集] 怎么样提高自己的multi-thread programming能力?
真正的multi-threading是5个thread要5个cpu?那apache是真正的m有谁用TBB吗
coroutine comes to Java - Project Loomwhy do we need to map user threads to kernel threads?
multi threading 还是 multi processingProblem of Thread by Prof. Lee大伙怎么看?
thread, semaphore, 问题。一个multithread的问题(是不是有人觉的很简单?)
python下的expectrand() in multitreading
相关话题的讨论汇总
话题: thread话题: coroutine话题: threads话题: csp话题: kotlin
进入Programming版参与讨论
1 (共1页)
i**p
发帖数: 902
1
怎么有的文章说没创建新的thread.
i**p
发帖数: 902
2
怎么做到的?
A function marked with this keyword can suspend a coroutine execution, but
is not blocking the thread.
https://www.kotlindevelopment.com/deep-dive-coroutines/

【在 i**p 的大作中提到】
: 怎么有的文章说没创建新的thread.
L*********s
发帖数: 3063
3
创建新的thread还叫什么coroutine啊, 创建新的thread实现"coroutine"不是脱裤子放
屁?

【在 i**p 的大作中提到】
: 怎么有的文章说没创建新的thread.
n******t
发帖数: 4406
4
因為大家就喜歡這這種名字啊。。lol

【在 L*********s 的大作中提到】
: 创建新的thread还叫什么coroutine啊, 创建新的thread实现"coroutine"不是脱裤子放
: 屁?

i**p
发帖数: 902
5
知道就说说怎么实现的,不知道就潜着。
插科打诨请到菌斑。

【在 L*********s 的大作中提到】
: 创建新的thread还叫什么coroutine啊, 创建新的thread实现"coroutine"不是脱裤子放
: 屁?

h*i
发帖数: 3446
6
CSP (communicating sequential processes)
Put plainly, like guvest said, these are all games of goto. "CSP", "
coroutine", "suspend", "park", "yield", "await" etc, whatever word they use,
it means the similar things: save the context, pass the PC elsewhere,
switch back when ready. The detailed differences are academic only.
These are often implemented in the language, not in OS, so it can be "
lightweight", so could be faster. But it may not always be so, because you
can implement CSP with OS threads.
e.g. in Clojure, CSP is implemented as a library via macro (these macros
compile the async code into state machines, eventually to a jump table,
hence a game of goto), backed up by a thread pool. In Clojurescript, CSP is
implemented the same, but Javascript does not have threads, so you basically
have one thread to play the same game.
In non-lisp languages, the same game is played, but it is not implemented as
a library, but in the language itself.
Javascript's async-await, kotlin coroutine, go Goroutine, etc, they are all
similar.
Anyone tells you that theirs is better are either idiots or language zealots
. Ignore them.

【在 i**p 的大作中提到】
: 怎么做到的?
: A function marked with this keyword can suspend a coroutine execution, but
: is not blocking the thread.
: https://www.kotlindevelopment.com/deep-dive-coroutines/

h*i
发帖数: 3446
7
Did a bit reading, kotlin's coroutines and go's goroutines are also backed
by OS threads in the end.
So basically, they added a scheduler/dispatcher to allocate g/coroutine to
threads. And these threads are from a thread pool with a fixed size (
configurable on system startup)
Not different from how Clojure does it.
h*i
发帖数: 3446
8
Contrary to most places' warnings, my suggestion to you is to use thousands
of threads for your CSP thread pool. They recommend you to use the same
number of threads as your CPU cores, don't listen to these fools.
Because modern programming is mostly doing IO, you will be blocked on IO
everywhere. If your thread pool is not large enough, you will run out of
threads with heavy load.
Modern Linux machines handles thousands of threads just fine. Compared with
IO, threading context switch costs almost nothing at all.

【在 h*i 的大作中提到】
: Did a bit reading, kotlin's coroutines and go's goroutines are also backed
: by OS threads in the end.
: So basically, they added a scheduler/dispatcher to allocate g/coroutine to
: threads. And these threads are from a thread pool with a fixed size (
: configurable on system startup)
: Not different from how Clojure does it.

h*i
发帖数: 3446
9
thread pool
the threads are already created. they are reused.

【在 i**p 的大作中提到】
: 怎么有的文章说没创建新的thread.
L*********s
发帖数: 3063
10
https://www.kotlindevelopment.com/deep-dive-coroutines/
《What's inside?》那节,
我是不可能解释得比它好了。

【在 i**p 的大作中提到】
: 知道就说说怎么实现的,不知道就潜着。
: 插科打诨请到菌斑。

i**p
发帖数: 902
11
确实还是在用thread支持coroutine的运行,有创建新thread的选项。
Coroutine context includes a coroutine dispatcher (see CoroutineDispatcher)
that determines what thread or threads the corresponding coroutine uses for
its execution. Coroutine dispatcher can confine coroutine execution to a
specific thread, dispatch it to a thread pool, or let it run unconfined.
https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-
dispatchers.html

【在 h*i 的大作中提到】
: thread pool
: the threads are already created. they are reused.

i**p
发帖数: 902
12
费解的是这个例子,println运行在 main, 同一个 thread
launch { // context of the parent, main runBlocking coroutine
println("main runBlocking : I'm working in thread ${Thread.
currentThread().name}")
}
main runBlocking : I'm working in thread main

【在 h*i 的大作中提到】
: thread pool
: the threads are already created. they are reused.

h*i
发帖数: 3446
13
it's entirely possible that they use one thread.
CSP has nothing to do with threads. It's about a game of goto. Turn your
code into a bunch of goto, each with the right context. That's it.
Remember, in Javascript, there's only one thread, you can still do CSP.

【在 i**p 的大作中提到】
: 费解的是这个例子,println运行在 main, 同一个 thread
: launch { // context of the parent, main runBlocking coroutine
: println("main runBlocking : I'm working in thread ${Thread.
: currentThread().name}")
: }
: main runBlocking : I'm working in thread main

1 (共1页)
进入Programming版参与讨论
相关主题
thread c++ 问题coroutine comes to Java - Project Loom
a question about Nachos in Cmulti threading 还是 multi processing
用多线程怎么比单线程还慢呢?thread, semaphore, 问题。
来来,讨论一下multithread, multi-core, affinitypython下的expect
Typescript是不是实际上反 functional programming 的?多线程编程前景如何?
C10M除了socket以外,还牵涉其他方面的技巧c++下如何实现多线程?
coroutine or thread[合集] 怎么样提高自己的multi-thread programming能力?
真正的multi-threading是5个thread要5个cpu?那apache是真正的m有谁用TBB吗
相关话题的讨论汇总
话题: thread话题: coroutine话题: threads话题: csp话题: kotlin