## THREADSTART Start a New Thread Computation

Section: FreeMat Threads

### Usage

The `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 `threadstart` function is

       threadstart(threadid,function,nargout,arg1,arg2,...)

where `threadid` is a thread handle (returned by `threadnew`), where `function` is a valid function name (it can be a built-in imported or M-function), `nargout` is the number of output arguments expected from the function, and `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 `threadwait` function, and then call `threadvalue` to retrieve the result. You can also stop the running thread prematurely by using the `threadkill` function. It is important to call `threadfree` on the handle you get from `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 `threadstart` and `threadreturn` multiple times on a single thread. The context is preserved between threads. When calling `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 `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.

### 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 `system` call in a thread.

    --> a = threadnew;                         % Create the thread
    --> threadstart(a,'system',1,'ls -lrt /'); % Start the thread
    --> b = rand(100)
    and(100,1);             % Solve some equations simultaneously
    --> c = threadvalue(a);                    % Retrieve the file list
    --> size(c)                                % It is large!

    ans =

     22  1

    --> threadfree(a);
    -->
    quit

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 `nargout` argument is best illustrated with an example. Suppose we want to compute the Singular Value Decomposition `svd` of a matrix `A` in a thread. The documentation for the `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):

    --> A = float(rand(4))

    A =

        0.1464    0.9718    0.5050    0.7066
        0.8136    0.2183    0.1436    0.5205
        0.7036    0.3557    0.4504    0.5723
        0.0734    0.0937    0.9466    0.8561

    --> [u,s,v] = svd(A)    % Compute the full decomposition
    u =

       -0.5672    0.2524    0.7754    0.1152
       -0.3902   -0.6769   -0.1549    0.6046
       -0.4901   -0.3820   -0.1191   -0.7744
       -0.5346    0.5764   -0.6004    0.1464

    s =

        2.0739         0         0         0
             0    0.8494         0         0
             0         0    0.6947         0
             0         0         0    0.1064

    v =

       -0.3783   -0.8715   -0.2021   -0.2379
       -0.4151    0.0185    0.8941   -0.1672
       -0.5156    0.4755   -0.3638   -0.6130
       -0.6471    0.1188   -0.1655    0.7347

    --> sigmas = svd(A)     % Only want the singular values

    sigmas =

        2.0739
        0.8494
        0.6947
        0.1064

    -->
    quit

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:

    --> 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);
    -->
    quit

If we want to compute just the singular values, we start the thread function with only one output argument:

    --> a = threadnew;
    --> threadstart(a,'svd',1,A);
    --> sigmas = threadvalue(a);
    --> threadfree(a)
    -->
    quit