Threads[Task]
Continue
create child tasks
Calling Sequence
Parameters
Description
Examples
Continue(fcn, arg1, ..., argN )
fcn
-
(appliable) the function to execute as the continuation task
args1..argsN
(anything) an argument to fcn or the specification for a child task
The Continue function creates zero or more child tasks and a continuation task.
The Continue function is part of the Task Programming Model.
Continue behaves much like the Start function, with two important differences. First Continue does not wait for the tasks it creates to complete, instead Continue returns as soon as the tasks are created. Secondly, Continue may only be called from within a invocation of the Task Model. The Continue function is intended to spread work across multiple tasks as opposed to calculate a particular result.
If child tasks are specified, then Continue creates one task for each child, and a continuation task with fnc as the continuation function. The value of fnc is either a procedure or the name of a predefined continuation function.
The two predefined continuation functions are passed and null
passed returns the arguments that were passed into it.
null returns NULL.
An argument to Continue of the form
Task = [ cfcn, carg1 ... cargn ]
creates a child task that executes the following command.
cfcn( carg1 ... cargn )
An argument to Continue of the form:
Tasks = [ cfcn, [c1args] ... [cnargs] ]
behaves as if multiple Task arguments of the form
Task = [ cfcn, c1args ], Task = [ cfcn, c2args ], ... Task = [ cfcn, cnargs ]
had been given. Thus, it creates tasks to execute the following commands.
cfcn( c1args ), cfcn( c2args ) ... cfcn( cnargs )
The return value of a child task is passed to the continuation function in the child's position in its argument sequence.
Arguments not of the form described above are passed to the continuation function fcn as arguments in the corresponding positions.
If child tasks are created as part of the call to Continue, the continuation task will not start executing until all of its child tasks have completed.
Solve the N Queens problem: place N queens on an N X N chess board so that no queen can capture any other queen.
nQueens := module() local checkBoard, completeBoardAndCheck, search, continuation, subInit; export ModuleApply; checkBoard := proc( n, board::Array ) local i, j, index; for i from 1 to n-1 do index := board[i]+1; for j from i+1 to n while index <= n do if ( index = board[j] ) then return NULL; end; index := index + 1; end do; index := board[i] - 1; for j from i+1 to n while index >= 0 do if ( index = board[j] ) then return NULL; end; index := index - 1; end do; end do; return Array( board ); end proc; completeBoardAndCheck := proc( n, board, i, unused ) local j; if ( i < n ) then return op( map( proc( j ) board[i] := j; completeBoardAndCheck( n, board, i+1, unused minus {j} ) end proc, unused ) ); else board[n] := unused[1]; return checkBoard( n, board ); end if; end proc; continuation := proc() args; end proc; search := proc( i::posint, n::posint, m::nonnegint, board::Array ) local j, k, boards, a, used, unused; if ( i <= m ) then if ( i > 1 ) then used := convert( board[1..i-1], set ); boards := [ seq( Array( board ), k=1..n-i+1 ) ]; k := 1; for j from 1 to n do if ( not j in used ) then boards[k][i] := j; k := k+1; end if; end do; else boards := [ seq( Array( board ), k=1..n ) ]; for j from 1 to n do boards[j][i] := j; end do; end if; Threads:-Task:-Continue( continuation, Tasks = [ search, seq( [i+1, n, m, j], j in boards ) ] ); else unused := { seq( 1..n ) } minus convert( board[1..i-1], set ); return completeBoardAndCheck( n, board, i, unused ); end if; return NULL; end proc; ModuleApply := proc( n::posint, m::nonnegint ) local board; board := Array( 1..n, datatype=integer[8] ); Threads:-Task:-Start( search, 1, n, m, board ); end proc; end;
nQueens ≔ modulelocalcheckBoard,completeBoardAndCheck,search,continuation,subInit;exportModuleApply;end module
boards≔nQueens⁡1,0
boards≔1
nops⁡boards
1
boards≔nQueens⁡2,1
boards≔
0
boards≔nQueens⁡3,1
boards≔nQueens⁡4,2
boards≔2413,3142
2
boards≔nQueens⁡5,3
boards≔13524,14253,24135,25314,31425,35241,41352,42531,52413,53142
10
boards≔nQueens⁡6,4
boards≔246135,362514,415263,531642
4
boards≔nQueens⁡7,4:
40
boards≔nQueens⁡8,4:
92
See Also
examples,Task
Start
Task
Task Programming Model
Download Help Document