Active object: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(20 intermediate revisions by 13 users not shown)
Line 1:
[[Category:Object oriented]]
{{task|Concurrency}}
[[Category:Object oriented]]{{requires|Concurrency}}{{requires|Objects}}{{requires|Mutable State}}
 
In [[object-oriented programming]] an object is active when its state depends on clock. Usually an active object encapsulates a [[task]] that updates the object's state. To the outer world the object looks like a normal object with methods that can be called from outside. Implementation of such methods must have a certain synchronization mechanism with the encapsulated task in order to prevent object's state corruption.
Line 18 ⟶ 19:
Verify that now the object's output is approximately 0 (the sine has the period of 2s). The accuracy of the result will depend on the [[OS]] scheduler time slicing and the accuracy of the clock.
 
{{omit from|VBScript}}
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Calendar; use Ada.Calendar;
with Ada.Numerics; use Ada.Numerics;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
Line 85:
Put_Line ("Integrated" & Float'Image (S) & "s");
I.Shut_Down;
end Test_Integrator;</langsyntaxhighlight>
Sample output:
<pre>
Line 91:
</pre>
 
=={{header|BBC BASICATS}}==
 
No memory management is needed. Everything is done in registers or on the stack, including the closure that is used to connect signal source and integrator.
 
The core code is templates for any floating point type, but, in the end, one must choose a type. Our choice is C '''float''' type.
 
<syntaxhighlight lang="ats">
(*------------------------------------------------------------------*)
(* I will not bother with threads. All we need is the ability to get
the time from the operating system. This is available as
clock(3). *)
 
#define ATS_PACKNAME "rosettacode.activeobject"
#define ATS_EXTERN_PREFIX "rosettacode_activeobject_"
 
#include "share/atspre_staload.hats"
 
(*------------------------------------------------------------------*)
(* Some math functionality, for all the standard floating point
types. The ats2-xprelude package includes this, and more, but one
may wish to avoid the dependency. And there is support for math
functions in libats/libc, but not with typekinds. *)
 
%{^
#include <math.h>
// sinpi(3) would be better than sin(3), but I do not yet have
// sinpi(3).
#define rosettacode_activeobject_pi \
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214L
#define rosettacode_activeobject_sinpi_float(x) \
(sinf (((atstype_float) rosettacode_activeobject_pi) * (x)))
#define rosettacode_activeobject_sinpi_double \
(sin (((atstype_double) rosettacode_activeobject_pi) * (x)))
#define rosettacode_activeobject_sinpi_ldouble \
(sinl (((atstype_ldouble) rosettacode_activeobject_pi) * (x)))
%}
 
extern fn sinpi_float : float -<> float = "mac#%"
extern fn sinpi_double : double -<> double = "mac#%"
extern fn sinpi_ldouble : ldouble -<> ldouble = "mac#%"
extern fn {tk : tkind} g0float_sinpi : g0float tk -<> g0float tk
implement g0float_sinpi<fltknd> x = sinpi_float x
implement g0float_sinpi<dblknd> x = sinpi_double x
implement g0float_sinpi<ldblknd> x = sinpi_ldouble x
 
overload sinpi with g0float_sinpi
 
(*------------------------------------------------------------------*)
(* Some clock(3) functionality for the three standard floating point
types. *)
 
%{^
#include <time.h>
 
typedef clock_t rosettacode_activeobject_clock_t;
 
ATSinline() rosettacode_activeobject_clock_t
rosettacode_activeobject_clock () // C23 drops the need for "void".
{
return clock ();
}
 
ATSinline() rosettacode_activeobject_clock_t
rosettacode_activeobject_clock_difference
(rosettacode_activeobject_clock_t t,
rosettacode_activeobject_clock_t t0)
{
return (t - t0);
}
 
ATSinline() atstype_float
rosettacode_activeobject_clock_scaled2float
(rosettacode_activeobject_clock_t t)
{
return ((atstype_float) t) / CLOCKS_PER_SEC;
}
 
ATSinline() atstype_double
rosettacode_activeobject_clock_scaled2double
(rosettacode_activeobject_clock_t t)
{
return ((atstype_double) t) / CLOCKS_PER_SEC;
}
 
ATSinline() atstype_ldouble
rosettacode_activeobject_clock_scaled2ldouble
(rosettacode_activeobject_clock_t t)
{
return ((atstype_ldouble) t) / CLOCKS_PER_SEC;
}
%}
 
typedef clock_t = $extype"clock_t"
extern fn clock : () -<> clock_t = "mac#%"
extern fn clock_difference : (clock_t, clock_t) -<> clock_t = "mac#%"
extern fn clock_scaled2float : clock_t -<> float = "mac#%"
extern fn clock_scaled2double : clock_t -<> double = "mac#%"
extern fn clock_scaled2ldouble : clock_t -<> ldouble = "mac#%"
 
extern fn {tk : tkind} clock_scaled2g0float : clock_t -<> g0float tk
 
implement clock_scaled2g0float<fltknd> t = clock_scaled2float t
implement clock_scaled2g0float<dblknd> t = clock_scaled2double t
implement clock_scaled2g0float<ldblknd> t = clock_scaled2ldouble t
 
overload - with clock_difference
overload clock2f with clock_scaled2g0float
 
(*------------------------------------------------------------------*)
 
%{^
#if defined __GNUC__ && (defined __i386__ || defined __x86_64__)
// A small, machine-dependent pause, for improved performance of spin
// loops.
#define rosettacode_activeobject_pause() __builtin_ia32_pause ()
#else
// Failure to insert a small, machine-dependent pause may overwork
// your hardware, but the task can be done anyway.
#define rosettacode_activeobject_pause() do{}while(0)
#endif
%}
 
extern fn pause : () -<> void = "mac#%"
 
(*------------------------------------------------------------------*)
 
(* Types such as this can have their internals hidden, but here I will
not bother with such details. *)
vtypedef sinusoidal_generator (tk : tkind) =
@{
phase = g0float tk,
afreq = g0float tk, (* angular frequency IN UNITS OF 2*pi. *)
clock0 = clock_t,
stopped = bool
}
 
fn {tk : tkind}
sinusoidal_generator_Initize
(gen : &sinusoidal_generator tk?
>> sinusoidal_generator tk,
phase : g0float tk,
afreq : g0float tk) : void =
gen := @{phase = phase,
afreq = afreq,
clock0 = clock (),
stopped = true}
 
fn {tk : tkind}
sinusoidal_generator_Start
(gen : &sinusoidal_generator tk) : void =
gen.stopped := false
 
(* IMO changing the integrator's input is bad OO design: akin to
unplugging one generator and plugging in another. What we REALLY
want is to have the generator produce a different signal. So
gen.Stop() will connect the output to a constant
zero. (Alternatively, the channel between the signal source and the
integrator could effect the shutoff.) *)
fn {tk : tkind}
sinusoidal_generator_Stop
(gen : &sinusoidal_generator tk) : void =
gen.stopped := true
 
fn {tk : tkind}
sinusoidal_generator_Sample
(gen : !sinusoidal_generator tk) : g0float tk =
let
val @{phase = phase,
afreq = afreq,
clock0 = clock0,
stopped = stopped} = gen
in
if stopped then
g0i2f 0
else
let
val t = (clock2f (clock () - clock0)) : g0float tk
in
sinpi ((afreq * t) + phase)
end
end
 
overload .Initize with sinusoidal_generator_Initize
overload .Start with sinusoidal_generator_Start
overload .Stop with sinusoidal_generator_Stop
overload .Sample with sinusoidal_generator_Sample
 
(*------------------------------------------------------------------*)
 
vtypedef inputter (tk : tkind, p : addr) =
(* This is a closure type that can reside either in the heap or on
the stack. *)
@((() -<clo1> g0float tk) @ p | ptr p)
 
vtypedef active_integrator (tk : tkind, p : addr) =
@{
inputter = inputter (tk, p),
t_last = clock_t,
sample_last = g0float tk,
integral = g0float tk
}
vtypedef active_integrator (tk : tkind) =
[p : addr] active_integrator (tk, p)
 
fn {tk : tkind}
active_integrator_Input
{p : addr}
(igrator : &active_integrator tk?
>> active_integrator (tk, p),
inputter : inputter (tk, p)) : void =
let
val now = clock ()
in
igrator := @{inputter = inputter,
t_last = now,
sample_last = g0i2f 0,
integral = g0i2f 0}
end
 
fn {tk : tkind}
active_integrator_Output
{p : addr}
(igrator : !active_integrator (tk, p)) : g0float tk =
igrator.integral
 
fn {tk : tkind}
active_integrator_Integrate
{p : addr}
(igrator : &active_integrator (tk, p)) : void =
let
val @{inputter = @(pf | p),
t_last = t_last,
sample_last = sample_last,
integral = integral} = igrator
macdef inputter_closure = !p
 
val t_now = clock ()
val sample_now = inputter_closure ()
 
val integral = integral + ((sample_last + sample_now)
* clock2f (t_last - t_now)
* g0f2f 0.5)
val sample_last = sample_now
val t_last = t_now
 
val () = igrator := @{inputter = @(pf | p),
t_last = t_last,
sample_last = sample_last,
integral = integral}
in
end
 
overload .Input with active_integrator_Input
overload .Output with active_integrator_Output
overload .Integrate with active_integrator_Integrate
 
(*------------------------------------------------------------------*)
 
implement
main () =
let
(* We put on the stack all objects that are not in registers. Thus
we avoid the need for malloc/free. *)
 
vtypedef gen_vt = sinusoidal_generator float_kind
vtypedef igrator_vt = active_integrator float_kind
 
var gen : gen_vt
var igrator : igrator_vt
 
val phase = 0.0f
and afreq = 1.0f (* Frequency of 0.5 Hz. *)
val () = gen.Initize (phase, afreq)
val () = gen.Start ()
 
(* Create a thunk on the stack. This thunk acts as a channel
between the sinusoidal generator and the active integrator. We
could probably work this step into the OO style of most of the
code, but doing that is left as an exercise. The mechanics of
creating a closure on the stack are already enough for a person
to absorb. (Of course, rather than use a closure, we could have
set up a type hierarchy. However, IMO a type hierarchy is
needlessly clumsy. Joining the objects with a closure lets any
thunk of the correct type serve as input.) *)
val p_gen = addr@ gen
var gen_clo_on_stack =
lam@ () : float =<clo1>
let
(* A little unsafeness is needed here. AFAIK there is no way
to SAFELY enclose the stack variable "gen" in the
closure. A negative effect is that (at least without some
elaborate scheme) it becomes POSSIBLE to use this
closure, even after "gen" has been destroyed. But we will
be careful not to do that. *)
extern praxi p2view :
{p : addr} ptr p -<prf>
(gen_vt @ p, gen_vt @ p -<lin,prf> void)
prval @(pf, fpf) = p2view p_gen
macdef gen = !p_gen
val sample = gen.Sample ()
prval () = fpf pf
in
sample
end
val sinusoidal_inputter =
@(view@ gen_clo_on_stack | addr@ gen_clo_on_stack)
 
val () = igrator.Input (sinusoidal_inputter)
 
fn {}
integrate_for_seconds
(igrator : &igrator_vt,
seconds : float) : void =
let
val t0 = clock2f (clock ())
fun
loop (igrator : &igrator_vt) : void =
if clock2f (clock ()) - t0 < seconds then
begin
igrator.Integrate ();
pause ();
loop igrator
end
in
loop igrator
end
 
(* Start the sinusoid and then integrate for 2.0 seconds. *)
val () = gen.Start ()
val () = integrate_for_seconds (igrator, 2.0f)
 
(* Stop the sinusoid and then integrate for 0.5 seconds. *)
val () = gen.Stop ()
val () = integrate_for_seconds (igrator, 0.5f)
 
val () = println! ("integrator output = ", igrator.Output ());
 
(* The following "prval" lines are necessary for type-safety, and
produce no executable code. *)
prval @{inputter = @(pf | _),
t_last = _,
sample_last = _,
integral = _} = igrator
prval () = view@ gen_clo_on_stack := pf
in
0
end
 
(*------------------------------------------------------------------*)
</syntaxhighlight>
 
{{out}}
One will get different results on different runs.
<pre>$ patscc -std=gnu2x -Ofast active_object_task.dats -lm && ./a.out
integrator output = -0.000002</pre>
 
=={{header|BASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"CLASSLIB"
INSTALL @lib$+"TIMERLIB"
INSTALL @lib$+"NOWAIT"
Line 115 ⟶ 472:
PROCwait(50)
PRINT "Final value = " FN(myinteg.output)
PROC_discard(myinteg{})</langsyntaxhighlight>
Output:
<pre>
Line 125 ⟶ 482:
{{libheader|pthread}}
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Line 194 ⟶ 551:
 
return 0;
}</langsyntaxhighlight>
output
<pre>-9.99348e-05</pre>
Line 200 ⟶ 557:
=={{header|C sharp|C#}}==
{{works with|C# 6}}
<langsyntaxhighlight lang="csharp">using System;
using System.Threading.Tasks;
 
Line 277 ⟶ 634:
Console.WriteLine(ao.Value);
}
}</langsyntaxhighlight>
 
Output:
Line 284 ⟶ 641:
=={{header|C++}}==
{{works with|C++14|}}
<langsyntaxhighlight lang="cpp">#include <atomic>
#include <chrono>
#include <cmath>
Line 341 ⟶ 698:
void Integrator::do_work()
{
while (continue_.test_and_set()) {
integrate();
std::this_thread::sleep_for(1ms);
Line 353 ⟶ 710:
dur_t start = t_prev - beginning;
dur_t fin = now - beginning;
if (func)
state += (func(start.count()) + func(fin.count())) * (fin - start).count() / 2;
t_prev = now;
Line 372 ⟶ 729:
std::this_thread::sleep_for(500ms);
std::cout << foo.output();
}</langsyntaxhighlight>
output
<pre>1.23136e-011</pre>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(ns active-object
(:import (java.util Timer TimerTask)))
 
Line 413 ⟶ 770:
user> (test-integrator)
1.414065859052494E-5
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
{{libheader|Bordeaux Threads}}
 
<syntaxhighlight lang="lisp">
(defclass integrator ()
((input :initarg :input :writer input :reader %input)
(lock :initform (bt:make-lock) :reader lock)
(start-time :initform (get-internal-real-time) :reader start-time)
(interval :initarg :interval :reader interval)
(thread :reader thread :writer %set-thread)
(area :reader area :initform 0 :accessor %area)))
 
(defmethod shared-initialize
((integrator integrator) slot-names &key (interval nil interval-s-p) &allow-other-keys)
(declare (ignore interval))
(cond
;; Restart the thread if any unsynchronized slots are
;; being initialized
((or
(eql slot-names t)
(member 'thread slot-names)
(member 'interval slot-names)
(member 'start-time slot-names)
(member 'lock slot-names)
interval-s-p)
;; If the instance already has a thread, stop it and wait for it
;; to stop before initializing any slots
(when (slot-boundp integrator 'thread)
(input nil integrator)
(bt:join-thread (thread integrator)))
(call-next-method)
(let* ((now (get-internal-real-time))
(current-value (funcall (%input integrator) (- (start-time integrator) now))))
(%set-thread
(bt:make-thread
(lambda ()
(loop
;; Sleep for the amount required to reach the next interval;
;; mitigates drift from theoretical interval times
(sleep
(mod
(/ (- (start-time integrator) (get-internal-real-time))
internal-time-units-per-second)
(interval integrator)))
(let* ((input
(bt:with-lock-held ((lock integrator))
;; If input is nil, exit the thread
(or (%input integrator) (return))))
(previous-time (shiftf now (get-internal-real-time)))
(previous-value
(shiftf
current-value
(funcall input (/ (- now (start-time integrator)) internal-time-units-per-second)))))
(bt:with-lock-held ((lock integrator))
(incf (%area integrator)
(*
(/ (- now previous-time)
internal-time-units-per-second)
(/ (+ previous-value current-value)
2)))))))
:name "integrator-thread")
integrator)))
(t
;; If lock is not in SLOT-NAMES, it must already be initialized,
;; so it can be taken while slots synchronized to it are set
(bt:with-lock-held ((lock integrator))
(call-next-method)))))
 
(defmethod input :around (new-value (integrator integrator))
(bt:with-lock-held ((lock integrator))
(call-next-method)))
 
(defmethod area :around ((integrator integrator))
(bt:with-lock-held ((lock integrator))
(call-next-method)))
 
(let ((integrator
(make-instance 'integrator
:input (lambda (time) (sin (* 2 pi 0.5 time)))
:interval 1/1000)))
(unwind-protect
(progn
(sleep 2)
(input (constantly 0) integrator)
(sleep 0.5)
(format t "~F~%" (area integrator)))
(input nil integrator)))
</syntaxhighlight>
 
=={{header|Crystal}}==
{{trans|Python}}
Crystal currently runs all code in a single thread, so a trivial example wouldn't have any issues with thread safety. However, this behavior will likely change in the future. This example was written with that in mind, and is somewhat more complex to show better idioms and be future-proof.
<syntaxhighlight lang="ruby">require "math"
require "time"
 
# this enum allows us to specify what type of message the proc_chan received.
# this trivial example only has one action, but more enum members can be added
# to update the proc, or take other actions
enum Action
Finished # we've waited long enough, and are asking for our result
# Update # potential member representing an update to the integrator function
end
 
class Integrator
property interval : Float64
getter s : Float64 = 0f64
 
# initialize our k function as a proc that takes a float and just returns 0
getter k : Proc(Float64, Float64) = ->(t : Float64) { 0f64 }
 
# channels used for communicating with the main fiber
@proc_chan : Channel(Tuple(Action, Proc(Float64, Float64)|Nil))
@result_chan : Channel(Float64)
 
def initialize(@k, @proc_chan, @result_chan, @interval = 1e-4)
# use a monotonic clock for accuracy
start = Time.monotonic.total_seconds
t0, k0 = 0f64, @k.call(0f64)
 
loop do
# this sleep returns control to the main fiber. if the main fiber hasn't finished sleeping,
# control will be returned to this loop
sleep interval.seconds
# check the channel to see if the function has changed
self.check_channel()
t1 = Time.monotonic.total_seconds - start
k1 = @k.call(t1)
@s += (k1 + k0) * (t1 - t0) / 2.0
t0, k0 = t1, k1
end
end
 
# check the proc_chan for messages, update the integrator function or send the result as needed
def check_channel
select
when message = @proc_chan.receive
action, new_k = message
case action
when Action::Finished
@result_chan.send @s
@k = new_k unless new_k.nil?
end
else
nil
end
end
end
 
# this channel allows us to update the integrator function,
# and inform the integrator to send the result over the result channel
proc_chan = Channel(Tuple(Action, Proc(Float64, Float64)|Nil)).new
 
# channel used to return the result from the integrator
result_chan = Channel(Float64).new
 
# run everything in a new top-level fiber to avoid shared memory issues.
# since the fiber immediately sleeps, control is returned to the main code.
# the main code then sleeps for two seconds, returning control to our state_clock fiber.
# when two seconds is up, this state_clock fiber will return control
# to the main code on the next `sleep interval.seconds`
spawn name: "state_clock" do
ai = Integrator.new ->(t : Float64) { Math.sin(Math::PI * t) }, proc_chan, result_chan
end
 
sleep 2.seconds
proc_chan.send({Action::Finished, ->(t : Float64) { 0f64 }})
sleep 0.5.seconds
puts result_chan.receive</syntaxhighlight>
 
Output:
<pre>
-2.5475883655389925e-10
</pre>
 
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight Dlang="d">import core.thread;
import std.datetime;
import std.math;
Line 489 ⟶ 1,020:
v0 = v1;
}
}</langsyntaxhighlight>
 
{{out}}
Line 497 ⟶ 1,028:
{{libheader| System.Classes}}
{{Trans|Python}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Active_object;
 
Line 583 ⟶ 1,114:
Integrator.Join;
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 591 ⟶ 1,122:
=={{header|E}}==
 
<langsyntaxhighlight lang="e">def makeIntegrator() {
var value := 0.0
var input := fn { 0.0 }
Line 641 ⟶ 1,172:
})
return result
}</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
We use the functions (at ..) : scheduling, (wait ...), and (every ...) ot the timer.lib. The accuracy will be function of the browser's functions setTimeout and setInterval ...
<langsyntaxhighlight lang="lisp">
(require 'timer)
Line 676 ⟶ 1,207:
(else (error "active:bad message" message))))))
</syntaxhighlight>
</lang>
{{Out}}
<langsyntaxhighlight lang="lisp">
(define (experiment)
(define (K t) (sin (* PI t )))
Line 698 ⟶ 1,229:
3/7/2015 20:34:28 : result
result 0.00026510586971023164
</syntaxhighlight>
</lang>
 
=={{header|Erlang}}==
I could not see what time to use between each integration so it is the argument to task().
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( active_object ).
-export( [delete/1, input/2, new/0, output/1, task/1] ).
Line 765 ⟶ 1,296:
 
zero( _ ) -> 0.
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
open System.Threading
 
Line 810 ⟶ 1,341:
 
printfn "%f" (i.Output())
i.Stop()</langsyntaxhighlight>
 
=={{header|Factor}}==
Working with dynamic quotations requires the stack effect to be known in advance. The apply-stack-effect serves this purpose.
<langsyntaxhighlight lang="factor">USING: accessors alarms calendar combinators kernel locals math
math.constants math.functions prettyprint system threads ;
IN: rosettacode.active
Line 854 ⟶ 1,385:
0.5 seconds sleep
[ output . ] [ destroy ] bi ;
MAIN: active-test</langsyntaxhighlight>
( scratchpad ) "rosettacode.active" run
-5.294207647335787e-05
Line 860 ⟶ 1,391:
=={{header|FBSL}}==
The Dynamic Assembler and Dynamic C JIT compilers integrated in FBSL v3.5 handle multithreading perfectly well. However, pure FBSL infrastructure has never been designed especially to support own multithreading nor can it handle long long integers natively. Yet a number of tasks with careful design and planning are quite feasible in pure FBSL too:
<langsyntaxhighlight lang="qbasic">#APPTYPE CONSOLE
 
#INCLUDE <Include\Windows.inc>
Line 953 ⟶ 1,484:
FUNCTION Task(BYVAL t AS DOUBLE) AS DOUBLE
RETURN SIN(2 * PI * 0.5 * t)
END FUNCTION</langsyntaxhighlight>
 
'''Typical console output:'''
>>> -0.000769965989580346
Press any key to continue...
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">#define twopi 6.2831853071795864769252867665590057684
dim shared as double S = 0 'set up the state as a global variable
dim shared as double t0, t1, ta
 
function sine( x as double, f as double ) as double
return sin(twopi*f*x)
end function
 
function zero( x as double, f as double ) as double
return 0
end function
 
sub integrate( K as function(as double, as double) as double, f as double )
'represent input as pointers to functions
t1 = timer
s += (K(t1,f) + K(t0,f))*(t1-t0)/2.0
t0 = t1
end sub
 
t0 = timer
ta = timer
 
while timer-ta <= 2.5
if timer-ta <= 2 then integrate( @sine, 0.5 ) else integrate( @zero, 0 )
wend
 
print S
</syntaxhighlight>
{{out}}<pre>
8.926050531860172e-07
</pre>
 
=={{header|Go}}==
Using time.Tick to sample K at a constant frequency. Three goroutines are involved, main, aif, and tk. Aif controls access to the accumulator s and the integration function K. Tk and main must talk to aif through channels to access s and K.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,040 ⟶ 1,604:
time.Sleep(time.Second / 2) // 4. sleep .5 sec
fmt.Println(a.output()) // output should be near zero
}</langsyntaxhighlight>
Output:
<pre>
Line 1,048 ⟶ 1,612:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">/**
* Integrates input function K over time
* S + (t1 - t0) * (K(t1) + K(t0)) / 2
Line 1,117 ⟶ 1,681:
System.out.println(integrator.getOutput())
}
}</langsyntaxhighlight>
{{out}}
<pre>0.0039642136156300455</pre>
Line 1,123 ⟶ 1,687:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">module Integrator (
newIntegrator, input, output, stop,
Time, timeInterval
Line 1,199 ⟶ 1,763:
 
-- Execute 'action' until it returns false.
whileM action = do b <- action; if b then whileM action else return ()</langsyntaxhighlight>
 
=={{header|J}}==
Line 1,205 ⟶ 1,769:
Implementation:
 
<langsyntaxhighlight Jlang="j">coclass 'activeobject'
require'dates'
 
Line 1,228 ⟶ 1,792:
getoutput=:3 :0
Zero+G T''
)</langsyntaxhighlight>
 
Task example (code):
 
<langsyntaxhighlight Jlang="j">cocurrent 'testrig'
 
delay=: 6!:3
Line 1,248 ⟶ 1,812:
delay 0.5
 
smoutput (T__object,getoutput__object) ''</langsyntaxhighlight>
 
Task example (output):
Line 1,258 ⟶ 1,822:
 
First column is time relative to start of processing, second column is object's output at that time.
 
=== Using a task thread ===
 
Variant using an independent task thread:
 
<syntaxhighlight lang=J>delay=: 6!:3
 
task=: {{
obj=. '' conew 'integra'
F__obj=: 1 o. o.
delay 2
F__obj=: 0:
delay 0.5
s=. S__obj
destroy__obj''
s
}}
 
coclass'integra'
reqthreads=: {{ 0&T.@''^:(0>.y-1 T.'')0 }}
time=: 6!:1
F=: 0:
K=: S=: SHUTDOWN=: 0
create=: {{
reqthreads cores=. {.8 T. ''
integrator t. '' T=: time''
}}
destroy=: {{ codestroy '' [ SHUTDOWN=: 1 }}
integrator=: {{
while. -.SHUTDOWN do.
t=. time''
k=. F t
S=: S + (k+K)*t-T
T=: t
K=: k
end.
}}
</syntaxhighlight>
 
This exhibits more timing variance because of the loose coupling of scheduling between threads:
 
<syntaxhighlight lang=J> task''
0.0194745
task''
_4.40316e_15
task''
0.00874017
task''
_0.0159841
</syntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">/**
* Integrates input function K over time
* S + (t1 - t0) * (K(t1) + K(t0)) / 2
Line 1,330 ⟶ 1,944:
}
}
</syntaxhighlight>
</lang>
Output:
<pre>4.783602720556498E-13</pre>
Line 1,338 ⟶ 1,952:
{{trans|E}}
 
<langsyntaxhighlight lang="javascript">function Integrator(sampleIntervalMS) {
var inputF = function () { return 0.0 };
var sum = 0.0;
Line 1,363 ⟶ 1,977:
shutdown: function () { clearInterval(updater) },
});
}</langsyntaxhighlight>
 
