Concurrent computing: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(4 intermediate revisions by 4 users not shown)
Line 60:
print(w)</syntaxhighlight>
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
{{libheader|gomp}}
{{works with|OpenMP}}
Line 96 ⟶ 97:
Rosetta</pre>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
The BBC BASIC interpreter is single-threaded so the only way of achieving 'concurrency' (short of using assembler code) is to use timer events:
Line 1,025 ⟶ 1,026:
=={{header|J}}==
 
Using J's new threading primitives (in place of some sort of thread emulation):
Example:
 
<syntaxhighlight lang="j"J>reqthreads=: {{ smoutput0&>({~?~T.@#);:'Enjoy Rosetta'^:(0>.y-1 CodeT.'')0 }}
dispatchwith=: (t.'')every
newmutex=: 10&T.
lock=: 11&T.
unlock=: 13&T.
synced=: {{
lock n
r=. u y
unlock n
r
}}
register=: {{ out=: out, y }} synced (newmutex 0)
task=: {{
reqthreads 3 NB. at least 3 worker threads
out=: EMPTY
#@> register dispatchwith ;:'Enjoy Rosetta Code'
out
Enjoy }}</syntaxhighlight>
 
Sample use:
 
<syntaxhighlight lang=J> task''
Enjoy
Rosetta
Code
task''
Enjoy </syntaxhighlight>
Enjoy
 
Code
NOTES AND CAUTIONS:
Rosetta</syntaxhighlight>
 
1) While J's syntax and semantics is highly parallel, it is a deterministic sort of parallelism (analogous to the design of modern GPUs) and not the stochastic parallelism which is implied in this task specification (and which is usually obtained by timeslicing threads of control). The randomness implemented here is orthogonal to the parallelism in the display (and you could remove <code>smoutput&</code> without altering the appearence, in this trivial example).
 
2) The current release of J (and the past implementations) do not implement hardware based concurrency. This is partially an economic issue (since all of the current and past language implementations have been distributed for free, with terms which allow free distribution), and partially a hardware maturity issue (historically, most CPU multi-core development has been optimized for stochastic parallelism with minimal cheap support for large scale deterministic parallelism and GPUs have not been capable of supporting the kind of generality needed by J).
 
This state of affairs is likely to change, eventually (most likely this will be after greater than factor of 2 speedups from hardware parallelism are available for the J users in cases which are common and important enough to support the implementation). But, for now, J's parallelism is entirely conceptual.
 
=={{header|Java}}==
Create a new <code>Thread</code> array, shuffle the array, start each thread.
<syntaxhighlight lang="java">
Thread[] threads = new Thread[3];
threads[0] = new Thread(() -> System.out.println("enjoy"));
threads[1] = new Thread(() -> System.out.println("rosetta"));
threads[2] = new Thread(() -> System.out.println("code"));
Collections.shuffle(Arrays.asList(threads));
for (Thread thread : threads)
thread.start();
</syntaxhighlight>
<br />
An alternate demonstration
{{works with|Java|1.5+}}
Uses CyclicBarrier to force all threads to wait until they're at the same point before executing the println, increasing the odds they'll print in a different order (otherwise, while the they may be executing in parallel, the threads are started sequentially and with such a short run-time, will usually output sequentially as well).
Line 1,930 ⟶ 1,960:
=={{header|Rust}}==
{{libheader|rand}}
<syntaxhighlight lang="rust">extern crate rand; // not needed for recent versions
use std::thread;
use rand::thread_rng;
Line 2,196 ⟶ 2,226:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
 
var words = ["Enjoy", "Rosetta", "Code"]
9,476

edits