scheme corotuine
In cooperative multithreading, a thread must yield control manually; it will not be preemptively switched out.
The API for cooperative multithreading has five functions:
(spawn thunk)puts a thread forthunkinto the thread queue.(quit)kills the current thread and removes it from the thread queue.(yield)hands control from the current thread to another thread.(start-threads)starts executing threads in the thread queue.(halt)exits all threads.
; thread-queue : list[continuation]
(define thread-queue '()) ; halt : continuation
(define halt #f) ; void : -> void
(define (void) (if #f #t)) ; current-continuation : -> continuation
(define (current-continuation)
(call-with-current-continuation
(lambda (cc)
(cc cc)))) ; spawn : (-> anything) -> void
(define (spawn thunk)
(let ((cc (current-continuation)))
(if (procedure? cc)
(set! thread-queue (append thread-queue (list cc)))
(begin (thunk)
(quit))))) ; yield : value -> void
(define (yield)
(let ((cc (current-continuation)))
(if (and (procedure? cc) (pair? thread-queue))
(let ((next-thread (car thread-queue)))
(set! thread-queue (append (cdr thread-queue) (list cc)))
(next-thread 'resume))
(void)))) ; quit : -> ...
(define (quit)
(if (pair? thread-queue)
(let ((next-thread (car thread-queue)))
(set! thread-queue (cdr thread-queue))
(next-thread 'resume))
(halt))) ; start-threads : -> ...
(define (start-threads)
(let ((cc (current-continuation)))
(if cc
(begin
(set! halt (lambda () (cc #f)))
(if (null? thread-queue)
(void)
(begin
(let ((next-thread (car thread-queue)))
(set! thread-queue (cdr thread-queue))
(next-thread 'resume)))))
(void)))) ;; Example cooperatively threaded program
(define counter ) (define (make-thread-thunk name)
(letrec ((loop (lambda ()
(if (< counter )
(quit))
(display "in thread ")
(display name)
(display "; counter = ")
(display counter)
(newline)
(set! counter (- counter ))
(yield)
(loop))))
loop)) (spawn (make-thread-thunk 'a))
(spawn (make-thread-thunk 'b))
(spawn (make-thread-thunk 'c)) (start-threads)
http://schemecookbook.org/Cookbook/CoRoutines
http://schematics.sourceforge.net/scheme-uk/continuations.html
scheme corotuine的更多相关文章
- Partition:Partiton Scheme是否指定Next Used?
在SQL Server中,为Partition Scheme多次指定Next Used,不会出错,最后一次指定的FileGroup是Partition Scheme的Next Used,建议,在执行P ...
- Android业务组件化之URL Scheme使用
前言: 最近公司业务发展迅速,单一的项目工程不再适合公司发展需要,所以开始推进公司APP业务组件化,很荣幸自己能够牵头做这件事,经过研究实现组件化的通信方案通过URL Scheme,所以想着现在还是在 ...
- iOS - URL Scheme 操作
推荐JLRoutes路由跳转 NSScanner 在寻找更加灵活的页面跳转和通知,我遇见了JLRoutes,从而学习使用URL Scheme来定义界面入口.以前从来没有使用过,不过很多大厂和流行的框架 ...
- 自定义 URL Scheme 完全指南
本文由 Migrant 翻译自 The Complete Tutorial on iOS/iPhone Custom URL Schemes,转载请注明出处. 注意: 自从自定义 URL 的引入,本文 ...
- JS魔法堂:Data URI Scheme介绍
一.前言 上周五公司内部的Any Topic Conf.上我和同事们分享了这个主题,有同事说这个有用,有同事说这个没啥用,后来还延伸到网站性能的话题上,大家讨论的激烈程度让我觉得这次选题还不错.本篇先 ...
- CSS魔法堂:小结一下Box Model与Positioning Scheme
前言 对于Box Model和Positioning Scheme中3种定位模式的细节,已经通过以下几篇文章记录了我对其的理解和思考. <CSS魔法堂:重新认识Box Model.IFC.B ...
- Project、Target、Workspace and Scheme
前言 今天有人问我Target和Project是什么关系?额...学习iOS开发都知道Target和Project的关系.这里我就简单的做了一个总结,而且加入的Scheme和Workspace.如果不 ...
- 自定义 URL Scheme 完全指南(转载)
iPhone / iOS SDK 最酷的特性之一就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用. 注册自定义 URL Scheme ...
- 【转】通过自定义的URL Scheme启动你的App
http://blog.csdn.net/ba_jie/article/details/6884818原文地址:http://iphonedevelopertips.com/cocoa/launchi ...
随机推荐
- 【linux驱动分析】ioctl函数的使用
一.用户空间的ioctl int ioctl(int fd, unsigned long cmd, void *data); 第一个參数是文件描写叙述符,第二个參数代表传递的命令,它会原样传 ...
- Netty4具体解释二:开发第一个Netty应用程序
既然是入门,那我们就在这里写一个简单的Demo,client发送一个字符串到server端,server端接收字符串后再发送回client. 2.1.配置开发环境 1.安装JDK 2.去官网下 ...
- eclipse 常见问题及解决
1. Target runtime Apache Tomcat v6.0 is not defined.错误解决方法 原文:http://blog.csdn.net/xw13106209/articl ...
- iOS-UIScrollView的delaysContentTouches与canCencelContentTouches属性
UIScrollView工作原理 在滚动过程当中,其实是在修改原点坐标 UIScrollView有一个BOOL类型的tracking属性,用来返回用户是否已经触及内容并打算开始滚动,我们从这个属性开始 ...
- 删除右键菜单的“用阿里旺旺发送此文件”项
在运行对话框里的输入框内输入Regedit.exe,点击确定按钮就启动了注册表编辑器程序. 在注册表编辑器窗口左侧展开HKEY_CLASSES_ROOT\CLSID{0DE1378D-F811-40E ...
- Python数据类型(元组、列表、字符串、字典)
元组tuple:不可修改的数据类型 ABC = ('a', 1, x, 'today') 列表list:可修改的数据类型 ABC = ['a', 1, x, 'today'] 字符串set: ABC ...
- linux下vi命令笔记
vim 编辑器 全屏编辑器 模式化编辑器 vi:Visual Interfacevim:VI iMproved vi增强版vi模式: 编辑模式(命令模式)(默认处于编辑模式) Ct ...
- 自己做的demo--关于HashMap
package com.pb.collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map ...
- (转)PHP下编码转换函数mb_convert_encoding与iconv的使用说明
之--http://www.jb51.net/article/21451.htm mb_convert_encoding这个函数是用来转换编码的.原来一直对程序编码这一概念不理解,不过现在好像有点开窍 ...
- 项目中常用SQL语句总结
1.项目中常常需要修改字段长度,但需要保留数据--增加业务受理 项目名称 字段长度alter table t_ywsl add aa varchar2(200);update t_ywsl set a ...