Test program as a HTML fragment:
 
<langsyntaxhighlight lang="html4strict"><p><span id="a">Test running...</span> <code id="b">-</code></p>
 
<script type="text/javascript">
Line 1,386 ⟶ 2,000:
}, 2000);
}, 1)
</script></langsyntaxhighlight>
 
=={{header|Julia}}==
Line 1,392 ⟶ 2,006:
Julia has inheritance of data structures and first-class types, but structures do not have methods.
Instead, methods are functions with multiple dispatch based on argument type.
<langsyntaxhighlight lang="julia">mutable struct Integrator
func::Function
runningsum::Float64
Line 1,431 ⟶ 2,045:
sleep(0.5)
v2 = it.runningsum
println("After 2.5 seconds, integrator value was $v2")</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
Athough this is a faithful translation of the Java entry, on my machine the output of the latter is typically an order of magnitude smaller than this version. I have no idea why.
<langsyntaxhighlight lang="scala">// version 1.2.0
 
import kotlin.math.*
Line 1,504 ⟶ 2,118:
integrator.stop()
println(integrator.getOutput())
}</langsyntaxhighlight>
 
Sample output:
Line 1,513 ⟶ 2,127:
=={{header|Lingo}}==
Parent script "Integrator":
<langsyntaxhighlight Lingolang="lingo">property _sum
property _func
property _timeLast
Line 1,553 ⟶ 2,167:
me._timeLast = t
me._valueLast = val
end</langsyntaxhighlight>
 
