Reputation: 107
I want to ask is there any alternative for the Python's Threadpool function. I am new to C#. I have a function that takes a string as input and i have a list of inputs. What i want is to execute the function in multiple threads(max. 12) such that to every thread one string input from the list is passed and if the size of the list is greater than the number of threads then it waits for any one the thread to complete its execution and then start the function again in the thread with the next input. Hope the question makes sense to you all.
I have done this in Python:
def myfunc(input):
print(input)
if__name__ == "__main__":
inputs = [1,12,2,3,4,5,6,7,8,9,1,0,1,2,3,4,5,6,4,8,97,7]
ThreadPool(12).map(myfunc, inputs)
Upvotes: 3
Views: 302
Reputation: 1062492
static void Main()
{
var inputs = new[] { 1, 12, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0, 1, 2, 3, 4, 5, 6, 4, 8, 97, 7 };
Parallel.ForEach(inputs, new ParallelOptions { MaxDegreeOfParallelism = 12 }, MyFunc);
}
static void MyFunc(int value) { /*...*/ }
Upvotes: 4