Heres a translation of the article code to cobra
"""
ParallelFor in cobra from <!-- m --><a class="postlink" href="http://coding-time.blogspot.com/2008/03/implement-your-own-parallelfor-in-c.html">http://coding-time.blogspot.com/2008/03 ... -in-c.html</a><!-- m -->
"""
use System.Threading
sig ForDelegate(i as int)
sig ThreadDelegate
class Parallel
"""
Parallel for loop. Invokes given action, passing arguments
fromInclusive - toExclusive on multiple threads.
Returns when loop finished.
"""
def for(fromInclusive as int, toExclusive as int, action as ForDelegate) is shared
"""
ChunkSize = 1 makes items to be processed in order.
Bigger chunk size should reduce lock waiting time and thus increase paralelism.
"""
chunkSize = 4 # number of process() threads
threadCount = Environment.processorCount
cnt = fromInclusive - chunkSize
# processing function
# takes next chunk and processes it using action
process as ThreadDelegate = do
while true
cntMem = 0
lock Parallel.getType
# take next chunk
cnt += chunkSize
cntMem = cnt
# process chunk
# here items can come out of order if chunkSize > 1
for i in cntMem : cntMem + chunkSize
if i >= toExclusive, break
action(i)
# launch process() threads
asyncResults = IAsyncResult[](threadCount)
for i in 0 : threadCount
asyncResults[i] = process.beginInvoke(nil, nil) # line 45
# wait for all threads to complete
for i in 0 : threadCount
process.endInvoke(asyncResults[i]) # line 49
def main is shared
Parallel.for(0, 100, do(i as int))
# Your parallel code
Thread.sleep(100)
Console.writeLine(i)
The only problem is it fails to compile
- Code: Select all
parallelFor.cobra(45): error: Cannot find a definition for "beginInvoke" in "process" whose type is "ThreadDelegate". There is a member named ".invoke" with a similar name.
parallelFor.cobra(49): error: Cannot find a definition for "endInvoke" in "process" whose type is "ThreadDelegate". There is a member named ".invoke" with a similar name.
Compilation failed - 2 errors, 0 warnings
I think I've got a correct translation ( but I could be wrong about that)
skim reading delegates seems to indicate .beginInvoke and .endInvoke are added by the Compiler/CLR.
Is it possible/likely that cobra is not aware of that?