C # initial thread with a function that has a delegate parameter
public delegate bool FunctieCompara(int a, int b); this is the delegate
Simple function calls:
TyG.bubbleSort(TyG.max, TyG.Lungime, TyG.Secv);
TyG.bubbleSort(TyG.min, TyG.Lungime, TyG.secvMin);
I have Class Sorts
, and in this class I have many methods like
public void bubbleSort(functionDelegate f, int n, int [] v)
and much more, but with these parameters. In another class, I have an instance
Sortst tyg = new Sorts()
I want to create a stream
Thread Thr = new Thread(new ThreadStart(tyg.bubbleSort(functionDelegate)))
I didn't understand that this thing works in my case, how can I use a stream using the method that the delegate uses, in my case the delegate is max/min
to compare numbers to sort in place in v[]
. I want to make 2 threads to execute both types bubbleSort(max, n, v)
and bubbleSort(min, n, v)
at the same time. This is what the thread does, anyway, can anyone help me a little?
source to share
Do you mean this?
var t1 = new Thread(
o =>
{
tyg.bubbleSort(max, n, v1);
});
var t2 = new Thread(
o =>
{
tyg.bubbleSort(min, n, v2);
});
t1.Start(); // start threads
t2.Start();
t1.Join(); // wait for both threads to finish
t2.Join();
Note: If you sort on the site, you must use different arrays ( v1
and v2
), because otherwise the threads will rewrite the same array.
Task
Check out the .NET 4.0 construct if you're interested .
Alternatively, if you want to be cool (.NET 4.0+):
Parallel.Invoke(
() => tyg.bubbleSort(max, n, v1),
() => tyg.bubbleSort(min, n, v2));
source to share
Using .NET Framework 4 and Parallel Task Library:
var factory = new TaskFactory(TaskCreationOptions.None,
TaskContinuationOptions.None);
var sorts = new Sorts();
FunctieCompara greaterThanComparer = (a, b) => { return a > b; };
FunctieCompara lessThanComparer = (a, b) => { return a < b; };
var sorterWorkers = new Task[]
{
factory.StartNew(() => sorts.BubbleSort(greaterThanComparer, 0, new int[] {})),
factory.StartNew(() => sorts.BubbleSort(lessThanComparer, 0, new int[] {}))
};
Task.WaitAll(sorterWorkers);
source to share