In some movie script:
<langsyntaxhighlight Lingolang="lingo">global gIntegrator
 
-- entry point
Line 1,574 ⟶ 2,188:
put gIntegrator.output()
timer.forget()
end</langsyntaxhighlight>
 
{{out}}
<pre>-- 0.0004</pre>
 
=={{header|MathematicaLua}}==
Pure/native Lua is not multithreaded, so this task should perhaps be marked "omit from|Lua" if following the implicit ''intent'' of the task. However, the explicit ''wording'' of the task does not seem to require a multithreaded solution. Perhaps this is ''cheating'', but I thought it might interest the reader to see the integrator portion nonetheless, so it is demonstrated using a mock sampling method at various intervals (to ''simulate'' multithreaded updates).
<lang Mathematica>Block[{start = SessionTime[], K, t0 = 0, t1, kt0, S = 0},
<syntaxhighlight lang="lua">local seconds = os.clock
 
local integrator = {
new = function(self, fn)
return setmetatable({fn=fn,t0=seconds(),v0=0,sum=0,nup=0},self)
end,
update = function(self)
self.t1 = seconds()
self.v1 = self.fn(self.t1)
self.sum = self.sum + (self.v0 + self.v1) * (self.t1 - self.t0) / 2
self.t0, self.v0, self.nup = self.t1, self.v1, self.nup+1
end,
input = function(self, fn) self.fn = fn end,
output = function(self) return self.sum end,
}
integrator.__index = integrator
 
-- "fake multithreaded sleep()"
-- waits for "duration" seconds calling "f" at every "interval" seconds
local function sample(duration, interval, f)
local now = seconds()
local untilwhen, nextinterval = now+duration, now+interval
f()
repeat
if seconds() >= nextinterval then f() nextinterval=nextinterval+interval end
until seconds() >= untilwhen
end
 
local pi, sin = math.pi, math.sin
local ks = function(t) return sin(2.0*pi*0.5*t) end
local kz = function(t) return 0 end
local intervals = { 0.5, 0.25, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001 }
for _,interval in ipairs(intervals) do
local i = integrator:new(ks)
sample(2.0, interval, function() i:update() end)
i:input(kz)
sample(0.5, interval, function() i:update() end)
print(string.format("sampling interval: %f, %5d updates over 2.5s total = %.15f", interval, i.nup, i:output()))
end</syntaxhighlight>
{{out}}
<pre>sampling interval: 0.500000, 6 updates over 2.5s total = -0.003628054395752
sampling interval: 0.250000, 11 updates over 2.5s total = 0.003994231784540
sampling interval: 0.100000, 25 updates over 2.5s total = 0.001891527454886
sampling interval: 0.050000, 51 updates over 2.5s total = 0.023521980508657
sampling interval: 0.025000, 101 updates over 2.5s total = -0.000573259909112
sampling interval: 0.010000, 250 updates over 2.5s total = 0.003001745575344
sampling interval: 0.005000, 501 updates over 2.5s total = -0.000415541052666
sampling interval: 0.002500, 999 updates over 2.5s total = -0.001480800340644
sampling interval: 0.001000, 2493 updates over 2.5s total = 0.000362576907805</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Block[{start = SessionTime[], K, t0 = 0, t1, kt0, S = 0},
K[t_] = Sin[2 Pi f t] /. f -> 0.5; kt0 = K[t0];
While[True, t1 = SessionTime[] - start;
S += (kt0 + (kt0 = K[t1])) (t1 - t0)/2; t0 = t1;
If[t1 > 2, K[t_] = 0; If[t1 > 2.5, Break[]]]]; S]</langsyntaxhighlight>
 
1.1309*10^-6
Line 1,595 ⟶ 2,261:
Of course, it is necessary to take some precautions when accessing or updating the shared object. We use a lock for this purpose.
 
<syntaxhighlight lang="text">
# Active object.
# Compile with "nim c --threads:on".
Line 1,709 ⟶ 2,375:
echo "Value after 0.5 more second: ", integrator.output()
integrator.destroy()
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,722 ⟶ 2,388:
Not totally certain this is a correct implementation since the value coming out is not close to zero. It does show all of the basics of multithreading and object synchronization though.
 
<syntaxhighlight lang="oorexx">
<lang ooRexx>
integrater = .integrater~new(.routines~sine) -- start the integrater function
call syssleep 2
Line 1,798 ⟶ 2,464:
::requires rxmath library
 
</syntaxhighlight>
</lang>
 
=={{header|OxygenBasic}}==
Line 1,805 ⟶ 2,471:
 
With a high precision timer the result is around -.0002
<langsyntaxhighlight lang="oxygenbasic">
double MainTime
 
Line 1,977 ⟶ 2,643:
print str(integral,4)
 
Rudolpho.clear</langsyntaxhighlight>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
fun {Const X}
fun {$ _} X end
Line 2,039 ⟶ 2,705:
 
{Show {I output($)}}
{I stop}</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 2,110 ⟶ 2,776:
say "0 after .5 seconds: ", $x->output;
 
$x->delete;</langsyntaxhighlight>
 
=={{header|Phix}}==
===classes===
{{libheader|Phix/Class}}
Needs 0.8.2+
<lang Phix>integer xlock = init_cs()
 
<!--<syntaxhighlight lang="phix">-->
class integrator
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.2"</span><span style="color: #0000FF;">)</span>
--
-- Integrates input function f over time
<span style="color: #004080;">integer</span> <span style="color: #000000;">xlock</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">init_cs</span><span style="color: #0000FF;">()</span>
-- v + (t1 - t0) * (f(t1) + f(t0)) / 2
--
<span style="color: #008080;">class</span> <span style="color: #000000;">integrator</span>
integer f -- function f(atom t); (see note)
<span style="color: #000080;font-style:italic;">--
atom interval, t0, k0 = 0, v = 0
-- Integrates input function f over time
bool running
-- v + (t1 - t0) * (f(t1) + f(t0)) / 2
public integer id
--</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">f</span> <span style="color: #000080;font-style:italic;">-- function f(atom t); (see note)</span>
procedure set_func(integer rid)
<span style="color: #004080;">atom</span> <span style="color: #000000;">interval</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
this.f = rid
<span style="color: #004080;">bool</span> <span style="color: #000000;">running</span>
end procedure
<span style="color: #008080;">public</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">id</span>
procedure update()
<span style="color: #008080;">procedure</span> <span style="color: #000000;">set_func</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">)</span>
enter_cs(xlock)
<span style="color: #008080;">this</span><span style="color: #0000FF;">.</span><span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rid</span>
integer f = this.f -- (nb: no "this")
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
atom t1 = time(),
k1 = f(t1)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">update</span><span style="color: #0000FF;">()</span>
v += (t1 - t0) * (k1 + k0) / 2
<span style="color: #7060A8;">enter_cs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xlock</span><span style="color: #0000FF;">)</span>
t0 = t1
<span style="color: #004080;">integer</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">this</span><span style="color: #0000FF;">.</span><span style="color: #000000;">f</span> <span style="color: #000080;font-style:italic;">-- (nb: no "this")</span>
k0 = k1
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span>
leave_cs(xlock)
<span style="color: #000000;">k1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t1</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #000000;">v</span> <span style="color: #0000FF;">+=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">t1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">k1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">k0</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span>
<span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t1</span>
<span style="color: #000000;">k0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k1</span>
<span style="color: #7060A8;">leave_cs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xlock</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">tick</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">running</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">running</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">interval</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">update</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">stop</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">running</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #000000;">wait_thread</span><span style="color: #0000FF;">(</span><span style="color: #000000;">id</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">get_output</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">v</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">new_integrator</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">interval</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">integrator</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">({</span><span style="color: #000000;">rid</span><span style="color: #0000FF;">,</span><span style="color: #000000;">interval</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()})</span>
<span style="color: #000000;">i</span><span style="color: #0000FF;">.</span><span style="color: #000000;">update</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">i</span><span style="color: #0000FF;">.</span><span style="color: #000000;">id</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">create_thread</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">.</span><span style="color: #000000;">tick</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zero</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000080;font-style:italic;">/*t*/</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sine</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">*</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">*</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #000000;">integrator</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">new_integrator</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sine</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.01</span><span style="color: #0000FF;">);</span>
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">i</span><span style="color: #0000FF;">.</span><span style="color: #000000;">get_output</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">i</span><span style="color: #0000FF;">.</span><span style="color: #000000;">set_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zero</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">i</span><span style="color: #0000FF;">.</span><span style="color: #000000;">stop</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">i</span><span style="color: #0000FF;">.</span><span style="color: #000000;">get_output</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
procedure tick()
running = true
while running do
sleep(interval)
update()
end while
end procedure
 
procedure stop()
running = false
wait_thread(id)
end procedure
 
function get_output()
return v
end function
end class
 
function new_integrator(integer rid, atom interval)
integrator i = new({rid,interval,time()})
i.update()
i.id = create_thread(i.tick,{i})
return i
end function
 
function zero(atom /*t*/) return 0 end function
function sine(atom t) return sin(2*PI*0.5*t) end function
 
integrator i = new_integrator(sine,0.01);
sleep(2)
?i.get_output()
i.set_func(zero)
sleep(0.5)
i.stop()
?i.get_output()</lang>
Note that were f a regular member function of the class, it would get a "this" parameter/argument, which we avoid by stashing it in a local integer prior to the call. Alternatively you could of course use zero/sine functions with an ignored parameter and the usual this.f() syntax [along with the usual "this." being optional inside the class definition].
{{out}}
Line 2,195 ⟶ 2,867:
If anything phix requires more locking that other languages due to the hidden shared reference counts.<br>
Just lock everything, it is not that hard, and you should never need much more than the stuff below.
<lang Phix>sequence x = {}
enum TERMINATE, INTERVAL, KFUN, VALUE, T0, K0, ID, ISIZE=$
integer xlock = init_cs()
 
<!--<syntaxhighlight lang="phix">-->
function zero(atom /*t*/) return 0 end function
<span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
function sine(atom t) return sin(2*PI*0.5*t) end function
<span style="color: #008080;">enum</span> <span style="color: #000000;">TERMINATE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">INTERVAL</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">KFUN</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">VALUE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">T0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">K0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ID</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ISIZE</span><span style="color: #0000FF;">=$</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">xlock</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">init_cs</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zero</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000080;font-style:italic;">/*t*/</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sine</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">*</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">*</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">update</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">enter_cs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xlock</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span>
<span style="color: #000000;">k1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">call_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">KFUN</span><span style="color: #0000FF;">],{</span><span style="color: #000000;">t1</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">VALUE</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">k1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">K0</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">t1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">T0</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">T0</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t1</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">K0</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k1</span>
<span style="color: #7060A8;">leave_cs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xlock</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">tick</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">TERMINATE</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">INTERVAL</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">update</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">new_integrator</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">interval</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ISIZE</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">TERMINATE</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">INTERVAL</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">interval</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">KFUN</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rid</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">T0</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">update</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ID</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">create_thread</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tick</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">dx</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">set_input</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">enter_cs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xlock</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">KFUN</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rid</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">K0</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #7060A8;">leave_cs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xlock</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">get_output</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">enter_cs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xlock</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">VALUE</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">leave_cs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xlock</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">v</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">stop_integrator</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">TERMINATE</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #000000;">wait_thread</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ID</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">new_integrator</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sine</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.01</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%f\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">get_output</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">set_input</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zero</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%f\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">get_output</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">stop_integrator</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
 
procedure update(integer dx)
enter_cs(xlock)
atom t1 = time(),
k1 = call_func(x[dx][KFUN],{t1})
x[dx][VALUE] += (k1 + x[dx][K0]) * (t1 - x[dx][T0]) / 2
x[dx][T0] = t1
x[dx][K0] = k1
leave_cs(xlock)
end procedure
 
procedure tick(integer dx)
while not x[dx][TERMINATE] do
sleep(x[dx][INTERVAL])
update(dx)
end while
end procedure
 
function new_integrator(integer rid, atom interval)
x = append(x,repeat(0,ISIZE))
integer dx = length(x)
x[dx][TERMINATE] = false
x[dx][INTERVAL] = interval
x[dx][KFUN] = rid
x[dx][T0] = time()
update(dx)
x[dx][ID] = create_thread(routine_id("tick"),{dx})
return dx
end function
 
procedure set_input(integer dx, rid)
enter_cs(xlock)
x[dx][KFUN] = rid
x[dx][K0] = 0
leave_cs(xlock)
end procedure
 
function get_output(integer dx)
enter_cs(xlock)
atom v = x[dx][VALUE]
leave_cs(xlock)
return v
end function
 
procedure stop_integrator(integer dx)
x[dx][TERMINATE] = true
wait_thread(x[dx][ID])
end procedure
 
puts(1,"")
integer dx = new_integrator(routine_id("sine"),0.01)
sleep(2)
printf(1,"%f\n",get_output(dx))
set_input(dx,routine_id("zero"))
sleep(0.5)
printf(1,"%f\n",get_output(dx))
stop_integrator(dx)</lang>
{{out}}
<pre>
Line 2,265 ⟶ 2,941:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/math.l")
 
(class +Active)
Line 2,308 ⟶ 2,984:
(wait 500) # Wait 0.5 sec
(prinl "Output: " (output> Obj)) # Print return value
(stop> Obj) ) ) # Stop active object</langsyntaxhighlight>
 
