Concurrent computing: Difference between revisions

Content added Content deleted
m (Removed output as defined by task, updated code to match task)
m (Updated code to match task, removed output as defined by task.)
Line 1,042: Line 1,042:


=={{header|Neko}}==
=={{header|Neko}}==
<lang ActionScript>/**
<lang ActionScript>var thread_create = $loader.loadprim("std@thread_create", 2);
Concurrent computing, in Neko
*/

var thread_create = $loader.loadprim("std@thread_create", 2);

var subtask = function(message) {
var subtask = function(message) {
$print(message, "\n");
$print(message, "\n");
}
}

/* The thread functions happen so fast as to look sequential */
thread_create(subtask, "Enjoy");
thread_create(subtask, "Enjoy");
thread_create(subtask, "Rosetta");
thread_create(subtask, "Rosetta");
thread_create(subtask, "Code");
thread_create(subtask, "Code");

/* slow things down */
var sys_sleep = $loader.loadprim("std@sys_sleep", 1);
var sys_sleep = $loader.loadprim("std@sys_sleep", 1);
var random_new = $loader.loadprim("std@random_new", 0);
var random_new = $loader.loadprim("std@random_new", 0);
var random_int = $loader.loadprim("std@random_int", 2);
var random_int = $loader.loadprim("std@random_int", 2);

var randomsleep = function(message) {
var randomsleep = function(message) {
var r = random_new();
var r = random_new();
var sleep = random_int(r, 3);
var sleep = random_int(r, 3);
sys_sleep(sleep);
sys_sleep(sleep);
$print(message, "\n");
$print(message, "\n");
}
}

$print("\nWith random delays\n");
$print("\nWith random delays\n");
thread_create(randomsleep, "Enjoy");
thread_create(randomsleep, "A");
thread_create(randomsleep, "Rosetta");
thread_create(randomsleep, "B");
thread_create(randomsleep, "Code");
thread_create(randomsleep, "C");

/* Let the threads complete */
sys_sleep(4);</lang>
sys_sleep(4);</lang>

{{out}}
<pre>prompt$ nekoc threading.neko
prompt$ neko threading
Enjoy
Rosetta
Code

With random delays
Rosetta
Enjoy
Code</pre>


=={{header|Nim}}==
=={{header|Nim}}==