Windows Thread Synchronization 33






Using Singly Linked Lists Program Example


The following example uses the InitializeSListHead() function to initialize a singly linked list and the InterlockedPushEntrySList() function to insert 10 items. The example uses the InterlockedPopEntrySList() function to remove 10 items and the InterlockedFlushSList() function to verify that the list is empty.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.


Using Singly Linked Lists Program Example: Creating new Win32 console application project in Visual C++ .NET


Then, add the source file and give it a suitable name.


Using Singly Linked Lists Program Example: Adding new C++ source file


Next, add the following source code.


#include <windows.h>

#include <malloc.h>

#include <stdio.h>


// Structure to be used for a list item; the first member is the

// SLIST_ENTRY structure, and additional members are used for data.

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

typedef struct _PROGRAM_ITEM {

    SLIST_ENTRY ItemEntry;

    ULONG Signature;



int wmain( )


    ULONG Count;

    PSLIST_ENTRY pFirstEntry, pListEntry;

    PSLIST_HEADER pListHead;

    PPROGRAM_ITEM pProgramItem;


    // Initialize the list header.


    if( NULL == pListHead )


        wprintf(L"Memory allocation failed, error %d\n");

        return -1;



            wprintf(L"Memory was allocated, initializing the list header...\n");




    // Insert 10 items into the list.

    for( Count = 1; Count <= 10; Count += 1 )


        pProgramItem = (PPROGRAM_ITEM)_aligned_malloc(sizeof(PROGRAM_ITEM), MEMORY_ALLOCATION_ALIGNMENT);

        if( NULL == pProgramItem )


            wprintf(L"Memory allocation failed, error %d\n");

            return -1;



                  wprintf(L"Memory allocated, inserting item #%d\n", Count);


        pProgramItem->Signature = Count;

        pFirstEntry = InterlockedPushEntrySList(pListHead, &(pProgramItem->ItemEntry));



    // Remove 10 items from the list and display the signature.

      wprintf(L"\n\nRemoving the 10 items from the list & displaying the signature...\n");


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


        pListEntry = InterlockedPopEntrySList(pListHead);


        if( NULL == pListEntry )


            wprintf(L"List is empty...\n");

            return -1;



                  wprintf(L"InterlockedPopEntrySList() is OK, removing an item #%d...\n", Count);


        pProgramItem = (PPROGRAM_ITEM)pListEntry;

        wprintf(L"      Signature is %d\n", pProgramItem->Signature);


        // This example assumes that the SLIST_ENTRY structure is the

        // first member of the structure. If your structure does not

        // follow this convention, you must compute the starting address

        // of the structure before calling the free function.




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

    pListEntry = InterlockedFlushSList(pListHead);

    pFirstEntry = InterlockedPopEntrySList(pListHead);

    if (pFirstEntry != NULL)


        wprintf(L"Error %d: List is not empty...\n", GetLastError());

        return -1;



            wprintf(L"Flushing the list, the list is empty...\n");


      // Frees a block of memory that was allocated with  _aligned_malloc

      // or  _aligned_offset_malloc.



    return 1;



Build and run the project. The following screenshot is a sample output.


Using Singly Linked Lists Program Example: A sample console program output





< Thread Synchronization 32 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 34 >