=={{header|PureBasic}}==
Using the open-source precompiler [http://www.development-lounge.de/viewtopic.php?t=5915 SimpleOOP].
<langsyntaxhighlight PureBasiclang="purebasic">Prototype.d ValueFunction(f.d, t.d)
 
Class IntegralClass
Line 2,383 ⟶ 3,059:
Delay( 500) ; Wait 1/2 sec
MessageRequester("Info", StrD(*a\Output())) ; Present the result
*a= FreeObject</langsyntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|3}}
Assignment is thread-safe in Python, so no extra locks are needed in this case.
 
 
<langsyntaxhighlight lang="python">from time import time, sleep
from threading import Thread
 
Line 2,423 ⟶ 3,100:
ai = Integrator(lambda t: sin(pi*t))
sleep(2)
print (ai.S)
ai.K = lambda t: 0
sleep(0.5)
print (ai.S)</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 2,461 ⟶ 3,138:
(sleep/yield 0.5)
(displayln (send active output))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,468 ⟶ 3,145:
There is some jitter in the timer, but it is typically accurate to within a few thousandths of a second.
 
<syntaxhighlight lang="raku" perl6line>class Integrator {
has $.f is rw = sub ($t) { 0 };
has $.now is rw;
Line 2,512 ⟶ 3,189:
sleep .5;
 
say "f(0): ", $a.Output;</langsyntaxhighlight>
 
{{out|Typical output}}
Line 2,521 ⟶ 3,198:
=={{header|Rust}}==
 
<langsyntaxhighlight lang="rust">#![feature(mpsc_select)]
 
extern crate num;
Line 2,692 ⟶ 3,369:
thread::sleep(Duration::from_millis(100));
assert_eq!(object.output() as u32, 0)
}</langsyntaxhighlight>
 
