 Threads[Task] - Maple Programming Help

 Return
 return a value from with the Task Model

 Calling Sequence Return( value )

Parameters

 value - (anything) the value to be returned.

Description

 • The Return function allows users to implement an early bailout from the Task Model.
 • The Return function causes the Task Model to stop executing tasks from the current invocation and to return value from Start.  Return does not terminate the execution of the current task.
 • The Return function is part of the Task Programming Model.
 • As the Task Model is inherently multi-threaded, there is a potential race condition if multiple tasks call Return with different values.  The value passed to the first call of Return will be returned from Start and Return will return true.  Subsequent calls to Return in that invocation of the Task Model will return false.
 • Calling Return does not interrupt currently executing Tasks.  It stops new tasks from being started.

Examples

This example solves the N Queens problem, returning the first valid board layout found.

 > nQueens := module()    local completeBoardAndCheck,            search,            continuation,            subInit;    export checkBoard,            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 false;                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 false;                end;                index := index - 1;            end do;        end do;        return true;    end proc;    completeBoardAndCheck := proc( n, board, i, unused )        local j;        if ( i < n ) then            return andmap( proc( j )                            board[i] := j;                            completeBoardAndCheck( n, board, i+1,                                unused minus {j} )                        end proc, unused );        else            board[n] := unused;            if ( checkBoard( n, board ) ) then                Threads:-Task:-Return( convert( board, 'list' ) );                return false            end;        end if;        return true;    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( passed,                Tasks = [ search, seq( [i+1, n, m, j], j in boards ) ] );        else            unused := { seq( j, j=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 );        Threads:-Task:-Start( search, 1, n, m, board );    end proc; end:
 > nQueens( 4, 2 );
 $\left[{3}{,}{1}{,}{4}{,}{2}\right]$ (1)
 > nQueens( 5, 3 );
 $\left[{5}{,}{3}{,}{1}{,}{4}{,}{2}\right]$ (2)
 > nQueens( 6, 4 );
 $\left[{2}{,}{4}{,}{6}{,}{1}{,}{3}{,}{5}\right]$ (3)
 > nQueens( 7, 4 );
 $\left[{7}{,}{5}{,}{3}{,}{1}{,}{6}{,}{4}{,}{2}\right]$ (4)
 > nQueens( 8, 4 );
 $\left[{2}{,}{8}{,}{6}{,}{1}{,}{3}{,}{5}{,}{7}{,}{4}\right]$ (5)
 > nQueens( 9, 4 );
 $\left[{3}{,}{9}{,}{6}{,}{8}{,}{2}{,}{4}{,}{1}{,}{7}{,}{5}\right]$ (6)
 > nQueens( 10, 4 );
 $\left[{3}{,}{10}{,}{8}{,}{5}{,}{2}{,}{4}{,}{1}{,}{7}{,}{9}{,}{6}\right]$ (7)
 > nQueens( 11, 4 );
 $\left[{2}{,}{11}{,}{9}{,}{7}{,}{5}{,}{3}{,}{1}{,}{10}{,}{8}{,}{6}{,}{4}\right]$ (8)
 > nQueens( 12, 4 );
 $\left[{2}{,}{12}{,}{10}{,}{8}{,}{3}{,}{5}{,}{9}{,}{11}{,}{1}{,}{7}{,}{4}{,}{6}\right]$ (9)