Solved

multithreading in a loop C#

Posted on 2013-12-16
8
363 Views
Last Modified: 2014-01-08
Hi;

I have a loop that has a function call in it which operates on some calculation and returns a string array.

The function accepts incoming values. The things that I expect to do in that function is serializable and thread safe.

So, how can i achieve this in multithreaded idea?

Should i create a seperate class for this or what to do?

Pseudocode:
public string[] GetAAA(string a, MyClass myObject)
{
...
 for (int i = 0; i < someLength; i++)
                {
arrRes[i] = obj.foo(a, b, c, d) ; // a, b, c, d are serializable
...
}

}

Open in new window

0
Comment
Question by:jazzIIIlove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 39721452
The simplest would be to use PLINQ to run the methods using several threads. Example:

addRes = Enumerable.Range(0, someLength)
  .AsParallel()
  .AsOrdered()
  .Select(i => obj.foo(a, b, c, d))
  .ToArray();

Open in new window


(Requires framework 4.0 or later.)
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39721745
Hi;

I was just considering it. Great and clever!

One dummy question, is your code segment replaces that for loop completely in that function?

I mean, your code:

addRes = Enumerable.Range(0, someLength)
  .AsParallel()
  .AsOrdered()
  .Select(i => obj.foo(a, b, c, d))
  .ToArray();

Open in new window


Is more detailed one like this?
public string[] GetAAA(string a, MyClass myObject)
{
...//no for loop
addRes = Enumerable.Range(0, someLength)
  .AsParallel()
  .AsOrdered()
  .Select(i => obj.foo(a, b, c, d))
  .ToArray();
...
}

Open in new window

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 39722943
Yes, that is right.

Using AsParallel will make it run in several threads, and using AsOrdered will make sure that the items in the result is in the order of the initial range.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39727287
Hi,

I have seen Parallel.For(
in some examples. Can i use this instead? Note that order of execution is still important.

Regards.
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 39728252
You could, but Parallel.For doesn't return a collection of results, so you would have to collect the results yourself, and rearrange them into the right order. As you are doing that from several threads, you also need to synchronise the access between them. PLINQ does all that for you.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39728967
ok, i understand but i was using the array element,
arrRes[i] 

Open in new window

in the code and the index. So, i guess Parallel. For is more relevant but how can i rearrange them? Do i need a old-school lock mechanism for this?

Regards.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39729011
What I have done is that:
System.Collections.Concurrent.ConcurrentStack<string> results = new System.Collections.Concurrent.ConcurrentStack<string>();

Parallel.For(0, someLength, i =>
{
someString = someArray[i];

arrRes[i] = someObject.someFunction(....);
results.Push(arrRes[i]);

Open in new window


My question is that with the current code, do i provide concurrency by using ConcurrentStack instance?

Regards.
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 39730212
That would work for collecting the results, but you would need to include the index in each result also, so that you can sort them afterwards. Now they would end up in the collection in the order that they would happen to be completed.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question