Is SyncLock reetrant?

I have the following code in a class that manages refreshing some object automatically that also allows you to manually refresh. I want to make it thread-safe.

Public Function ForceRefresh() As Foo
  DoRefresh()
  ResetTimer()
  Return Me.CurrentFoo
End Function

Private Sub DoRefresh()
  Me._currentFoo = Me._retriever.GetTheFoo()
End Sub

I'm tempted to Synclock both of these methods:

  • ForceRefresh: So we don't get multiple consumer threads trying to force a refresh and reset the timer at the same time
  • DoRefresh: So we don't try to GetTheFoo() if we're already in the middle of trying to retrieve one. I'm using System.Threading.Timer which might call back on any of the ThreadPool threads, so simply using a flag might not be appropriate (but maybe is still necessary?)

... like this:

Private _syncRoot As New Object

Public Function ForceRefresh() As Foo
  Synclock _syncRoot
    'Snip ...
  End Synclock
End Function

Private Sub DoRefresh()
  Synclock _syncRoot
    'Snip ...
  End Synclock
End Sub

But then I end up trying to Synclock on _syncRoot twice when calling ForceRefresh... now if Synclock is reentrant then nesting these won't be an issue. The MSDN documentation page seems to imply that this is the case, but doesn't say it explicitly.

Note: I'm gonna give this a try myself, but I didn't see anything about this on StackOverflow and thought it was an useful question to have answered.

Answers


The SyncLock statement uses Monitor.Enter() under the hood. The MSDN library says:

It is legal for the same thread to invoke Enter more than once without it blocking

Which means it is re-entrant. Easy to check btw.


Need Your Help

jquery with check box in asp.net adding click event

asp.net jquery

I have textbox and checkbox in asp.net page.

Read/Write from/to Hierarchical Object Store - SharePoint 2007

c# sharepoint sharepoint-2007 wss-3.0 sharepoint-timer-job

I've created a custom timer job which has requires some configurations to run. I'm trying to save the configurations as an SPPersistedObject in hierarchical object store.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.