So I've started learning threads in c#. I have a main thread that makes some child threads to split the hard work across more threads. When I do this, the main thread waits for the child threads to finish their job. So i have a semaphore that has the maximum count equal to the number of threads. The initial count is 0 so that the main thread has to wait. Each time one of the child threads does it's job, it releases the semaphore by 1. So in the main thread I have a "for" structure in order to wait for all the child threads. But I always get an exception when I release the semaphore.
The code looks like this:
namespace Tema1
{
public partial class Form1 : Form
{
//other members
private Semaphore Signal;
private[] Thread Slices;
public Form1()
{
InitializeComponent();
//other initializations
this.Signal = new Semaphore(0, Environment.ProcessorCount);
this.Slices = new Thread[Environment.ProcessorCount];
}
//event handler that calls my function which creates the child threads
public void myfunction()
{
//other stuff
for (int aux = 0; aux < Environment.ProcessorCount; aux++)
{
this.Slices[aux] = new Thread(delegate()
{
this.adjustscore( //parameters here);
});
this.Slices[aux].Start();
}
//now we have to wait for the child threads
for(int aux = 0; aux < this.ProcessorCount; aux++)
{
this.Signal.WaitOne();
}
//return result,doesn't matter
}
private void adjustscore(//paramters here)
{
//do some work and post the result
//now we have to release the semaphore once
this.Signal.Release(1);//this is where I get the exception
}
}
Question
Cold Blood
So I've started learning threads in c#. I have a main thread that makes some child threads to split the hard work across more threads. When I do this, the main thread waits for the child threads to finish their job. So i have a semaphore that has the maximum count equal to the number of threads. The initial count is 0 so that the main thread has to wait. Each time one of the child threads does it's job, it releases the semaphore by 1. So in the main thread I have a "for" structure in order to wait for all the child threads. But I always get an exception when I release the semaphore.
The code looks like this:
namespace Tema1 { public partial class Form1 : Form { //other members private Semaphore Signal; private[] Thread Slices; public Form1() { InitializeComponent(); //other initializations this.Signal = new Semaphore(0, Environment.ProcessorCount); this.Slices = new Thread[Environment.ProcessorCount]; } //event handler that calls my function which creates the child threads public void myfunction() { //other stuff for (int aux = 0; aux < Environment.ProcessorCount; aux++) { this.Slices[aux] = new Thread(delegate() { this.adjustscore( //parameters here); }); this.Slices[aux].Start(); } //now we have to wait for the child threads for(int aux = 0; aux < this.ProcessorCount; aux++) { this.Signal.WaitOne(); } //return result,doesn't matter } private void adjustscore(//paramters here) { //do some work and post the result //now we have to release the semaphore once this.Signal.Release(1);//this is where I get the exception } }Link to comment
Share on other sites
2 answers to this question
Recommended Posts