=={{header|Scala}}==
 
<langsyntaxhighlight Scalalang="scala">object ActiveObject {
 
class Integrator {
Line 2,743 ⟶ 3,420:
integrator.bye
}
}</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
 
<syntaxhighlight lang="smalltalk">
<lang Smalltalk>
Object subclass:#Integrator
instanceVariableNames:'tickRate input s thread'
Line 2,809 ⟶ 3,486:
showCR:(i stop).
].
</syntaxhighlight>
</lang>
running:
<syntaxhighlight lang Smalltalk="smalltalk">Integrator example</langsyntaxhighlight>
 
output:
Line 2,834 ⟶ 3,511:
=={{header|SuperCollider}}==
Instead of writing a class, here we just use an environment to encapsulate state.
<syntaxhighlight lang="supercollider">
<lang SuperCollider>
(
a = TaskProxy { |envir|
Line 2,866 ⟶ 3,543:
}
)
</syntaxhighlight>
</lang>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">// For NSObject, NSTimeInterval and NSThread
import Foundation
// For PI and sin
Line 2,934 ⟶ 3,611:
 
println(activeObject.Output())
</syntaxhighlight>
</lang>
Sample output:
<pre>
Line 2,944 ⟶ 3,621:
 
This implementation Tcl 8.6 for object support (for the active integrator object) and coroutine support (for the controller task). It could be rewritten to only use 8.5 plus the TclOO library.
<langsyntaxhighlight Tcllang="tcl">package require Tcl 8.6
oo::class create integrator {
variable e sum delay tBase t0 k0 aid
Line 2,993 ⟶ 3,670:
pause 0.5
puts [format %.15f [i output]]
}</langsyntaxhighlight>
Sample output:
-0.000000168952702
Line 3,001 ⟶ 3,678:
Since this object is CPU intensive, shutting it down when done is crucial. To facilitate this, the IDisposable pattern was used.
 
