473,382 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Multithreaded loop?!

24
Hi guys!

I have a simple for loop in c# that traverses all elements in an array, and invokes a method ProcessArrayIndex() each time. The result is stored in another array, at the same index.

Expand|Select|Wrap|Line Numbers
  1. int[] processedArray;
  2. int[] myArray;
  3.  
  4. for (int k = 0; k < myArray.Length; k++)
  5. {
  6.   myArray[k] = ProcessArrayIndex(k);
  7. }
Now I would like to make this faster, through the use of multiple threads.

I would like to have (for example) 10 threads working in parallel. Each loop iteration grabs one of the free threads for it to execute. If all threads are busy, the loop will wait until a thread becomes available. I tried messing something up with ThreadPool but I didn't manage to get what i want.

Basically I would need:
-> A way to have multiple loop iterations running concurrently (with a limited maximum number of threads)
-> A way to make sure that all threads have finished executing before continuing with the rest of my code!

Any ideas &/or sample code would be very much appreciated!

thanks!
Apr 7 '07 #1
3 1358
Guy007
24
BTW, the size of the loop is not known. I might have even a couple of hundred elements to process... :S
Apr 7 '07 #2
nmadct
83 Expert
First of all, be aware that splitting the job up into 10 threads won't make things any faster unless you're running the program on a machine with many CPUs. On a single-CPU machine, you will probably lose speed by using multiple threads. That said, let's look at how you would go about it.

I would have each thread process a subsection of the range of indexes. That way you don't have to deal with any kind of thread pooling, instead you have a fixed number of threads but the number of indexes that each thread is responsible for processing increases as the overall input range gets larger.

Each thread generates a list of results. Then, when the threads are all done, concatenate the result lists in the proper order (i.e. the same order as their inputs).
Apr 7 '07 #3
nmadct
83 Expert
If the time it takes to process each index is highly variable, so that threads with the same number of indexes to process are likely to finish at different times, you could make all of the threads take indexes from a shared counter or queue. The problem with this method is that you have to do locking on the queue or counter, which introduces a bottleneck. Ideally you want threads to run independently with as little communication as possible for as long as possible to get maximum benefit from multithreading.
Apr 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: John Lull | last post by:
I'm writing a multithreaded COM server to manage a pool of hardware resources. All objects are designed to be thread-safe, and I've set sys.coinit_flags to COINIT_MULTITHREADED before importing...
1
by: Elbert Lev | last post by:
I started with Python two weeks ago and already saved some time and efforts while writing 2 programs: 1. database extraction and backup tool, which runs once a month and creates a snapshot of...
2
by: pradyumna | last post by:
In Project settins - C/C++ - Code Generation, what is the difference between the option "Multithreaded" and "Multithreaded DLL". I understand that on selecting multithreaded option, single and...
10
by: Arsen V. | last post by:
Hello, Does anyone know if an object or how to create one, that will allow me to fetch up to 10 URLs (containing XML-feed data) in an extremelly fast server side fashion? If the request is...
5
by: Ken Saganowski | last post by:
Does anyone have any sample code for a multi threaded form that refreshes itself while an extended operation is executing: Example: I am updating a local dataset which could potentially have...
4
by: Bruno van Dooren | last post by:
Hi, i was debugging a multithreaded app, when i stumbled across some weird behavior of the debugger. in the destructor of my main object i send a stop event to the worker thread, and then...
9
by: wdwedw | last post by:
I have included all the source codes in the attached MyTest.zip (http://www.codeguru.com/forum/attachment.php?attachmentid=11218) There are three projects: VBTestCOM project is a apartment...
4
by: Richard Bell | last post by:
I'm new to VB.net and have a multithreaded application that needs to display a VB form with a cancel button. The thread that displays the form needs to set a cancel flag if the button is pressed. ...
9
by: Svein Seldal | last post by:
Hi A couple of mulithreaded C API python questions: I) The PyGILState_Ensure() simply ensures python api call ability, it doesnt actually lock the GIL, right? PyGILState_STATE gstate;...
3
by: | last post by:
Is it possible to have just a multithreaded sub procedure? What I need is a timer time_elapsed event (2 sec interval) send params to a sub that is multithreaded. I have a COM component used to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.