D
DC
Hi,
to use a specified amount of threads to carry out a certain step I
typically use Semaphores. I then have to be able to determine when all
the threads have done their work.
I have attached a code snipped of how I do this. I would be happy if
someone could review this, since I lately had some problems possibly
caused by this approach. It seems that sometimes the synchronization
point will never be reached, although I am quiet confident that all
threads were able to do what they were supposed to do and release the
Semaphore.
I am especially worried about this code:
// Is this the way to wait until all threads have finished?
for (int k = 0; k < maxthreads; k++)
workSemaphore.WaitOne();
Looks clumsy to me, but it was the only simple way I found to join the
threads.
TIA for any comments!
Regards
DC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadingTest
{
class Program
{
static void Main(string[] args)
{
int maxthreads = 100;
Semaphore workSemaphore = new Semaphore(maxthreads, maxthreads);
for (int i = 0; i < 1000; i++)
{
ThreadData td = new ThreadData();
td.DataSemaphore = workSemaphore;
td.IntData = i;
Thread t = new Thread(new ParameterizedThreadStart(DoWork));
t.Start(td);
}
// Is this the way to wait until all threads have finished?
for (int k = 0; k < maxthreads; k++)
workSemaphore.WaitOne();
Console.WriteLine("Done.");
Console.ReadLine();
}
public static void DoWork(object data)
{
try
{
((ThreadData)data).DataSemaphore.WaitOne();
Console.Write("{0, 3}-", ((ThreadData)data).IntData);
// wait 0 - 2000 ms
Random ran = new Random();
Thread.Sleep((int)ran.NextDouble() * 2000);
}
finally
{
((ThreadData)data).DataSemaphore.Release();
}
}
public class ThreadData
{
public Semaphore DataSemaphore{ get; set; }
public int IntData { get; set; }
}
}
}
to use a specified amount of threads to carry out a certain step I
typically use Semaphores. I then have to be able to determine when all
the threads have done their work.
I have attached a code snipped of how I do this. I would be happy if
someone could review this, since I lately had some problems possibly
caused by this approach. It seems that sometimes the synchronization
point will never be reached, although I am quiet confident that all
threads were able to do what they were supposed to do and release the
Semaphore.
I am especially worried about this code:
// Is this the way to wait until all threads have finished?
for (int k = 0; k < maxthreads; k++)
workSemaphore.WaitOne();
Looks clumsy to me, but it was the only simple way I found to join the
threads.
TIA for any comments!
Regards
DC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadingTest
{
class Program
{
static void Main(string[] args)
{
int maxthreads = 100;
Semaphore workSemaphore = new Semaphore(maxthreads, maxthreads);
for (int i = 0; i < 1000; i++)
{
ThreadData td = new ThreadData();
td.DataSemaphore = workSemaphore;
td.IntData = i;
Thread t = new Thread(new ParameterizedThreadStart(DoWork));
t.Start(td);
}
// Is this the way to wait until all threads have finished?
for (int k = 0; k < maxthreads; k++)
workSemaphore.WaitOne();
Console.WriteLine("Done.");
Console.ReadLine();
}
public static void DoWork(object data)
{
try
{
((ThreadData)data).DataSemaphore.WaitOne();
Console.Write("{0, 3}-", ((ThreadData)data).IntData);
// wait 0 - 2000 ms
Random ran = new Random();
Thread.Sleep((int)ran.NextDouble() * 2000);
}
finally
{
((ThreadData)data).DataSemaphore.Release();
}
}
public class ThreadData
{
public Semaphore DataSemaphore{ get; set; }
public int IntData { get; set; }
}
}
}