| | |
c# multithreading help needed ---urgent
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2006
Posts: 40
Reputation:
Solved Threads: 2
I have written a program in c# for quick sort using multithreading. but the code has a problem. the array is not sorted properly. i think it is because the parent thread terminates soon before the child threads sort the array. i am attaching the code herewith. kindly help me.any help is highly appreciated. thanks in advance.
Beulah
Beulah
C# Syntax (Toggle Plain Text)
using System; using System.Threading; using System.Collections; namespace QSort { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { Class1 class1 = new Class1(); } //public int[] a = new int[10000000]; //added private const int MAX=6; public int[] a = new int[MAX]; public Class1() { int choice=1; while (choice<3) { Random random = new Random(); /*for(Int32 i=0;i<10000000;++i) a[i] = random.Next(100000000);*/ //added a[0]=20; a[1]=10; a[2]=40; a[3]=30; a[4]=60; a[5]=50; //}added ArrayList list = new ArrayList(); list.Add(0); list.Add(a.Length-1); DateTime startTime = DateTime.Now; Console.Write("1. SigleThreaded Sort\n2.MultithreadSort\nEnter Choice : "); choice=Convert.ToInt32(Console.ReadLine()); switch(choice) { case 1: startTime = DateTime.Now; QSort(0, a.Length-1); break; case 2: //multithreading startTime = DateTime.Now; QSort(list); break; default: break; } if (choice<3) { Console.WriteLine("Time Taken to sort "+a.Length.ToString() +" elements = "+(DateTime.Now.Subtract(startTime)).TotalMilliseconds.ToString()+" MilliSeconds"); //added for (int index=0; index<MAX; index++) Console.WriteLine(a[index]); } } } //this works fine public void QSort( int left, int right )//singlethreaded { int pivot, lhold, rhold; lhold = left; rhold = right; pivot = a[left]; while( left < right ) { while( (a[right] >= pivot) && (left < right) ) { right--; } if( left != right ) { a[left] = a[right]; left++; } while( (a[left] <= pivot) && (left < right) ) { left++; } if( left != right ) { a[right] = a[left]; right--; } } a[left] = pivot; pivot = left; left = lhold; right = rhold; if( left < pivot ) { QSort( left, pivot-1 ); } if( right > pivot ) { QSort( pivot+1, right ); } } //i have problem in this method public void QSort(object listObject) //Multithreaded { Int32 left = (Int32)((ArrayList)listObject)[0]; Int32 right = (Int32)((ArrayList)listObject)[1]; int pivot, lhold, rhold; lhold = left; rhold = right; pivot = a[left]; lock(a) { while( left < right ) { while( (a[right] >= pivot) && (left < right) ) { right--; } if( left != right ) { a[left] = a[right]; left++; } while( (a[left] <= pivot) && (left < right) ) { left++; } if( left != right ) { a[right] = a[left]; right--; } } a[left] = pivot; pivot = left; left = lhold; right = rhold; } if( left < pivot ) { ArrayList list = new ArrayList(); list.Add(left); list.Add(pivot-1); ThreadPool.QueueUserWorkItem(new WaitCallback(QSort),list); } if( right > pivot ) { ArrayList list = new ArrayList(); list.Add(pivot+1); list.Add(right); ThreadPool.QueueUserWorkItem(new WaitCallback(QSort),list); } } } }
I by no means am an expert in threading but here is what i can gather from this.
you are using 'ThreadPool.QueueUserWorkItem' which queues the item into the current main thread.
What you need to do is create a whole new thread which i did not see.
which is something like:
that creates a seperate thread from the main with a method to initialze it.
the locks go in functions that are shared by more then one thread, which could be a single function to change data in the array then a seperate function for sorting.
you are using 'ThreadPool.QueueUserWorkItem' which queues the item into the current main thread.
What you need to do is create a whole new thread which i did not see.
which is something like:
C# Syntax (Toggle Plain Text)
myThread = new System.Threading.Thread(new System.Threading.ThreadStart(myStartingMethod)); myThread.Start();
that creates a seperate thread from the main with a method to initialze it.
the locks go in functions that are shared by more then one thread, which could be a single function to change data in the array then a seperate function for sorting.
![]() |
Similar Threads
- asp.net help needed URGENT (ASP.NET)
- Help Needed in C. Urgent (C)
- Ethernet Card HELP NEEDED (URGENT) (PCI and Add-In Cards)
Other Threads in the C# Forum
- Previous Thread: Looking for a code for editing XML
- Next Thread: c# code for login and checking password
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion csharp custom data database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox serialization server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update upload usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml





