• 0

C# Semaphore Full Exception


Question

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
https://www.neowin.net/forum/topic/836256-c-semaphore-full-exception/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

[edit: nvm my previous reply].

Well, from what you've posted, the code seems correct. I created a small test that replicates yours, using the Console instead of WinForms, and it runs fine.

using System;
using System.Threading;

namespace CsharpThreadsTest {

	class ThreadTest {
		Semaphore semaphore = new Semaphore(0, 4);
		Thread[] threads = new Thread[4];		

		public void MainFunc() {
			for (int i = 0; i < 4; ++i) {
				threads[i] = new Thread(delegate() {
					Worker();
				});
				threads[i].Start();
			}

			for (int i = 0; i < 4; ++i) {
				semaphore.WaitOne();
			}
		}

		public void Worker() {
			Console.WriteLine("Worker!");
			semaphore.Release(1);
		}
	}


	class Program {
		static void Main(string[] args) {
			new ThreadTest().MainFunc();
		}
	}
}

You're probably doing something else incorrectly. What exactly is your exception?

Edited by Dr_Asik
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.