c# multithreading help needed ---urgent

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2006
Posts: 40
Reputation: beuls is an unknown quantity at this point 
Solved Threads: 2
beuls beuls is offline Offline
Light Poster

c# multithreading help needed ---urgent

 
1
  #1
Aug 30th, 2006
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

  1. using System;
  2. using System.Threading;
  3. using System.Collections;
  4.  
  5. namespace QSort
  6. {
  7. /// <summary>
  8. /// Summary description for Class1.
  9. /// </summary>
  10. class Class1
  11. {
  12. /// <summary>
  13. /// The main entry point for the application.
  14. /// </summary>
  15. [STAThread]
  16. static void Main(string[] args)
  17. {
  18. Class1 class1 = new Class1();
  19. }
  20.  
  21.  
  22. //public int[] a = new int[10000000];
  23. //added
  24. private const int MAX=6;
  25. public int[] a = new int[MAX];
  26.  
  27.  
  28. public Class1()
  29. { int choice=1;
  30. while (choice<3)
  31. {
  32. Random random = new Random();
  33. /*for(Int32 i=0;i<10000000;++i)
  34. a[i] = random.Next(100000000);*/
  35. //added
  36. a[0]=20;
  37. a[1]=10;
  38. a[2]=40;
  39. a[3]=30;
  40. a[4]=60;
  41. a[5]=50;
  42. //}added
  43. ArrayList list = new ArrayList();
  44. list.Add(0);
  45. list.Add(a.Length-1);
  46.  
  47. DateTime startTime = DateTime.Now;
  48. Console.Write("1. SigleThreaded Sort\n2.MultithreadSort\nEnter Choice : ");
  49. choice=Convert.ToInt32(Console.ReadLine());
  50. switch(choice)
  51. {
  52. case 1:
  53. startTime = DateTime.Now;
  54. QSort(0, a.Length-1);
  55. break;
  56.  
  57. case 2:
  58. //multithreading startTime = DateTime.Now;
  59. QSort(list);
  60. break;
  61. default:
  62. break;
  63. }
  64. if (choice<3)
  65. {
  66. Console.WriteLine("Time Taken to sort "+a.Length.ToString() +" elements = "+(DateTime.Now.Subtract(startTime)).TotalMilliseconds.ToString()+" MilliSeconds");
  67. //added
  68. for (int index=0; index<MAX; index++)
  69. Console.WriteLine(a[index]);
  70. }
  71.  
  72. }
  73. }
  74.  
  75.  
  76.  
  77. //this works fine
  78. public void QSort( int left, int right )//singlethreaded
  79. {
  80. int pivot, lhold, rhold;
  81.  
  82. lhold = left;
  83. rhold = right;
  84. pivot = a[left];
  85.  
  86. while( left < right )
  87. {
  88. while( (a[right] >= pivot) && (left < right) )
  89. {
  90. right--;
  91. }
  92.  
  93. if( left != right )
  94. {
  95. a[left] = a[right];
  96. left++;
  97. }
  98.  
  99. while( (a[left] <= pivot) && (left < right) )
  100. {
  101. left++;
  102. }
  103.  
  104. if( left != right )
  105. {
  106. a[right] = a[left];
  107. right--;
  108. }
  109. }
  110.  
  111. a[left] = pivot;
  112. pivot = left;
  113. left = lhold;
  114. right = rhold;
  115.  
  116. if( left < pivot )
  117. {
  118. QSort( left, pivot-1 );
  119. }
  120.  
  121. if( right > pivot )
  122. {
  123. QSort( pivot+1, right );
  124. }
  125. }
  126.  
  127.  
  128. //i have problem in this method
  129. public void QSort(object listObject) //Multithreaded
  130.  
  131. {
  132. Int32 left = (Int32)((ArrayList)listObject)[0];
  133. Int32 right = (Int32)((ArrayList)listObject)[1];
  134. int pivot, lhold, rhold;
  135.  
  136. lhold = left;
  137. rhold = right;
  138. pivot = a[left];
  139. lock(a)
  140. {
  141. while( left < right )
  142. {
  143.  
  144. while( (a[right] >= pivot) && (left < right) )
  145. {
  146. right--;
  147. }
  148.  
  149. if( left != right )
  150. {
  151. a[left] = a[right];
  152. left++;
  153. }
  154.  
  155. while( (a[left] <= pivot) && (left < right) )
  156. {
  157. left++;
  158. }
  159.  
  160. if( left != right )
  161. {
  162. a[right] = a[left];
  163. right--;
  164. }
  165. }
  166.  
  167. a[left] = pivot;
  168. pivot = left;
  169. left = lhold;
  170. right = rhold;
  171. }
  172.  
  173. if( left < pivot )
  174. {
  175. ArrayList list = new ArrayList();
  176. list.Add(left);
  177. list.Add(pivot-1);
  178. ThreadPool.QueueUserWorkItem(new WaitCallback(QSort),list);
  179. }
  180.  
  181. if( right > pivot )
  182. {
  183. ArrayList list = new ArrayList();
  184.  
  185. list.Add(pivot+1);
  186. list.Add(right);
  187. ThreadPool.QueueUserWorkItem(new WaitCallback(QSort),list);
  188. }
  189. }
  190. }
  191. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: c# multithreading help needed ---urgent

 
1
  #2
Aug 30th, 2006
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:
  1. myThread = new System.Threading.Thread(new
  2. System.Threading.ThreadStart(myStartingMethod));
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 40
Reputation: beuls is an unknown quantity at this point 
Solved Threads: 2
beuls beuls is offline Offline
Light Poster

Re: c# multithreading help needed ---urgent

 
0
  #3
Sep 5th, 2006
thank u for ur reply. but my code also allots a separte thread from the thread pool. the prob is how to prevent the main thread from terminating b4 the child thread terminates.

Beulah
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: _r0ckbaer is an unknown quantity at this point 
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: c# multithreading help needed ---urgent

 
1
  #4
Sep 5th, 2006
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC