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

  • SEARCH
  • Home
  • 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 6782305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:43:32+00:00 2026-05-26T16:43:32+00:00

In the following example GetList returns an instance of a static (shared) variable. That

  • 0

In the following example GetList returns an instance of a static (shared) variable. That one needs locking in order to be thread-safe.

But what about DoSomething which doesn’t use any static variables outside the method? Does it need locking too?

EDIT: I’d like to clarify that in this particular case I expect DoSomething to always print 0-100 in sequence (i.e. no 0123745…) regardless of number of calling threads. Or, in general, that different threads don’t affect each other’s variables (printing to console is only an example). The language in the following example is VB.NET.

As paxdiablo said:

In this case, it appears the only thing touched is the local variable
i which would have a separate copy for every function invocation.
In other words, it wouldn’t need protecting.

That is exactly what I was trying to solve. Thank you!

Public Class TestClass

    Private Shared lock As New Object
    Private Shared list As List(Of Integer)

    Public Shared Function GetList() As List(Of Integer)

        SyncLock lock
            If list Is Nothing Then
                list = New List(Of Integer)
            End If
            Return list
        End SyncLock

    End Function

    Public Shared Sub DoSomething()

        Dim i As Integer

        For i = 0 To 100
            Console.WriteLine(i.ToString)
        Next

    End Sub

End Class
  • 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-05-26T16:43:33+00:00Added an answer on May 26, 2026 at 4:43 pm

    Well, that would mostly depend on the language which you haven’t specified but, generally, if code doesn’t touch a resource that another thread can also touch, it doesn’t have to be protected.

    In this case, it appears the only thing touched is the local variable i which would have a separate copy for every function invocation. In other words, it wouldn’t need protecting.

    Of course, it could be argued that the console is also a resource and may need protection if, for example, you didn’t want lines interfering with each other (synclock the write) or wanted the entire hundred lines output as a single unit (synclock the entire for loop).

    But that won’t really protect the console, just the block of code here that uses it. Other threads will still be able to write to the console by not using this method.

    Bottom line, I don’t think you need a synclock in the second method.


    This section below is not relevant if you’re using SyncLock in VB.Net (as now seems to be the case). The language guarantees that the lock is released no matter how you leave the block. I’ll leave it in for hysterical purposes.

    I’d be a little concerned about your synclock placement in the first method, especially if the return statement was a transfer of control back to the caller (and the synclock didn’t automatically unlock on scope change). This looks like you can return without unlocking, which would be a disaster. I would think the following would be more suitable:

    Public Shared Function GetList() As List(Of Integer)
        SyncLock lock
            If list Is Nothing Then
                list = New List(Of Integer)
            End If
        End SyncLock
        Return list
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following example code (SSCCE) complains that local variable a must be final. public
Example: Suppose in the following example I want to match strings that do not
Let me use the following example to explain my question: public string ExampleFunction(string Variable)
In the following example should I expect that values.size() will be called every time
The following example, beats me. I've been so far thinking, that when functor is
In the following example, is there a way to construct the object such that
The following example is one way in JavaScript to create and manipulate objects, i.e.
The following example works, but how can I change it so that instead of
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()

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.