I am trying to make a multithreading application. But the input is onely with one thread. But I try to make it with three threads. THis is the program:
class MyThread
{
public int Count;
public Thread Thrd;
public MyThread(string name)
{
Count = 0;
Thrd = new Thread(this.Run);
Thrd.Name = name;
Thrd.Start();
}
// Entry point of thread.
void Run()
{
Console.WriteLine(Thrd.Name + " starting.");
do
{
Thread.Sleep(500);
Console.WriteLine("In " + Thrd.Name +
", Count is " + Count);
Count++;
} while (Count < 10);
Console.WriteLine(Thrd.Name + " terminating.");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
while (true)
{
Console.Write(".");
Thread.Sleep(100);
if (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10)
{
break;
}
Console.WriteLine("Main thread ending.");
}
//do
//{
// Console.Write(".");
// Thread.Sleep(100);
//}
//while (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10);
//Console.WriteLine("Main thread ending.");
// Console.ReadKey();
}
}
But the output is: see image:
So it onely displays one thread. And not three threads.
THank you
This has to be the output:
Main thread starting.
.Child #1 starting.
Child #2 starting.
Child #3 starting.
....In Child #1, Count is 0
In Child #2, Count is 0
In Child #3, Count is 0
.....In Child #1, Count is 1
In Child #2, Count is 1
In Child #3, Count is 1
.....In Child #1, Count is 2
In Child #2, Count is 2
In Child #3, Count is 2
.....In Child #1, Count is 3
In Child #2, Count is 3
In Child #3, Count is 3
.....In Child #1, Count is 4
In Child #2, Count is 4
In Child #3, Count is 4
.....In Child #1, Count is 5
In Child #2, Count is 5
In Child #3, Count is 5
.....In Child #1, Count is 6
In Child #2, Count is 6
In Child #3, Count is 6
.....In Child #1, Count is 7
In Child #2, Count is 7
In Child #3, Count is 7
.....In Child #1, Count is 8
In Child #2, Count is 8
In Child #3, Count is 8
.....In Child #1, Count is 9
Child #1 terminating.
In Child #2, Count is 9
Child #2 terminating.
In Child #3, Count is 9
Child #3 terminating.
Main thread ending.
But if I do it like this:
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
//while (true)
//{
// Console.Write(".");
// Thread.Sleep(100);
// if (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10)
// {
// break;
// }
// Console.WriteLine("Main thread ending.");
//}
do
{
Console.Write(".");
Thread.Sleep(100);
}
while (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10);
Console.WriteLine("Main thread ending.");
Console.ReadKey();
}
It gives the same result.
If I do this:
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Thrd.IsAlive && mt2.Thrd.IsAlive && mt3.Thrd.IsAlive);
Console.WriteLine("Main thread ending.");
Same result. Just one thread.
I try it like this:
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Count < 10 || mt2.Count < 10 || mt3.Count < 10);
Console.WriteLine("Main thread ending.");
But still the same result.
oh:
This worked for me!!
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Thrd.IsAlive || mt2.Thrd.IsAlive || mt3.Thrd.IsAlive);
Console.WriteLine("Main thread ending.");
You're creating three classes. Each class creates a new thread which increments its own property. But you're exiting the console app if all of the numbers are less than 10. In other words, if any one of these reaches 10 before the others
while (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10);
then the Main() will end. There's no guarantee that any one of those counts will reach 10 before that condition is checked, let alone all three of them. One of them could reach 10 before the others even start.
You would get closer to intended affect if you change it to this:
while (mt1.Count < 10 || mt2.Count < 10 || mt3.Count < 10);
In other words, if any one of them is less than 10, keep going. When they all reach 10, stop.
Something else you'll find (or already have) is that unless you're careful, multithreaded apps can behave in unpredictable, inconsistent ways. There's nothing more frustrating than a program that runs perfectly 99% of the time but then suddenly does something different, and then it works as expected again. Here's an example where you can test and see the output.
The if statement you have is incorrect and doesn't work at all. When I used the "while" version, your code works for me except that your strategy to detect when the threads have finished is buggy (perhaps a source of your issue even). Try this code and see if your problems go away. If not, then you might be running into issues locally with available memory/cpu/thread pools. Reboot your computer and try again :)
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
Console.WriteLine("Main thread ending.");
}
I found the solution. See my post
Related
I'm new to multi-threading, I want to achieve something like below using C#.
Thread 0 printed 0
Thread 1 printed 1
Thread 2 printed 2
Thread 3 printed 3
Thread 4 printed 4
Thread 5 printed 5
Thread 6 printed 6
Thread 7 printed 7
Thread 8 printed 8
Thread 9 printed 9
Thread 0 printed 10
Thread 1 printed 11
Thread 2 printed 12
Thread 3 printed 13
Thread 4 printed 14
Thread 5 printed 15
Thread 6 printed 16
Thread 7 printed 17
Thread 8 printed 18
Thread 9 printed 19
.
.
.
Thread 10 printed 99.
I have done something like this but of course, requirement is beyond.
class Program
{
static int count = 0; // the shared counter from 1 to 100
static void Main(string[] args)
{
Thread[] tr = new Thread[10]; // i have created 10 threads each one will print 10 cyclic values
string result = "";
int cc = 0;
while (cc!=10) {
for (int i = 0; i < 10; i++)
{
tr[i] = new Thread(new ThreadStart(printTill10));
tr[i].Start();
tr[i].Join();
}
cc++;
}
}
string s = "";
static void printTill10()
{
Console.WriteLine(++count+ "Printed by thread #"+
Thread.CurrentThread.ManagedThreadId);
}
}
I am confused either I should use a lock or something like monitor.wait or monitor.pulse etc.
Thanks for any help.
Do it like so:
First declare a variable count in the class so that it can be accessed by all threads. Also, create an object locker that will allow us to lock the count variable.
static int count;
static object locker;
Then, create the method that contains the code that the threads will all run:
static void printTill10()
{
while (true)
{
lock (locker)
{
if (count < 100)
{
count++;
Console.WriteLine(string.Format("Thread {0} printed {1}", Thread.CurrentThread.ManagedThreadId.ToString(), count.ToString()));
}
}
}
}
What this code does when run is the following:
Enters a while loop which loops forever.
Locks locker to make sure only one operation is being performed on count at a time.
Checks to see if count is under 100.
Increases count by one.
Prints a string exactly like the one you're trying to get (I used String.Format instead of concentration because its neater)
Simple right? This is the code that our threads will run. Now we can focus on the multithreading part.
static void Main()
{
count = 0; // Be sure to give count an initial value to prevent an exception from being thrown.
locker = new object();
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++)
{
threads[i] = new Thread(() => printTill100());
threads[i].Start();
}
Thread.Sleep(Timeout.Infinite);
}
Our Main does the following:
Gives an initial value to the count and locker variables.
Creates an array to put our threads in.
Enters a for loop which populates the array with threads and starts them.
Makes the main thread (the one code runs in by default) wait forever (specified by Timeout.Infinite. This last bit is an important one. By default, all code runs in a single thread called the main thread. If we don't tell it to wait, it will exit after the loop is done, closing the program. It will not wait until our other threads are finished.
There is just one thing you have missed while writing code in
printTill10()
method.
Just put one lock block in printTill10() method like this.
static void printTill10()
{
lock (_locker)
{
Console.WriteLine(++count + "Printed by thread #" + Thread.CurrentThread.ManagedThreadId);
}
}
and also declare locker object in the Pragram class like
static readonly object _locker = new object();
Here is the complete code
class Program
{
static int count = 0; // the shared counter from 1 to 100
static readonly object _locker = new object();
static void Main(string[] args)
{
Thread[] tr = new Thread[10]; // i have created 10 threads each one will print 10 cyclic values
string result = "";
int cc = 0;
while (cc != 10)
{
for (int i = 0; i < 10; i++)
{
tr[i] = new Thread(new ThreadStart(printTill10));
tr[i].Start();
tr[i].Join();
}
cc++;
}
}
string s = "";
static void printTill10()
{
lock (_locker)
{
Console.WriteLine(++count + "Printed by thread #" + Thread.CurrentThread.ManagedThreadId);
}
}
}
It will work as per your requirement. :)
After a continuous try, I got to complete the requirements of my task. Here is the code:
using System;
using System.Threading;
public class EntryPoint
{
static private int counter = 0;
static private object theLock = new Object();
static object obj = new object();
static private void count()
{
{
for (int i = 0; i < 10; i++)
{
lock (theLock)
{
Console.WriteLine("Count {0} Thread{1}",
counter++, Thread.CurrentThread.GetHashCode());
if (counter>=10)
Monitor.Pulse(theLock);
Monitor.Wait(theLock); } }}
}
static void Main()
{
Thread[] tr = new Thread[10];
for (int i = 0; i < 10; i++)
{
tr[i] = new Thread(new ThreadStart(count));
tr[i].Start();
}
}
}
Monitor maintains a ready queue in a sequential order hence I achieved what I wanted:
I'm looking for a fast way to let many worker threads wait for an event to continue and block the main thread until all worker threads are finished. I first used TPL or AutoResetEvent but since my calculation isn't that expensive the overhead was way too much.
I found a pretty interesting article concerning this problem and got great results (using only one worker thread) with the last synchronization solution (Interlocked.CompareExchange). But I don't know how to utilize it for a scenario where many threads wait for one main tread repeatedly.
Here is an example using single thread, CompareExchange, and Barrier:
static void Main(string[] args)
{
int cnt = 1000000;
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < cnt; i++) { }
Console.WriteLine($"Single thread: {stopwatch.Elapsed.TotalSeconds}s");
var run = true;
Task task;
stopwatch.Restart();
int interlock = 0;
task = Task.Run(() =>
{
while (run)
{
while (Interlocked.CompareExchange(ref interlock, 0, 1) != 1) { Thread.Sleep(0); }
interlock = 2;
}
Console.WriteLine($"CompareExchange synced: {stopwatch.Elapsed.TotalSeconds}s");
});
for (int i = 0; i < cnt; i++)
{
interlock = 1;
while (Interlocked.CompareExchange(ref interlock, 0, 2) != 2) { Thread.Sleep(0); }
}
run = false;
interlock = 1;
task.Wait();
run = true;
var barrier = new Barrier(2);
stopwatch.Restart();
task = Task.Run(() =>
{
while (run) { barrier.SignalAndWait(); }
Console.WriteLine($"Barrier synced: {stopwatch.Elapsed.TotalSeconds}s");
});
for (int i = 0; i < cnt; i++) { barrier.SignalAndWait(); }
Thread.Sleep(0);
run = false;
if (barrier.ParticipantsRemaining == 1) { barrier.SignalAndWait(); }
task.Wait();
Console.ReadKey();
}
Average results (in seconds) are:
Single thread: 0,002
CompareExchange: 0,4
Barrier: 1,7
As you can see Barriers' overhead seems to be arround 4 times higher! If someone can rebuild me the CompareExchange-scenario to work with multiple worker threads this would surely help, too!
Sure, 1 second overhead for a million calculations is pretty less! Actually it just interests me.
Edit:
System.Threading.Barrier seems to be the fastest solution for this scenario. For saving a double blocking (all workers ready for work, all workes finished) I used the following code for the best results:
while(work)
{
while (barrier.ParticipantsRemaining > 1) { Thread.Sleep(0); }
//Set work package
barrier.SignalAndWait()
}
It seems like you might want to use a Barrier to synchronise a number of workers with a main thread.
Here's a compilable example. Have a play with it, paying attention to when the output tells you that you can "Press <Return> to signal the workers to start".
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Demo
{
static class Program
{
static void Main()
{
print("Main thread is starting the workers.");
int numWorkers = 10;
var barrier = new Barrier(numWorkers + 1); // Workers + main (controlling) thread.
for (int i = 0; i < numWorkers; ++i)
{
int n = i; // Prevent modified closure.
Task.Run(() => worker(barrier, n));
}
while (true)
{
print("***************** Press <RETURN> to signal the workers to start");
Console.ReadLine();
print("Main thread is signalling all the workers to start.");
// This will wait for all the workers to issue their call to
// barrier.SignalAndWait() before it returns:
barrier.SignalAndWait();
// At this point, all workers AND the main thread are at the same point.
}
}
static void worker(Barrier barrier, int workerNumber)
{
int iter = 0;
while (true)
{
print($"Worker {workerNumber} on iteration {iter} is waiting for barrier.");
// This will wait for all the other workers AND the main thread
// to issue their call to barrier.SignalAndWait() before it returns:
barrier.SignalAndWait();
// At this point, all workers AND the main thread are at the same point.
int delay = randomDelayMilliseconds();
print($"Worker {workerNumber} got barrier, now sleeping for {delay}");
Thread.Sleep(delay);
print($"Worker {workerNumber} finished work for iteration {iter}.");
}
}
static void print(string message)
{
Console.WriteLine($"[{sw.ElapsedMilliseconds:00000}] {message}");
}
static int randomDelayMilliseconds()
{
lock (rng)
{
return rng.Next(10000) + 5000;
}
}
static Random rng = new Random();
static Stopwatch sw = Stopwatch.StartNew();
}
}
I am trying to understand threading concepts in .Net.
I am unable to use Yield() method. I want the control to go to a parallel thread when i becomes divisible by 10.
Please help.
Below is my sample code:
class ThreadTest
{
//Index i is declared as static so that both the threads have only one copy
static int i;
static void Main(string[] args)
{
Thread t = new Thread(WriteY);
i = 0;
//Start thread Y
t.Start();
//Do something on the main thread.
for (; i < 100; i++)
{
if (i % 10 == 0)
{
//Simulate Yield() function
Thread.Sleep(0);
Console.WriteLine("The X thread");
}
Console.Write(i + ":X ");
}
Console.ReadKey(true);
}
static void WriteY()
{
for (; i < 100; i++)
{
if (i % 10 == 0)
{
//Simulate Yield() function
Thread.Sleep(0);
Console.WriteLine("The Y thread");
}
Console.Write(i + ":Y ");
}
}
}
I get the compile time error:
System.Threading.Thread does not contain a definition for 'Yield'
Answered by Tudor. This method will only work on .Net 4.0 and upwards.
Ideally I would want one thread to start and want each thread to execute for 10 incremented of i each. With my current method, I either get all 'X' or all 'Y'.
Edit:
With inputs from Tudor and TheHe - I have been able to get alternate X and Y. The crux of the problem was usage of lock object. But the output of this code is not predictable.
Thread.Yield will simply enable the scheduler to select a different thread that is ready to run:
Causes the calling thread to yield execution to another thread that is
ready to run on the current processor. The operating system selects
the thread to yield to.
If other threads in your application are also waiting on that lock, you can yield all you want, they won't get a chance to run.
Btw, Yield is a .NET 4.0+ method. Make sure you're not targeting an earlier version.
Edit: IMO, to do what you want you should use events:
class Test
{
//Index i is declared as static so that both the threads have only one copy
static int i;
static AutoResetEvent parentEvent = new AutoResetEvent(true);
static AutoResetEvent childEvent = new AutoResetEvent(false);
static void Main(string[] args)
{
Thread t = new Thread(WriteY);
i = 0;
//Start thread Y
t.Start();
// Print X on the main thread
parentEvent.WaitOne();
while (i < 100)
{
if (i % 10 == 0)
{
childEvent.Set();
parentEvent.WaitOne();
}
Console.Write(i + ":Y ");
i++;
}
t.Join();
}
static void WriteY()
{
childEvent.WaitOne();
while (i < 100)
{
if (i % 10 == 0)
{
parentEvent.Set();
childEvent.WaitOne();
}
Console.Write(i + ":X ");
i++;
}
}
}
Forget Thread.Yield; that is unrelated to what you are trying to do. Ultimately, you have a lock, which uses Monitor to synchronize access. Inside the lock, your thread exclusively has access. What you need to do is relinquish the lock temporarily; the way you do that is with Monitor.Wait. However, if you Wait, you also end up in the "waiting" queue rather than the "ready" queue, so in order to make sure that each thread gets attention, we also need to Pulse, both before the Wait, and also at the end (to make sure both threads get chance to exit). Here we go:
using System.Threading;
using System;
class ThreadTest
{
//Index i is declared as static so that both the threads have only one copy
static int i;
//The lock object
static readonly object locker = new object();
static void Main(string[] args)
{
Thread t = new Thread(WriteY);
i = 0;
//Start thread Y
t.Start();
lock (locker)
{
// Print X on the main thread
for (; i < 100; i++)
{
if (i % 10 == 0)
{
Monitor.PulseAll(locker); // move any "waiting" threads to the "ready" queue
Monitor.Wait(locker); // relinquish the lock, and wait for a pulse
Console.WriteLine("The X thread");
}
Console.Write(i + ":X ");
}
Monitor.PulseAll(locker);
}
Console.ReadKey(true);
}
static void WriteY()
{
lock (locker)
{
for (; i < 100; i++)
{
if (i % 10 == 0)
{
Monitor.PulseAll(locker); // move any "waiting" threads to the "ready" queue
Monitor.Wait(locker); // relinquish the lock, and wait for a pulse
Console.WriteLine("The Y thread");
}
Console.Write(i + ":Y ");
}
Monitor.PulseAll(locker); // move any "waiting" threads to the "ready" queue
}
}
}
from my point of view, you're locking "locker" in current thread and want to yield the current task to an other thread...
the lock is held by the first thread all the time -- it can't work?!
you have to manually lock the objects if you want to use multiple threads...
In the code below I want to syncronize the reporting of the results of a list of tasks. This is working now because task.Result blocks until the task completes. However, task id = 3 takes a long time to complete and blocks all of the other finished tasks from reporting their status.
I think that I can do this by moving the reporting (Console.Write) into a .ContinueWith instruction but I don't have a UI thread so how do I get a TaskScheduler to syncronize the .ContinueWith tasks?
What I have now:
static void Main(string[] args)
{
Console.WriteLine("Starting on {0}", Thread.CurrentThread.ManagedThreadId);
var tasks = new List<Task<int>>();
for (var i = 0; i < 10; i++)
{
var num = i;
var t = Task<int>.Factory.StartNew(() =>
{
if (num == 3)
{
Thread.Sleep(20000);
}
Thread.Sleep(new Random(num).Next(1000, 5000));
Console.WriteLine("Done {0} on {1}", num, Thread.CurrentThread.ManagedThreadId);
return num;
});
tasks.Add(t);
}
foreach (var task in tasks)
{
Console.WriteLine("Completed {0} on {1}", task.Result, Thread.CurrentThread.ManagedThreadId);
}
Console.WriteLine("End of Main");
Console.ReadKey();
}
I would like to move to this or something similar but I need the Console.Write("Completed...") to all happen on the same thread:
static void Main(string[] args)
{
Console.WriteLine("Starting on {0}", Thread.CurrentThread.ManagedThreadId);
for (var i = 0; i < 10; i++)
{
var num = i;
Task<int>.Factory.StartNew(() =>
{
if (num == 3)
{
Thread.Sleep(20000);
}
Thread.Sleep(new Random(num).Next(1000, 10000));
Console.WriteLine("Done {0} on {1}", num, Thread.CurrentThread.ManagedThreadId);
return num;
}).ContinueWith(value =>
{
Console.WriteLine("Completed {0} on {1}", value.Result, Thread.CurrentThread.ManagedThreadId);
}
/* need syncronization context */);
}
Console.WriteLine("End of Main");
Console.ReadKey();
}
-- SOLUTION --
After getting some comments and reading some of the solutions this is the complete solution that does what I want. The goal here is to process severl long running tasks as fast as possible and then do something with the results of each task one at a time.
static void Main(string[] args)
{
Console.WriteLine("Starting on {0}", Thread.CurrentThread.ManagedThreadId);
var results = new BlockingCollection<int>();
Task.Factory.StartNew(() =>
{
while (!results.IsCompleted)
{
try
{
var x = results.Take();
Console.WriteLine("Completed {0} on {1}", x, Thread.CurrentThread.ManagedThreadId);
}
catch (InvalidOperationException)
{
}
}
Console.WriteLine("\r\nNo more items to take.");
});
var tasks = new List<Task>();
for (var i = 0; i < 10; i++)
{
var num = i;
var t = Task.Factory.StartNew(() =>
{
if (num == 3)
{
Thread.Sleep(20000);
}
Thread.Sleep(new Random(num).Next(1000, 10000));
Console.WriteLine("Done {0} on {1}", num, Thread.CurrentThread.ManagedThreadId);
results.Add(num);
});
tasks.Add(t);
}
Task.Factory.ContinueWhenAll(tasks.ToArray(), _ => results.CompleteAdding());
Console.WriteLine("End of Main");
Console.ReadKey();
}
You'll have to create a writer task of some sort, however, keep in mind even this task can be rescheduled onto another native or managed thread! Using the default scheduler in TPL you have no control over which managed thread receives the work.
public class ConcurrentConsole
{
private static BlockingCollection<string> output
= new BlockingCollection<string>();
public static Task CreateWriterTask(CancellationToken token)
{
return new Task(
() =>
{
while (!token.IsCancellationRequested)
{
string nextLine = output.Take(token);
Console.WriteLine(nextLine);
}
},
token);
}
public static void WriteLine(Func<string> writeLine)
{
output.Add(writeLine());
}
}
When I switched your code to use this I received the following output:
End of Main
Done 1 on 6
Completed 1 on 6
Done 5 on 9
Completed 5 on 9
Done 0 on 4
Completed 0 on 4
Done 2 on 5
Completed 2 on 13
Done 7 on 10
Completed 7 on 10
Done 4 on 8
Completed 4 on 5
Done 9 on 12
Completed 9 on 9
Done 6 on 6
Completed 6 on 5
Done 8 on 11
Completed 8 on 4
Done 3 on 7
Completed 3 on 7
Even with your code sending () => String.Format("Completed {0} on {1}"... to ConcurrentConsole.WriteLine, ensuring the ManagedThreadId would be picked up on the ConcurrentConsole Task, it still would alter which thread it ran on. Although with less variability than the executing tasks.
You can use OrderedTaskScheduler to ensure only one task completion is run at a time; however, they will run on a threadpool thread (not necessarily all on the same thread).
If you really need them all on the same thread (not just one at a time), then you can use ActionThread from the Nito.Async library. It provides a SynchronizationContext for its code, which can be picked up by FromCurrentSynchronizationContext.
I would suggest:
1) Creating a lock object
2) Create a list of strings to be written
3) Spawn a thread that loops, sleeping for a bit, then locking the list of strings, then if it isn't empty, writing all of them and emptying the list
4) Other threads then lock the list, add their status, unlock and continue.
object writeListLocker = new object();
List<string> linesToWrite = new List<string>();
// Main thread loop
for (; ; )
{
lock (writerListLocker)
{
foreach (string nextLine in linesToWrite)
Console.WriteLine(nextLine);
linesToWrite.Clear();
}
Thread.Sleep(500);
}
// Reporting threads
lock (writerListLocker)
{
linesToWrite.Add("Completed (etc.)");
}
I think you expect a result like the following.
Starting on 8
Done 1 on 11
Completed 1 on 9
Done 5 on 11
Completed 5 on 9
Done 0 on 10
Completed 0 on 9
Done 2 on 12
Completed 2 on 9
Done 7 on 16
Completed 7 on 9
Done 4 on 14
Completed 4 on 9
Done 9 on 18
Completed 9 on 9
Done 6 on 15
Completed 6 on 9
Done 8 on 17
Completed 8 on 9
Done 3 on 13
Completed 3 on 9
As below, I used the StaSynchronizationContext in my code from the Understanding SynchronizationContext where a synchronized call in one thread is explained well. Please, refer to it.
My code snippet is:
static void Main(string[] args)
{
StaSynchronizationContext context = new StaSynchronizationContext();
StaSynchronizationContext.SetSynchronizationContext(context);
Console.WriteLine("Starting on {0}", Thread.CurrentThread.ManagedThreadId);
for (var i = 0; i < 10; i++)
{
var num = i;
Task<int>.Factory.StartNew(() =>
{
if (num == 3)
{
Thread.Sleep(20000);
}
Thread.Sleep(new Random(num).Next(1000, 10000));
Console.WriteLine("Done {0} on {1}", num, Thread.CurrentThread.ManagedThreadId);
return num;
}).ContinueWith(
value =>
{
Console.WriteLine("Completed {0} on {1}", value.Result, Thread.CurrentThread.ManagedThreadId);
}
,TaskScheduler.FromCurrentSynchronizationContext());
}
Console.WriteLine("End of Main");
Console.ReadKey();
}
I've got this code below, where I spawn several threads, normally about 7, and join them to wait until all are done:
List<Thread> threads = new List<Thread>();
Thread thread;
foreach (int size in _parameterCombinations.Keys)
{
thread = new Thread(new ParameterizedThreadStart(CalculateResults));
thread.Start(size);
threads.Add(thread);
}
// wait for all threads to finish
for (int index = 0; index < threads.Count; index++)
{
threads[index].Join();
}
When I check this most of the time only one or two threads are running at the same time, only once or twice when I rerun the app all of them executed.
Is there any way to force all the threads to start executing?
Many thanks.
Your code is fine.. i changed it abit to show you that the execution of the thread is not limited to 2 threads.
I would look for problems in the calculation process..
class Program
{
static void Main(string[] args)
{
List<Thread> threads = new List<Thread>();
Thread thread;
for (int i = 0; i < 7; i++)
{
thread = new Thread(new ParameterizedThreadStart(CalculateResults));
thread.Start();
threads.Add(thread);
}
// wait for all threads to finish
for (int index = 0; index < threads.Count; index++)
{
threads[index].Join();
}
}
static void CalculateResults(object obj)
{
Console.WriteLine("Thread number " + Thread.CurrentThread.ManagedThreadId + " is alive");
Thread.Sleep(1000);
Console.WriteLine("Thread number " + Thread.CurrentThread.ManagedThreadId + " is closing");
}
}