<langsyntaxhighlight lang="vbnet">Module Module1
Sub Main()
Line 3,074 ⟶ 3,751:
End Sub
End Class</langsyntaxhighlight>
 
 
Output: 0.000241446762282308
 
=={{header|Wren}}==
Wren doesn't have threads but does have fibers which are cooperatively (rather than preemptively) scheduled. Only one fiber can run at a time.
 
However, it is possible to perform asynchronous operations in Wren-cli using a combination of the Scheduler and Timer classes which use the C library ''libuv'' under the hood. The problem is that, due to a bug, the Timer.sleep method doesn't just pause the current fiber, it also pauses the System.clock method! So we can't use the latter to measure time here.
 
What I've done instead is to pre-compute the number of updates performed on my machine for a given function and time period which is a fairly stable figure (though it will obviously be different on other machines). I've then used this figure to measure elapsed time for each update. On average this gives results of around 0.003 seconds which I consider acceptable in the circumstances.
<syntaxhighlight lang="wren">import "scheduler" for Scheduler
import "timer" for Timer
 
var Interval = 0
 
class Integrator {
construct new() {
_sum = 0
}
 
input(k) {
_k = k
_v0 = k.call(0)
_t = 0
_running = true
integrate_()
}
 
output { _sum }
 
stop() {
_running = false
}
 
integrate_() {
while (_running) {
Timer.sleep(1)
update_()
}
}
 
update_() {
_t = _t + Interval
var v1 = _k.call(_t)
var trap = Interval * (_v0 + v1) / 2
_sum = _sum + trap
_v0 = v1
}
}
 
