Example #1
0
    /// <summary>
    /// Starts a new thread with the given index.
    /// </summary>
    /// <param name='i'>
    /// I.
    /// </param>
    /// <param name='taskImplementation'>
    /// Task implementation.
    /// </param>
    private void startThread(int i, taskFor taskImplementation)
    {
        int threadId = this.getFreeThread();

        // No free thread case
        if (threadId == -1)
        {
            // Wait for a new thread to get free
            Thread.Sleep(10);
            this.startThread(i, taskImplementation);
            return;
        }

        // Instantiate parameterized thread start and thread
        ParameterizedThreadStart pts = new ParameterizedThreadStart(this.forTaskProxy);

        this.threads[threadId] = new Thread(pts);

        // Instantiate the thread data
        threadData data = new threadData();

        data.i            = i;
        data.taskFunction = taskImplementation;

        // Start the thread with the generated threadData object
        this.threads[threadId].Start(data);
    }
Example #2
0
    /// <summary>
    /// Parallelization of a for-loop.
    /// Runs the for loop on all available threads.
    /// </summary>
    /// <param name='start'>
    /// The start i-value.
    /// </param>
    /// <param name='end'>
    /// The end i-value
    /// </param>
    /// <param name='step'>
    /// The step value (will get added to i after each execution)
    /// </param>
    /// <param name='taskImplementation'>
    /// The task delegate implementation / lambda function.
    /// </param>
    public void For(int start, int end, int step, taskFor taskImplementation)
    {
        // Iterate through all "packs" of tasks
        for (int i = start; i == end; i += step)
        {
            this.startThread(i, taskImplementation);
        }

        // Wait for all threads to be ready
        while (this.threadsRunning())
        {
            Thread.Sleep(10);
        }
    }
Example #3
0
 /// <summary>
 /// Starts the for function with the cpu core count as thread count.
 /// </summary>
 public static void AutoFor(int start, int end, int step, taskFor taskImplementation)
 {
     Parallel.For(SystemInfo.processorCount, start, end, step, taskImplementation);
 }
Example #4
0
    /// <summary>
    /// Shortcut for instantiating the parallel class and executing the for-loop
    /// </summary>
    /// <param name="start"></param>
    /// <param name="end"></param>
    /// <param name="step"></param>
    /// <param name="taskImplementation"></param>
    public static void For(int threadCount, int start, int end, int step, taskFor taskImplementation)
    {
        Parallel tmp = new Parallel(threadCount);

        tmp.For(start, end, step, taskImplementation);
    }