当前位置: 首页 > news >正文

在线做流程图的网站做网站有兼职的吗

在线做流程图的网站,做网站有兼职的吗,wordpress 官方主题,电商网站建设方案模板#x1f4a2;欢迎来到张胤尘的技术站 #x1f4a5;技术如江河#xff0c;汇聚众志成。代码似星辰#xff0c;照亮行征程。开源精神长#xff0c;传承永不忘。携手共前行#xff0c;未来更辉煌#x1f4a5; 文章目录 Lua | 每日一练 (4)题目参考答案线程和协程调度方式上… 欢迎来到张胤尘的技术站 技术如江河汇聚众志成。代码似星辰照亮行征程。开源精神长传承永不忘。携手共前行未来更辉煌 文章目录 Lua | 每日一练 (4)题目参考答案线程和协程调度方式上下文切换资源占用实现机制使用场景 lua 中的协程协程的生命周期主要函数创建协程启动或恢复协程检查当前是否在主协程中运行暂停协程检测协程是否可暂停获取协程状态包装函数关闭协程 具体使用 Lua | 每日一练 (4) 题目 协程和线程有何区别简述 lua 中的协程。 参考答案 线程和协程 协程和线程虽然在某些方面有相似之处但它们在设计目标、实现原理和使用方式上有很大的区别。下面从调用方式、上下文切换、资源使用、实现机制、使用场景这几个方面进行阐述。 调度方式 线程线程的执行由操作系统内核控制操作系统会根据调度算法如时间片轮转、优先级调度等自动切换线程的执行。另外线程的切换时间点不可预测程序无法直接控制线程的暂停和恢复。协程协程的执行由程序显式控制需要开发者通过 coroutine.yield 和 coroutine.resume 显式地暂停和恢复协程。协程的切换完全由程序逻辑决定切换点是明确的。 上下文切换 线程线程切换涉及操作系统内核的上下文切换需要保存和恢复线程的寄存器状态、栈信息等开销较大。协程协程的上下文切换在用户态完成不需要操作系统内核介入开销非常小。 资源占用 线程每个线程都有自己的栈空间通常默认分配 8 MB资源占用较大。如果线程数量过多会导致系统资源耗尽。 $ ulimit -s 8192协程协程的栈空间是动态分配的通常占用较少的内存。另外协程的数量可以非常大适合处理大规模的并发任务。 实现机制 线程线程是操作系统提供的并发机制由操作系统内核管理。线程的创建和销毁需要系统调用涉及内核态和用户态的切换。协程协程是语言层面的机制由 lua 解释器实现。协程的创建和切换完全在用户态完成不涉及操作系统内核。 使用场景 线程适合处理真正的并发任务例如多核 CPU 上的并行计算处理 I/O 密集型任务。协程适合处理单核 CPU 上的并发任务尤其是需要频繁切换的场景适合实现非阻塞 I/O 操作例如网络编程中的异步请求处理。 lua 中的协程 在 lua 中协程是实现异步编程的核心工具之一。由于 lua 本身没有内置的多线程支持而协程提供了一种轻量级的并发机制可以用来模拟异步操作从而实现非阻塞的程序设计。 协程的生命周期 lua 协程的生命周期包括以下几个阶段 创建使用 coroutine.create 创建一个协程。此时协程处于挂起状态尚未开始执行。运行使用 coroutine.resume 启动或恢复协程的执行。暂停在协程执行过程中可以通过 coroutine.yield 暂停协程的执行将控制权交回主程序。结束当协程运行完成或因错误终止时协程进入结束状态。 协程的状态之间切换如下图所示 主要函数 下面介绍 lua 中关于协程的主要函数。 创建协程 local co coroutine.create(f)f协程函数表示协程的主体逻辑。返回一个协程对象 co类型为 thread。协程对象可以用于后续的 coroutine.resume 和 coroutine.yield 等操作。 例如 -- func 是协程函数主体逻辑 local function func()print(Coroutine is running) end-- 创建协程 local co coroutine.create(func) print(co) -- thread: 0x60f0c8666df8启动或恢复协程 ok, ... coroutine.resume(co, ...)co要启动或者恢复的协程对象。...可选参数这些参数会传递给协程中的 coroutine.yield 或协程的入口函数。ok布尔值表示协程是否成功恢复。如果协程因错误终止返回 false。...协程中 coroutine.yield 的返回值。如果协程运行完成返回值为 nil。 说明 如果协程处于 suspended 状态coroutine.resume 会启动或恢复协程的执行。如果协程已经处于 running 状态调用 coroutine.resume 会抛出错误。如果协程已经处于 dead 状态调用 coroutine.resume 也会抛出错误。 例如 local function func(x, y)print(Coroutine started with:, x, y) -- Coroutine started with: 10 20 endlocal co coroutine.create(func)local ok, result coroutine.resume(co, 10, 20) print(ok, result) -- true nil检查当前是否在主协程中运行 current_co, is_main coroutine.running()current_co当前运行的协程对象。is_main布尔值表示当前是否在主协程中运行如果是主协程返回 true否则返回 false。 说明 用于检查当前是否在主协程中运行以及当前协程是否是主线程。 例如 local function printCurrentCoroutine()local current_co, is_main coroutine.running()print(current_co, is_main) endprintCurrentCoroutine() -- thread: 0x61617e0492a8 truelocal co coroutine.create(function()print(Printing from the coroutine) -- Printing from the coroutineprintCurrentCoroutine() -- thread: 0x61617e04fec8 false end)coroutine.resume(co) -- 启动协程暂停协程 ... coroutine.yield(...)...可选参数这些参数会传递给调用 coroutine.resume 的代码。返回值是 coroutine.resume 调用时传递的参数。 说明 coroutine.yield 用于暂停当前协程的执行并将控制权返回给调用 coroutine.resume 的代码。协程暂停后可以通过再次调用 coroutine.resume 恢复执行。 例如 local function func()print(Coroutine running) -- Coroutine runninglocal value coroutine.yield(Yielded value)print(Coroutine resumed with:, value) -- Coroutine resumed with: Hello endlocal co coroutine.create(func) local ok, result coroutine.resume(co) print(ok, result) -- true Yielded valuelocal ok, result coroutine.resume(co, Hello) print(ok, result) -- true nil检测协程是否可暂停 is_yieldable coroutine.isyieldable([co])co可选参数表示要检查的协程对象。默认为当前运行的协程。is_yieldable布尔值表示协程是否可以暂停。 说明 如果协程不是主协程且不在非可暂停的 C 函数中则返回 true。主协程调用时返回 false。协程处于 suspended、dead 状态时 coroutine.isyieldable 仍然返回 true因为协程对象本身仍然是有效的并且在 lua 的语义中dead 状态的协程仍然可以被认为是可以暂停的尽管它已经无法再被恢复。 例如 local co coroutine.create(function()print(coroutine.isyieldable()) -- truecoroutine.yield() end)coroutine.resume(co) -- 启动协程 print(coroutine.isyieldable(co)) -- true print(coroutine.status(co)) -- suspended coroutine.resume(co) print(coroutine.status(co)) -- dead print(coroutine.isyieldable(co)) -- true print(coroutine.isyieldable()) -- false获取协程状态 status coroutine.status(co)co要检查状态的协程对象。返回一个字符串表示协程的当前状态 running协程正在运行。suspended协程处于暂停状态。dead协程已经运行完成或因错误终止。normal协程尚未启动初始状态。 例如 local function func(co)local status coroutine.status(co)print(status) -- runningcoroutine.yield(hello, world!) endlocal co coroutine.create(func) local status coroutine.status(co) print(status) -- suspendedlocal ok, result coroutine.resume(co, co) print(ok, result) -- true hello, world!local ok, result coroutine.resume(co, co) print(ok, result) -- true nillocal status coroutine.status(co) print(status) -- dead 包装函数 f coroutine.wrap(f)f协程函数表示协程的主体逻辑。返回一个包装函数 f。调用这个包装函数时会自动启动或恢复协程的执行。 说明 coroutine.wrap 是 coroutine.create 和 coroutine.resume 的简化版本。包装函数的返回值是协程中 coroutine.yield 的参数。如果协程运行完成包装函数返回 nil如果协程因错误终止会抛出错误。 例如 local wrapped coroutine.wrap(function()print(Coroutine running)local value coroutine.yield(Yielded value)print(Coroutine resumed with:, value) end)local result wrapped() print(result) -- Yielded valuewrapped(Hello) -- Coroutine resumed with: Hello关闭协程 ok, err coroutine.close(co)co要关闭的协程对象协程的状态必须是 suspended 或 dead 状态。 ok布尔值表示操作是否成功。 err如果操作失败返回错误信息。 说明 关闭协程 co将其状态设置为 dead并关闭协程中所有待关闭的变量。如果协程已经是 dead 状态调用 coroutine.close 会返回 true。 例如 local co coroutine.create(function()coroutine.yield() -- 暂停协程 end)coroutine.resume(co) -- 启动协程 print(coroutine.status(co)) -- suspendedlocal ok, err coroutine.close(co) -- 关闭协程 print(ok, err) -- true nil print(coroutine.status(co)) -- dead具体使用 使用协程实现经典的生产者-消费者模型。协程的暂停和恢复特性非常适合这种场景因为生产者和消费者可以分别在协程中运行通过共享队列进行通信。 一个简单的生产者-消费者模型实现如下所示 -- 队列实现 local Queue {}function Queue:new()local obj {}setmetatable(obj, self)self.__index selfobj.list {}return obj endfunction Queue:put(item)table.insert(self.list, item) endfunction Queue:get()if #self.list 0 thenreturn nilendlocal item self.list[1]table.remove(self.list, 1)return item endfunction Queue:is_empty()return #self.list 0 end-- 生产者函数 local function producer(queue, count)for i 1, count doprint(Produced:, i)queue:put(i)coroutine.yield()end end-- 消费者函数 local function consumer(queue, count)for i 1, count dowhile queue:is_empty() doprint(Waiting for item...)coroutine.yield()endlocal item queue:get()print(Consumed:, item)end endlocal queue Queue:new() local count 100 -- 生产/消费数量-- 创建生产者和消费者协程 local co_producer coroutine.create(function()producer(queue, count) end)local co_consumer coroutine.create(function()consumer(queue, count) end)-- 同时运行生产者和消费者协程 while coroutine.status(co_producer) ~ dead or coroutine.status(co_consumer) ~ dead doif coroutine.status(co_producer) ~ dead thencoroutine.resume(co_producer)endif coroutine.status(co_consumer) ~ dead thencoroutine.resume(co_consumer)end end撒花 如果本文对你有帮助就点关注或者留个 如果您有任何技术问题或者需要更多其他的内容请随时向我提问。
http://www.pierceye.com/news/83291/

