Interlocking a list Win32 C code example




// For WinXp as a target, change appropriately

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>

#include <malloc.h>


// Structure to be used for a list item. Typically, the first member is of type SLIST_ENTRY. Additional members are used for data.

// Here, the data is simply a signature for testing purposes.

typedef struct _PROGRAM_ITEM {


ULONG Signature;



int main(void)


ULONG Count;

PSLIST_ENTRY FirstEntry, ListEntry;




// Initialize the list header.



// Insert 10 items into the list.

for (Count = 1; Count <10>Signature = Count;

FirstEntry = InterlockedPushEntrySList(&ListHead, &ProgramItem->ItemEntry);



// Remove 10 items from the list.

for (Count = 10; Count >= 1; Count -= 1)


ListEntry = InterlockedPopEntrySList(&ListHead);




// Flush the list and verify that the items are gone.

ListEntry = InterlockedFlushSList(&ListHead);

FirstEntry = InterlockedPopEntrySList(&ListHead);

if (FirstEntry != NULL)


printf("InterlockedPopEntrySList() failed, error: List is not empty.");



printf("InterlockedPopEntrySList() is OK, list is empty.\n");

return 0;



Output example:


InterlockedPopEntrySList() is OK, list is empty.

Press any key to continue . . .



Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Interlocking a list Win32 C program example

To show: The Windows process and thread related functions usage in Win32 system programming



C and C++ Programming Resources | C & C++ Code Example Index