"Erroneous Locking Granularity"

private static readonly object wordCountCalculatorSyncObj = new object();

private static void ThreadDoWork() {
	bool atLeastOneWordRemaining;
	int thisWordIndex = currentWordIndex;
	lock (wordCountCalculatorSyncObj) {
		currentWordIndex = currentWordIndex + 1;
	}
	if (thisWordIndex >= wordList.Count) atLeastOneWordRemaining = false;
	else atLeastOneWordRemaining = true;

	while (atLeastOneWordRemaining) {
		string thisWord = wordList[thisWordIndex];
		lock (wordCountCalculatorSyncObj) {
			bool firstOccurrenceOfWord = !wordCountDict.ContainsKey(thisWord);
		}

		if (curseWords.Contains(thisWord)) Console.WriteLine("Curse word detected!");
		
		if (firstOccurrenceOfWord) wordCountDict.Add(thisWord, 1);
		else {
			lock (wordCountCalculatorSyncObj) {
				wordCountDict[thisWord] = wordCountDict[thisWord] + 1;
			}
		}
		

		thisWordIndex = currentWordIndex;
		lock (wordCountCalculatorSyncObj) {
			currentWordIndex = currentWordIndex + 1;
		}
		if (thisWordIndex >= wordList.Count) atLeastOneWordRemaining = false;
	}
}


Code snippet taken from "Common Multithreading Mistakes in C# - I: Incorrect Granularity".