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. tomcat work 目录

    用tomcat作web服务器的时候,部署的程序在webApps下,这些程序都是编译后的程序(发布到tomcat的项目里含的类,会被编译成.class后才发布过来,源文件没有发布过来,但这里的jsp没有 ...

  2. PHP中的循环while、do...while、for、foreach四种循环。

    php中的while循环,循环执行代码块制定的次数,或者当指定的条件为真时循环执行代码块. 在我们编写代码是时候,我们经常需要一块代码块重复执行多次.我们就可以使用while循环语句来完成这个任务. ...

  3. PHP利用超级全局变量$_GET来接收表单数据。

    我们在linux上用 touch php_get.html php_get.php创建两个文件. php_get.html文件的代码如下: <!doctype html> <html ...

  4. Unity3D 5.0简单的射线检测实现跳跃功能

    这里是一个简单的跳跃,5.0和其他版本貌似不一样,并且,再起跳功能做的不完全. 不过一个基本的思路在这里. 1.首先,射线检测,这里是利用一个空对象,放到主角对象的下面 2.然后调节射线的位置,在主角 ...

  5. SGU 119.Magic pairs

    题意: 对于给出的一个整数N,和一对(A0,B0) 找到所有的整数对(A,B)满足 : 对于任意 X,Y 当 A0 * X + B0 * Y 能被 N 整除时 A * X + B * Y 也能被 N ...

  6. Xcode 使用自定义字体

    添加对应的字体(.ttf或.odf)到工程的resurce,使用cocos2d中的FontLabel库,FontLabel继承于UILabel,象UILabel一样使用就好了 fontName直接使用 ...

  7. tbody 滚动条

    ARRIVALS TO BRISTOL - ENGLAND FLIGHT CODE FROM STA ETA Notes T3 4264 ISLE OF MAN 11:40 11:42 LANDED ...

  8. underscorejs-shuffle学习

    2.21 shuffle 2.21.1 语法 _.shuffle(list) 2.21.2 说明 返回一个随机乱序的list副本数组, 使用 Fisher-Yates shuffle 来进行随机乱序. ...

  9. css学习--inline-block详解及dispaly:inline inline-block block 三者区别精要概括

    *知识储备: 内联元素:是不可以控制宽和高.margin等:并且在同一行显示,不换行. 块级元素:是可以控制宽和高.margin等,并且会换行. 1.inline-block 详解 (1)一句话就是在 ...

  10. magento站点还原到本地

    问题描述 首先将网站文件夹解压到xampp/htdocs/wenjianjia目录下,然后替换sql文件里的域名为localhost/wenjianjia.然后访问前台,正常.访问后台,出问题了 Ma ...