patterncsharpMajor
Printing all even and odd numbers with threads
Viewed 0 times
allwiththreadsnumbersprintingandevenodd
Problem
The original question is on careercup.
Write a multi threaded C code with one thread printing all even numbers and the other all odd numbers. The output should always be in sequence
ie. 0,1,2,3,4....etc
Now I want to use
Any improvements?
Write a multi threaded C code with one thread printing all even numbers and the other all odd numbers. The output should always be in sequence
ie. 0,1,2,3,4....etc
Now I want to use
C# for it. class Program
{
static Object obj = new Object();
static Thread t1;
static Thread t2;
static LinkedList a = new LinkedList();
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
a.AddLast(i);
}
t1 = new Thread(PrintOdd);
t2 = new Thread(PrintEven);
t1.Name = "Odd";
t2.Name = "Even";
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine("Done!");
Console.Read();
}
private static void PrintOdd()
{
while (true)
{
if (a.Count == 0)
break;
lock (obj)
{
int x = a.First();
if (x % 2 != 0)
{
Console.WriteLine(Thread.CurrentThread.Name + " " + x);
a.RemoveFirst();
}
}
}
}
private static void PrintEven()
{
while (true)
{
lock (obj)
{
if (a.Count == 0)
break;
int x = a.First();
if (x % 2 == 0)
{
Console.WriteLine(Thread.CurrentThread.Name + " " + x);
a.RemoveFirst();
}
}
}
}
}Any improvements?
Solution
This looks very good to me, but I am not very advanced at C# myself. I can give you some tips, though.
First, you should not use ambiguous names like
Second, you can shorten this:
Into this:
And this:
Also, you could look into other forms of mutual exclusion techniques, such as semaphores and mutexes, instead of just using an
This is an implementation with a
First, you should not use ambiguous names like
t1 and t2. You should use more descriptive names like EvenThread and OddThread instead.Second, you can shorten this:
while (true)
{
if (a.Count == 0)
break;Into this:
while (Numbers.Count > 0) // For PrintOdd()And this:
while (Numbers.Count > 1) // For PrintEven()Also, you could look into other forms of mutual exclusion techniques, such as semaphores and mutexes, instead of just using an
Object.This is an implementation with a
SemaphoreSlim (only code shown is changed):using System.Threading;
class Program
{
static SemaphoreSlim ThreadLock = new SemaphoreSlim(1,1);
static Thread Odd;
static Thread Even;
static LinkedList Numbers = new LinkedList();
static void Main(string[] args)
{
for (int i = 0; i 0)
{
ThreadLock.Wait();
int x = Numbers.First();
if (x % 2 != 0)
{
Console.WriteLine(Thread.CurrentThread.Name + " " + x);
Numbers.RemoveFirst();
}
ThreadLock.Release();
}
}
private static void PrintEven()
{
while (Numbers.Count > 1)
{
ThreadLock.Wait();
int x = Numbers.First();
if (x % 2 == 0)
{
Console.WriteLine(Thread.CurrentThread.Name + " " + x);
Numbers.RemoveFirst();
}
ThreadLock.Release();
}
}
}Code Snippets
while (true)
{
if (a.Count == 0)
break;while (Numbers.Count > 0) // For PrintOdd()while (Numbers.Count > 1) // For PrintEven()using System.Threading;
class Program
{
static SemaphoreSlim ThreadLock = new SemaphoreSlim(1,1);
static Thread Odd;
static Thread Even;
static LinkedList<int> Numbers = new LinkedList<int>();
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
Numbers.AddLast(i);
}
Odd = new Thread(PrintOdd);
Even = new Thread(PrintEven);
Odd.Name = "Odd";
Even.Name = "Even";
Odd.Start();
Even.Start();
Odd.Join();
Even.Join();
Console.WriteLine("Done!");
Console.Read();
}
private static void PrintOdd()
{
while (Numbers.Count > 0)
{
ThreadLock.Wait();
int x = Numbers.First();
if (x % 2 != 0)
{
Console.WriteLine(Thread.CurrentThread.Name + " " + x);
Numbers.RemoveFirst();
}
ThreadLock.Release();
}
}
private static void PrintEven()
{
while (Numbers.Count > 1)
{
ThreadLock.Wait();
int x = Numbers.First();
if (x % 2 == 0)
{
Console.WriteLine(Thread.CurrentThread.Name + " " + x);
Numbers.RemoveFirst();
}
ThreadLock.Release();
}
}
}Context
StackExchange Code Review Q#72255, answer score: 20
Revisions (0)
No revisions yet.