How to resolve the algorithm Atomic updates step by step in the FutureBasic programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Atomic updates step by step in the FutureBasic programming language
Table of Contents
Problem Statement
Define a data type consisting of a fixed number of 'buckets', each containing a nonnegative integer value, which supports operations to: In order to exercise this data type, create one set of buckets, and start three concurrent tasks:
The display task need not be explicit; use of e.g. a debugger or trace tool is acceptable provided it is simple to set up to provide the display. This task is intended as an exercise in atomic operations. The sum of the bucket values must be preserved even if the two tasks attempt to perform transfers simultaneously, and a straightforward solution is to ensure that at any time, only one transfer is actually occurring — that the transfer operation is atomic.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Atomic updates step by step in the FutureBasic programming language
Source code in the futurebasic programming language
local fn PopulateArrayWithRandomNumbers
NSUInteger i
for i = 0 to 9
mda (i) = rnd(90)
next
end fn
local fn Display( title as CFStringRef ) as NSUInteger
NSUInteger i, worth = 0
CFStringRef comma = @","
printf @"%@ [\b", title
for i = 0 to 9
worth += mda_integer (i)
if i == 9 then comma = @""
printf @"%2lu%@\b", mda_integer (i), comma
next
printf @"] Sum = %lu", worth
end fn = worth
local fn Flatten( f as NSUInteger )
NSUInteger i, f1 = int((f / 10) + .5 ), f2 = 0, temp
for i = 0 to 9
mda (i) = f1
f2 += f1
next
temp = mda_integer (9)
mda (9) = temp + f - f2
end fn
local fn Transfer( a1 as NSUInteger, a2 as NSUInteger )
NSUInteger t, temp = int( rnd( mda_integer ( a1 ) ) )
t = mda_integer ( a1 ) : mda ( a1 ) = t -temp
t = mda_integer ( a2 ) : mda ( a2 ) = t +temp
end fn
NSUInteger a, i
random
fn PopulateArrayWithRandomNumbers
a = fn Display( @" Initial array:" )
fn Flatten( a )
a = fn Display( @" Current values:" )
fn Transfer( 3, 5 )
fn Display( @" 19 from 3 to 5:" )
HandleEvents
You may also check:How to resolve the algorithm Sort disjoint sublist step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the PL/I programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the BASIC256 programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Action! programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the LSE64 programming language