3
\$\begingroup\$

Every call to the web service goes through a custom queuing system. This has a limit that is set to 1. Every call that enters the same time with another call is purged and not handled. Not sure if this is completely thread safe.

public void QueueForExecution(T value)
{
 lock (_queueLock)
 {
 if (_entryCount < _queueLimit)
 {
 Interlocked.Increment(ref _entryCount);
 Logger.Trace(String.Format("Running task queue item for {0}.", _methodName));
 Task.Run(() => RunTask(value));
 }
 else
 {
 lock (_queueItemLock)
 {
 _queuedExecutionValue = value;
 }
 Logger.Trace(String.Format("Task queue limit reached for {0} - queued request - overriding any previous queued items.", _methodName));
 }
 }
}
Mast
13.8k12 gold badges57 silver badges127 bronze badges
asked Dec 14, 2015 at 9:21
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

If any locking on _queueItemLock is happening inside a lock on _queueLock then you can consider this as thread safe.

If any access of _entryCount is done in a lock on _queueLock then you don't need the Interlocked.Increment(ref _entryCount); but you can just increment in the normal way.

If you by any chance are using C# 6.0 (VS 2015) you can use string interpolation by using the $ operator without the need of calling string.Format() like so

Logger.Trace($"Running task queue item for {_methodName}."); 

other than that there isn't much to say.

answered Dec 14, 2015 at 10:37
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.