Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

multithreading in a loop C#

Posted on 2013-12-16
8
Medium Priority
?
375 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 2000 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

Independent Software Vendors: 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!

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

705 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