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));
        }
    }
}
\$\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.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.