Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A heads up, this example is not concise nor exceptionally clear. This is a demonstration of a powerful application of continuations. As a VB/ASP/C# programmer, you may not be familiar with the concept of a system stack or saving state, so the goal of this answer is a demonstration and not an explanation.</p> <p>Continuations are extremely versatile and are a way to save execution state and resume it later. Here is a small example of a cooperative multithreading environment using continuations in Scheme:</p> <p>(Assume that the operations enqueue and dequeue work as expected on a global queue not defined here)</p> <pre><code>(define (fork) (display "forking\n") (call-with-current-continuation (lambda (cc) (enqueue (lambda () (cc #f))) (cc #t)))) (define (context-switch) (display "context switching\n") (call-with-current-continuation (lambda (cc) (enqueue (lambda () (cc 'nothing))) ((dequeue))))) (define (end-process) (display "ending process\n") (let ((proc (dequeue))) (if (eq? proc 'queue-empty) (display "all processes terminated\n") (proc)))) </code></pre> <p>This provides three verbs that a function can use - fork, context-switch, and end-process. The fork operation forks the thread and returns #t in one instance and #f in another. The context-switch operation switches between threads, and end-process terminates a thread.</p> <p>Here's an example of their use:</p> <pre><code>(define (test-cs) (display "entering test\n") (cond ((fork) (cond ((fork) (display "process 1\n") (context-switch) (display "process 1 again\n")) (else (display "process 2\n") (end-process) (display "you shouldn't see this (2)")))) (else (cond ((fork) (display "process 3\n") (display "process 3 again\n") (context-switch)) (else (display "process 4\n"))))) (context-switch) (display "ending process\n") (end-process) (display "process ended (should only see this once)\n")) </code></pre> <p>The output should be</p> <pre><code>entering test forking forking process 1 context switching forking process 3 process 3 again context switching process 2 ending process process 1 again context switching process 4 context switching context switching ending process ending process ending process ending process ending process ending process all processes terminated process ended (should only see this once) </code></pre> <p>Those that have studied forking and threading in a class are often given examples similar to this. The purpose of this post is to demonstrate that with continuations you can achieve similar results within a single thread by saving and restoring its state - its continuation - manually.</p> <p>P.S. - I think I remember something similar to this in On Lisp, so if you'd like to see professional code you should check the book out.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload