\section{THREADSTART Start a New Thread Computation}

\subsection{Usage}

The \verb|threadstart| function starts a new computation on a
FreeMat thread, and you must provide a function (no scripts 
are allowed) to run inside the thread, pass any parameters that
the thread function requires, as well as the number of output
arguments expected.  The general syntax for the 
\verb|threadstart| function is
\begin{verbatim}
   threadstart(threadid,function,nargout,arg1,arg2,...)
\end{verbatim}
where \verb|threadid| is a thread handle (returned by \verb|threadnew|),
where \verb|function| is a valid function name (it can be a built-in
imported or M-function), \verb|nargout| is the number of output arguments
expected from the function, and \verb|arg1| is the first argument that
is passed to the function.  Because the function runs in its 
own thread, the return values of the function are not available
imediately.  Instead, execution of that function will continue
in parallel with the current thread.  To retrieve the output
of the thread function, you must wait for the thread to complete
using the \verb|threadwait| function, and then call \verb|threadvalue|
 to retrieve the result.  You can also stop the running thread
prematurely by using the \verb|threadkill| function.  It is important
to call \verb|threadfree| on the handle you get from \verb|threadnew|
when you are finished with the thread to ensure that the resoures
are properly freed.  

It is also perfectly reasonable to use a single thread multiple
times, calling \verb|threadstart| and \verb|threadreturn| multiple times
on a single thread.  The context is preserved between threads.
When calling \verb|threadstart| on a pre-existing thread, FreeMat
will attempt to wait on the thread.  If the wait fails, then
an error will occur.

Some additional important information.  Thread functions operate
in their own context or workspace, which means that data cannot
be shared between threads.  The exception is \verb|global| variables,
which provide a thread-safe way for multiple threads to share data.
Accesses to global variables are serialized so that they can 
be used to share data.  Threads and FreeMat are a new feature, so
there is room for improvement in the API and behavior.  The best
way to improve threads is to experiment with them, and send feedback.

\subsection{Example}

Here we do something very simple.  We want to obtain a listing of
all files on the system, but do not want the results to stop our
computation.  So we run the \verb|system| call in a thread.
\begin{verbatim}
--> a = threadnew;                         % Create the thread
--> threadstart(a,'system',1,'ls -lrt /'); % Start the thread
--> b = rand(100)\rand(100,1);             % Solve some equations simultaneously
--> c = threadvalue(a);                    % Retrieve the file list
--> size(c)                                % It is large!
ans = 
  <uint32>  - size: [1 2]
 
Columns 1 to 2
 22   1  
--> threadfree(a);

\end{verbatim}
The possibilities for threads are significant.  For example,
we can solve equations in parallel, or take Fast Fourier Transforms
on multiple threads.  On multi-processor machines or multicore CPUs,
these threaded calculations will execute in parallel.  Neat.

The reason for the  \verb|nargout| argument is best illustrated with
an example.  Suppose we want to compute the Singular Value 
Decomposition \verb|svd| of a matrix \verb|A| in a thread.  
The documentation for the \verb|svd| function tells us that
the behavior depends on the number of output arguments we request.
For example, if we want a full decomposition, including the left 
and right singular vectors, and a diagonal singular matrix, we
need to use the three-output syntax, instead of the single output
syntax (which returns only the singular values in a column vector):
\begin{verbatim}
--> A = float(rand(4))
A = 
  <float>  - size: [4 4]
 
Columns 1 to 4
 0.146389037  0.971821547  0.504965425  0.706567705  
 0.813649774  0.218331441  0.143571243  0.520508647  
 0.703605354  0.355683208  0.450385213  0.572325826  
 0.073398657  0.093707494  0.946649253  0.856120169  
--> [u,s,v] = svd(A)    % Compute the full decomposition
u = 
  <float>  - size: [4 4]
 
Columns 1 to 4
 -0.56720954   0.25243336   0.77542746   0.11516613  
 -0.39022702  -0.67686397  -0.15489662   0.60463637  
 -0.49008527  -0.38197401  -0.11912314  -0.77441716  
 -0.53461456   0.57639259  -0.60044074   0.14638893  
s = 
  <float>  - size: [4 4]
 
Columns 1 to 4
 2.07391810  0.00000000  0.00000000  0.00000000  
 0.00000000  0.84941119  0.00000000  0.00000000  
 0.00000000  0.00000000  0.69469124  0.00000000  
 0.00000000  0.00000000  0.00000000  0.10644028  
v = 
  <float>  - size: [4 4]
 
Columns 1 to 4
 -0.378321618  -0.871461749  -0.202111065  -0.237861603  
 -0.415077925   0.018471666   0.894098341  -0.167204514  
 -0.515577793   0.475503623  -0.363805771  -0.612960875  
 -0.647118688   0.118782789  -0.165483192   0.734672248  
--> sigmas = svd(A)     % Only want the singular values
sigmas = 
  <float>  - size: [4 1]
 
Columns 1 to 1
 2.07391834  
 0.84941107  
 0.69469130  
 0.10644029  

\end{verbatim}

Normally, FreeMat uses the left hand side of an assignment to calculate
the number of outputs for the function.  When running a function in a
thread, we separate the assignment of the output from the invokation
of the function.  Hence, we have to provide the number of arguments at the
time we invoke the function.  For example, to compute a full decomposition
in a thread, we specify that we want 3 output arguments:
\begin{verbatim}
--> a = threadnew;               % Create the thread
--> threadstart(a,'svd',3,A);    % Start a full decomposition 
--> [u1,s1,v1] = threadvalue(a); % Retrieve the function values
--> threadfree(a);

\end{verbatim}
If we want to compute just the singular values, we start the thread
function with only one output argument:
\begin{verbatim}
--> a = threadnew;
--> threadstart(a,'svd',1,A);
--> sigmas = threadvalue(a);
--> threadfree(a)

\end{verbatim}

