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 for thunk into 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.

[coop-threads.scm]

; 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://matt.might.net/articles/programming-with-continuations--exceptions-backtracking-search-threads-generators-coroutines/

http://schemecookbook.org/Cookbook/CoRoutines

http://schematics.sourceforge.net/scheme-uk/continuations.html

scheme corotuine的更多相关文章

  1. Partition:Partiton Scheme是否指定Next Used?

    在SQL Server中,为Partition Scheme多次指定Next Used,不会出错,最后一次指定的FileGroup是Partition Scheme的Next Used,建议,在执行P ...

  2. Android业务组件化之URL Scheme使用

    前言: 最近公司业务发展迅速,单一的项目工程不再适合公司发展需要,所以开始推进公司APP业务组件化,很荣幸自己能够牵头做这件事,经过研究实现组件化的通信方案通过URL Scheme,所以想着现在还是在 ...

  3. iOS - URL Scheme 操作

    推荐JLRoutes路由跳转 NSScanner 在寻找更加灵活的页面跳转和通知,我遇见了JLRoutes,从而学习使用URL Scheme来定义界面入口.以前从来没有使用过,不过很多大厂和流行的框架 ...

  4. 自定义 URL Scheme 完全指南

    本文由 Migrant 翻译自 The Complete Tutorial on iOS/iPhone Custom URL Schemes,转载请注明出处. 注意: 自从自定义 URL 的引入,本文 ...

  5. JS魔法堂:Data URI Scheme介绍

    一.前言 上周五公司内部的Any Topic Conf.上我和同事们分享了这个主题,有同事说这个有用,有同事说这个没啥用,后来还延伸到网站性能的话题上,大家讨论的激烈程度让我觉得这次选题还不错.本篇先 ...

  6. CSS魔法堂:小结一下Box Model与Positioning Scheme

    前言  对于Box Model和Positioning Scheme中3种定位模式的细节,已经通过以下几篇文章记录了我对其的理解和思考.  <CSS魔法堂:重新认识Box Model.IFC.B ...

  7. Project、Target、Workspace and Scheme

    前言 今天有人问我Target和Project是什么关系?额...学习iOS开发都知道Target和Project的关系.这里我就简单的做了一个总结,而且加入的Scheme和Workspace.如果不 ...

  8. 自定义 URL Scheme 完全指南(转载)

    iPhone / iOS SDK 最酷的特性之一就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用. 注册自定义 URL Scheme ...

  9. 【转】通过自定义的URL Scheme启动你的App

    http://blog.csdn.net/ba_jie/article/details/6884818原文地址:http://iphonedevelopertips.com/cocoa/launchi ...

随机推荐

  1. LINUX 性能 测试 优化工具

    监控 测试 优化

  2. mapreduce实战:统计美国各个气象站30年来的平均气温项目分析

    气象数据集 我们要写一个气象数据挖掘的程序.气象数据是通过分布在美国各地区的很多气象传感器每隔一小时进行收集,这些数据是半结构化数据且是按照记录方式存储的,因此非常适合使用 MapReduce 程序来 ...

  3. mysql 异常处理实例

    1. 语法: DECLARE handler_action HANDLER FOR condition_value [, condition_value] ... statement handler_ ...

  4. Android 开发实践 ViewGroup 实现左右滑出窗口(一)

    利用假期把以前做的东西总结整理一下,先从简单的开始吧.实现的效果是这样的:   做了个截屏动画,比例有点不对了,凑合着看吧. 整个窗口有3部分组成,中间的主界面是个列表,左边的滑出界面是个菜单,右边的 ...

  5. linux的openfire运行日志配置经历

    openfire的日志可以通过/usr/openfire/lib/log4j.xml(与openfire的安装目录有关,我的openfire是安装在/usr/openfire/)的xml配置文件进行设 ...

  6. 使用Convert 类和Parse方法将字符串转换为数值类型

    //用Parse方法将字符串转换为数值类型; long num=Int64.Parse(args[2]) //用别名为Int64c#类型long; long num=long.Parse(args[2 ...

  7. java web(jsp)-The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    在静态项目上新建 jsp文件的时候,报错:The superclass "javax.servlet.http.HttpServlet" was not found on the ...

  8. .net RAW(16)与GUID互相转换

    .net 1.raw转guidnew guid(byte[] id);2.guid转rawGuid result;string ids = BitConverter.ToString(result.T ...

  9. What and where are the stack and heap?

    The stack is the memory set aside as scratch space for a thread of execution. When a function is cal ...

  10. JavaScript HTML DOM 事件

    JavaScript HTML DOM 事件 HTML DOM 使 JavaScript 有能力对 HTML 事件做出反应. 实例 Mouse Over Me 对事件做出反应 我们可以在事件发生时执行 ...