- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Lua之协同程序coroutine代码实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
do
--create coroutine table
--coroutine state: suspended, running, dead, normal
--when create the coroutine, the status is suspended, After calling it, the status is dead
--get the coroutine status by the way coroutine.status
local coA = 0;
local coB = 0;
function createCoroutineA()
coA = coroutine.create(
function()
--
for
i = 0, 10
do
print(
"coA: "
, 0);
print(
"coB status: "
, coroutine.status(coB)); --normal status
print(
"coA status: "
, coroutine.status(coA));
print(
"coA coroutine next status"
);
--coroutine.yield();--the current coroutine is suspended
--end
end
);
print(
"From coA to resume coB"
);
end
function createCoroutineB()
coB = coroutine.create(
function()
--
for
i = 0, 10
do
print(
"coB: "
, 0);
print(
"coA status: "
, coroutine.status(coA));
coroutine.resume(coA); --when resume coA, the coB will suspended, calling coB ,the coA status is
--suspended and dead,
this
time
will
continue
to execute the next code
print(
"coB status: "
, coroutine.status(coB));
print(
"coB coroutine next status"
);
--coroutine.yield();
--end
end
);
print(
"From coB to resume coA"
);
end
--display the coA and coB status
createCoroutineA();
print(coroutine.status(coA));
createCoroutineB();
print(coroutine.status(coB));
coroutine.resume(coB);
print(coroutine.resume(coB)); --
if
the coroutine is dead ,the resume will resume
false
, and can't resume the dead coroutine
--print(
"coA status: "
, coroutine.status(coA));
--print(
"coB status: "
, coroutine.status(coB));
end
|
注: resume得到返回值, 如果有对应的yield在wait resume,那么yield的参数作为resum的返回值,第一个返回值表示coroutine没有错误,后面的返回值个数及其值视yeild参数而定。 如果没有yield在wait,那么返回值是对应函数的返回值,:true,* * * 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
do
--create coroutine table
--coroutine state: suspended, running, dead, normal
--when create the coroutine, the status is suspended, After calling it, the status is dead
--get the coroutine status by the way coroutine.status
local coA = 0;
local coB = 0;
function createCoroutineA()
coA = coroutine.create(
function(paramA, paramB)
--
for
i = 0, 10
do
print(
"coA: "
, 0);
coroutine.yield(paramA, paramB);--the current coroutine is suspended
--end
return
100, 200;
end
);
print(
"From coA to resume coB"
);
end
function createCoroutineB()
coB = coroutine.create(
function()
--
for
i = 0, 10
do
print(
"coB: "
, 0);
print(
"coA status: "
, coroutine.status(coA));
coroutine.resume(coA); --when resume coA, the coB will suspended, calling coB ,the coA status is
--suspended and dead,
this
time
will
continue
to execute the next code
print(
"coB status: "
, coroutine.status(coB));
print(
"coB coroutine next status"
);
--coroutine.yield();
--end
end
);
print(
"From coB to resume coA"
);
end
createCoroutineA();
--
if
not yield is waiting ,the
return
values that the main function
return
as the results of the resume
--or the
return
as the yield params
print( coroutine.resume(coA, 10, 20));--OutPut:
true
, 10, 20
end
|
。
最后此篇关于Lua之协同程序coroutine代码实例的文章就讲到这里了,如果你想了解更多关于Lua之协同程序coroutine代码实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
import kotlinx.coroutines.* fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
我有一个长计算的外部库。这个库就是一切,但在过早停止方面是合作的。它被包装并在一个协程中启动。 我想从调用方终止进程。协程取消是合作的,所以它不起作用。有没有办法突然终止协程? 最佳答案 非挂起的协程
我是 Kotlin/Coroutines 的新手,我注意到两种不同的使用方式 CoroutineScope . 选项 1 如下,在任何函数中: CoroutineScope(Dispatchers.D
我对协同例程比较陌生,我正在尝试获取 Launch 协同例程将完成的行为: launch(UI) { val v1 = someDeferredType val v2 = v1.a
文档说: @asyncio.coroutine Decorator to mark generator-based coroutines. This enables the generator use
我在 kotest 中使用了 Funspec 测试风格,我得到了一个由框架自动注入(inject)的 coroutineScope,如下所示。 class MyTestSpec: FunSpec()
我想尝试从一个courtine 中暂停主线程。几个问题,但首先让我向您展示我正在使用的代码块作为测试: class MainActivity : Activity(), CoroutineScope
我不确定我是否理解 purescript-coroutines 中有一个 connect 和一个 pullFrom 函数的原因。 ,以及何时使用它们。看看这些类型,它们似乎已经习惯了改变“通信方向”(
我在不同的类(class_A、class_B)中有 2 个协程(co_A、co_B)由调度程序定期调用。在执行期间的某个时候,co_B 需要一个 co_A 在其运行期间正在计算的结果。 我想做的是这样
在 document , 如果方法也用@gen.coroutine 修饰,则不需要@web.asynchronous。像这样 @web.asynchronous @gen.coroutine def
目前在 pzmq 中有多种方法来实现文档中提到的异步 IO 循环。 http://pyzmq.readthedocs.io/en/latest/api/index.html 从纯性能的角度来看,文档中
看来我误解了当有许多套接字被打开时 Windows 如何在 TIME_WAIT 中处理套接字。如果在 TIME_WAIT 中挂出太多,它只会出错。 Linux 清理旧连接并成功(至少在我的盒子上,不确
我正在尝试对我使用 Kotlin 协程制作的 Presenter 进行单元测试,这也是我第一次使用 Mockito 每当我尝试运行单元测试时,它在协程中第一次尝试对我的 View 执行任何操作时都会收
我注意到 Boost.Coroutine2 for C++ 中删除了对对称协程的支持,所以我想了解 - 两者之间有什么区别? 最佳答案 Ana Lúcia de Moura 和 Roberto Ier
在Linux System Programming, 2nd Edition书中,协程与纤维之间的差异解释如下: 协程和纤维提供了比线程更轻的执行单元(前者是编程语言结构时的名称,后者是系统结构时的名
此示例已在另一个问题中用于说明如何使用协程来编写视频游戏中的过场动画: bob.walkto(jane) bob.lookat(jane) bob.say("How are you?") wait(2
我正在寻找实现 FSM 的方法,这导致我第一次遇到协程。 我看到了几个示例(here、here 和 here),它们暗示状态机可以由单个协程实现。然而,我注意到所有这些机器的共同点是,不包括循环,它们
我试图从ViewModel异步获取所有可启动的已安装应用程序。 这是我的ViewModel类的样子: class AppInstalledViewModel(application: Applicat
如何在kotlin中像多线程一样进行多次启动 我想让 first second 永远同时工作!! 喜欢这段代码... runBlocking { // first launch{
最近我正在按照这个 CodeLabs tutorial 学习 kotlin-coroutine 。经过一些实践后,我想知道我是否可以在 java 中编写相同的代码。所以首先我在 MyKotlinFra
我是一名优秀的程序员,十分优秀!