Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:35:36+00:00 2026-06-14T12:35:36+00:00

class myclass { private Semaphore _sync; myclass () { _sync = new Semaphore(1,1); }

  • 0
class myclass 
{
    private Semaphore _sync;

    myclass ()
    {
          _sync = new Semaphore(1,1);
    }

    doasync()
    {
           _sync.WaitOne();
           //do some stuff
           _sync.Release();
    }
}


 somefunctionsomewhere() 
 {
     var myobject = new myclass();    
     //spawn 100 threads that all call myobject.doasync()
 }

is the above safe + clean? Will the both _sync get disposed and myclass be collected when all 100 threads have finished doing their async work?

If not, what’s the best way to know when to call _sync.Dispose() ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:35:38+00:00Added an answer on June 14, 2026 at 12:35 pm

    Short answer: No, you don’t need to call Dispose().

    Long answer:

    Though it is recommended to call Dispose() when you are finished using the Semaphore, the garbage collector will take care of the semaphore’s resources sooner or later.

    An explicit call to Dispose() will guarantee an early release of the associated resource, but it’s only required if you are creating and forgetting a lot of Semaphore instance in a short amount of time.

    And if you call Dispose(), it is perfectly fine to use a simple Dispose() call. You don’t need to create a finalizer ~myclass() because that would just duplicate what’s already done the Semaphore implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As in: public class MyClass { private static var MyProp = new {item1 =
Given a generic class: class MyClass ( private List l = new LinkedList <String>();
Consider the following MCVE : public class MyClass { private LinkedList<Foo> myList = new
Take this code: public class MyClass { private final Object _lock = new Object();
Code: public class MyClass { private Map<Integer,String> myMap; ........................... void methodFillMap(){ myMap=new HashMap<Integer, String>();
Code: public class MyClass { private Map<Integer,String> myMap=new HashMap<Integer, String>(); ........................... void methodFillMap(){ myMap.put(.....);
class MyClass { private function isExist($arr) { // need to do some formatting here
I have a class like this: Public Class MyClass Private _intList As New List(Of
I have a class: class MyClass { private var num : Int; } I
Suppose I have a class public class MyClass { private Set<String> set = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.