相关文章:

  • 塔城市建设局网站做空eth网站
  • 上海网站设计找哪家昆明网站词排名优化
  • 网站做商标在那类网络美工是干啥的
  • 沌口网站建设网站推广如何做的
  • 淘客app网站是怎么做的c 做网站 知乎
  • 网站套模板教程轻创优选地推app
  • 长春建设工程信息网站广西建设工程造价管理协会网站
  • 加盟网站制作公司wordpress 国内镜像
  • 苏州网站排名方案怎么做自己的快递查询网站
  • python可以做网站后台吗免费域名注册平台永久
  • 昆明网站建设首选才力北京定制网络营销推广
  • 网站盈利了湖北网站建设xiduyun
  • 佛山外贸网站建设行情关键词搜索指数
  • 织梦调用网站名称两学一做 知识竞赛网站
  • 途牛网站建设方案电商网络销售是做什么
  • 珠海建网站设计asp.net做的小网站
  • 做网站怎么赚钱广告杭州网站改版
  • 江门网站推广多少钱网站代码字体变大
  • 全定制网站开发湖北省住建厅网站官网
  • 建设科普网站的意义盘锦兴隆台住房和城乡建设网站
  • 竞价网站策划网站后台数据库下载
  • 网页设计怎么建站点莱芜求职信息查询
  • 网站优化 月付费潍坊网站制作发
  • 网站目录做跳转做网站前期构架图
  • 徐州企业网站推广wordpress菜单 自定义菜单
  • 用iis做网站福建做网站公司排名
  • 霸州建设局网站九天利建公司简介
  • 找做课件的网站专门做简历的网站有哪些
  • 中国空间站合作的17个国家网站设计师工资一般多少
  • 制作网站后台怎样做易支付网站