Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

Threading paradigms available under the .NET CLR, using C#

, 22 Dec 2004
This article discusses the various threading paradigms available under the .NET CLR, using C#.
managedthreadcs-async.zip
ManagedThreadCS-Async.sln.old
App.ico
ManagedThreadCS-Async.csproj.user
managedthreadcs-pool.zip
App.ico
ManagedThreadCS-Pool.csproj.user
managedthreadcs-pool2.zip
App.ico
ManagedThreadCS-Pool.csproj.user
managedthreadcs.zip
App.ico
ManagedThreadCS.csproj.user
using System;
using System.Collections;
using System.IO;

namespace ManagedThreadCS
{
	/// <summary>
	/// Summary description for CGSRMgr.
	/// </summary>
	public class CGSRMgr
	{
	 private int          _nThreads;
	 private ArrayList    _vecObligor;
	 private ArrayList [] _vecSubDivObligor;
	 private ArrayList    _vecThread;

  public CGSRMgr(int nThreads)
   {
    _nThreads = nThreads;

    // create vector for complete obligor list
    _vecObligor = new ArrayList();

    // create vector which will hold each thread reference
    _vecThread = new ArrayList();

    // create vector for subdivided obligor list
    _vecSubDivObligor = new ArrayList[_nThreads];
   }

  ///////////////////////////////////////////////////////////////////////////////
  //  Function    : BuildSubdivisionList()
  //  Description : This function takes an list of integers and
  //                subdivides it into a jagged array 
  ///               based upon the number of subdivisions requested in the 
  //                input argument.
  //
  // Modification History
  //  May xx, 2004  gjk  initial design
  ///////////////////////////////////////////////////////////////////////////////
  //
	 private void BuildSubdivisionList(int subdivs)
		{
		 int num = _vecObligor.Count / subdivs;
		 int rem = _vecObligor.Count % subdivs;
		 int cnt = num;

   // clear jagged array
   //if (_vecSubDivObligor.Length > 0)
   // for (int i=0; i<_vecSubDivObligor.Rank; i++)
   //  _vecSubDivObligor[i].Clear();

		 // set aside space for all subdivisions
		 //_vecSubDivObligor = new ArrayList[subdivs];

		 // walk through and fill all subdivisions
		 int itOb = 0;
		 for (int j=0; j<subdivs; j++)
		  {
     // if at the last division, pick up the remainder
     if (subdivs == j+1)
       cnt = num + rem;
    
     // adjust index into source array
     itOb = (j*num);

     // create jagged entry to accommodate cnt elements
     _vecSubDivObligor[j] = new ArrayList(cnt);

     // add elements
		   for (int k=0; k<cnt; k++)
       _vecSubDivObligor[j].Add(_vecObligor[itOb+k]);
		  }
		}

  ///////////////////////////////////////////////////////////////////////////////
  //  Function    : ProcessObligorList()
  //  Description : This function creates a number of threads via the exposed
  //                delegate. Each thread will in turn operate upon its own
  //                set of integers.
  //
  // Modification History
  //  May xx, 2004  gjk  initial design
  ///////////////////////////////////////////////////////////////////////////////
  //
		public void ProcessObligorList()
		{
   // build subdivision list
		 BuildSubdivisionList(_nThreads);

   // if we have data, start your engines!
   if (_vecSubDivObligor.Length != 0)
    {
     // apportion data among worker threads
     foreach(ArrayList x in _vecSubDivObligor)
     {
      // get obligor set for each thread
      // func goes here.
      CGSRMgrThread t = new CGSRMgrThread(x);
      _vecThread.Add(t);
     }

    // wait for all threads to complete
    foreach (CGSRMgrThread t in _vecThread)
     t.GetMRE().WaitOne();

    }
		}

  ///////////////////////////////////////////////////////////////////////////////
  //  Function    : StreamToFile()
  //  Description : This function currently stands as a debug function for 
  //                streaming, to the file system, various details for all 
  //                records under the manager's control.
  //
  // Modification History
  //  May xx, 2004  gjk  initial design
  ///////////////////////////////////////////////////////////////////////////////
  //
  public void StreamToFile(String fname)
  {
   if (File.Exists(fname))
    File.Delete(fname);

   StreamWriter sw = File.CreateText(fname);
   foreach (CGSRMgrThread t in _vecThread)
   {
    ArrayList x = t.GetGSRs();
    foreach (CGSR g in x)
    {
     sw.WriteLine("{0} {1}", g.GetObligor(),g.GetSymbol());    
    }
   }
   sw.Close();
  }

  ///////////////////////////////////////////////////////////////////////////////
  //  Function    : GetTotalGSRCount()
  //  Description : This function determines the total number of records
  //                processed across all threads control by this manager object
  //
  // Modification History
  //  May xx, 2004  gjk  initial design
  ///////////////////////////////////////////////////////////////////////////////
  //
  public int GetTotalGSRCount()
  {
   int total = 0;
   
   foreach (CGSRMgrThread t in _vecThread)
   {
    total += t.GetGSRs().Count;
   }

   return total;
 }
  
		public void SetObligorList(ArrayList obList)
		{
		 _vecObligor = obList;
		}
  
  protected CGSRMgr() {;}
	}
 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Gary J. Kuehn
Engineer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 23 Dec 2004
Article Copyright 2004 by Gary J. Kuehn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid