Channels ▼
RSS

Parallel

Deadlock-Proof Your Code: Part 1


Listing 1 is example code for Windows that successfully deadlocks. It would deadlock over and over, recursively, if it didn't hang from a deadlock at the outset of its recursion.


/*  Listing 1: Deadlock demonstration  */

#include <windows.h>
#include <stdio.h>

// Constants
const long  kSuccess = 0;
const long  kFailure = 1;
const long  kTextAreaSize = 100;
const short kNumThreads = 2;

// Globals
CRITICAL_SECTION L1;
CRITICAL_SECTION L2;
int iSharedIntegerCounter = 0x61;			// ASCII 'a'
char szSharedTextBase[kTextAreaSize];
char *pSharedText = szSharedTextBase;

// Function prototypes
DWORD WINAPI FuncA(void *vp);
DWORD WINAPI FuncB(void *vp);

// The following code deadlocks.
long main(long argc, char **argv)
{
	long status = kSuccess;

	HANDLE hThreads[kNumThreads];
	DWORD TID1, TID2;

	InitializeCriticalSection(&L1);
	InitializeCriticalSection(&L2);

	// Create two threads.
	hThreads[0] = CreateThread(NULL, 
					0, 
					&FuncA, 
					(void *)1, 
					0, 
					&TID2);
	if (hThreads[0] == NULL) 
	{
		printf("Error creating thread.\n");
		status = kFailure;
	}
	else
	{
		hThreads[1] = CreateThread(NULL, 
					0,
					&FuncB, 
					(void *)1, 
					0, 
					&TID2);
		if (hThreads[1] == NULL) 
		{
			printf("Error creating thread.\n");
			status = kFailure;
		}
	}

	if (status == kSuccess)
	{
		DWORD wfmo =
		     WaitForMultipleObjects(kNumThreads, hThreads, TRUE, INFINITE);
		int LastError = GetLastError();

		DeleteCriticalSection(&L1);
		DeleteCriticalSection(&L2);

		// Close the thread handles.
		CloseHandle(hThreads[0]);
		CloseHandle(hThreads[1]);
	}

	// Add a NULL terminator to our 'a' - 'z' string, and display it.
	*pSharedText = '\0';
	printf("%s\n", szSharedTextBase);
	return(status);	
}

DWORD WINAPI FuncA(void *vp)
{
	static int count = 13;
	EnterCriticalSection(&L1);

	if (count-- > 0)
	{
		*(pSharedText++) = iSharedIntegerCounter++;
		FuncB(vp);
		LeaveCriticalSection(&L1);
	}
	ExitThread(0);
	return(0);
}

DWORD WINAPI FuncB(void *vp)
{
	static int count = 13;
	EnterCriticalSection(&L2);

	if (count-- > 0)
	{
		*(pSharedText++) = iSharedIntegerCounter++;
		FuncA(vp);
		LeaveCriticalSection(&L2);
	}
	ExitThread(0);
	return(0);
}

Listing 1: Deadlock demonstration


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video