var integrator = Integrator.new()
Scheduler.add {
Interval = 2 / 1550 // machine specific value
integrator.input(Fn.new { |t| (Num.pi * t).sin })
}
Timer.sleep(2000)
 
Scheduler.add {
Interval = 0.5 / 775 // machine specific value
integrator.input(Fn.new { |t| 0 })
}
Timer.sleep(500)
 
integrator.stop()
System.print(integrator.output)</syntaxhighlight>
 
{{out}}
<pre>
0.0028437802254386
</pre>
 
=={{header|zkl}}==
{{trans|Python}}
Uses cheese ball thread safety: since the integrator runs continuously and I don't want to queue the output, just sample it, strong references are used as they change atomically.
<langsyntaxhighlight lang="zkl">class Integrator{
// continuously integrate a function `K`, at each `interval` seconds'
fcn init(f,interval=1e-4){
Line 3,100 ⟶ 3,844:
fcn sample { S.value }
fcn setF(f) { K.set(f) }
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">ai:=Integrator(fcn(t){ ((0.0).pi*t).sin() });
Atomic.sleep(2);
ai.sample().println();
Line 3,107 ⟶ 3,851:
ai.setF(fcn{ 0 });
Atomic.sleep(0.5);
ai.sample().println();</langsyntaxhighlight>
{{out}}
<pre>
Line 3,113 ⟶ 3,857:
1.11571e-07
</pre>
{{omit from|6502 Assembly|Not an object-oriented language.}}
 
{{omit from|8080 Assembly}}
{{omit from|8086 Assembly}}
{{omit from|68000 Assembly}}
{{omit from|ACL2}}
{{omit from|ARM Assembly}}
{{omit from|AWK}}
{{omit from|gnuplot}}
Line 3,120 ⟶ 3,868:
{{omit from|LaTeX}}
{{omit from|Locomotive Basic}}
{{omit from|Make}}
{{omit from|Metafont}}
{{omit from|M4}}
{{omit from|Make}}
{{omit from|Maxima}}
{{omit from|Metafont}}
{{omit from|ML/I}}
{{omit from|Octave}}
{{omit from|PlainTeX}}
{{omit from|TI-89 BASIC}} <!-- Does not have concurrency or background processes. -->
{{omit from|Retro}}
{{omit from|TI-89 BASIC}} <!-- Does not have concurrency or background processes. -->
{{omit from|UNIX Shell}}
{{omit from|VBScript}}
{{omit from|x86 Assembly}}
{{omit from|Z80 Assembly}}
{{omit from|ZX Spectrum Basic}}
9,476

edits