Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
Use SinglyInterlockedQueue as a non-blocked shared queue instead of a non-blocked shared stack

Domanda Use SinglyInterlockedQueue as a non-blocked shared queue instead of a non-blocked shared stack

  • venerdì 13 gennaio 2012 15:09
     
     

    Hi, WIN32 Folks

    I modified and tested out the example of SinglyIntrelockedQueue in msdn. It is a non-blocked shared stack. What I want is a non-blocked shared queue. How can I achieve that using

    #include

    <windows.h>

    #include

    <malloc.h>

    #include

    <stdio.h>

    #include

    <process.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;

    } PROGRAM_ITEM, *PPROGRAM_ITEM;

    ULONG Count;

    PSLIST_ENTRY pFirstEntry, pListEntry;

    PSLIST_HEADER pListHead;

    PPROGRAM_ITEM pProgramItem;

    int

    Running;

    void

    static doWork(void * ptr)

    {

     

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

     

     

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

    {

    retry:

    pListEntry = InterlockedPopEntrySList(pListHead);

     

    if( NULL == pListEntry )

    {

    printf(

    "List is empty.\n");

     

    goto retry;

    }

     

    pProgramItem = (PPROGRAM_ITEM)pListEntry;

    printf(

    "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.

    _aligned_free(pListEntry);

    }

    Running = 0;

    }

     

    int

    main(int argc, char* argv[])

    {

    Count = atoi(argv[1]);

     

    // Initialize the list header to a MEMORY_ALLOCATION_ALIGNMENT boundary.

    pListHead = (PSLIST_HEADER)_aligned_malloc(

    sizeof(SLIST_HEADER),

    MEMORY_ALLOCATION_ALIGNMENT);

     

    if( NULL == pListHead )

    {

    printf(

    "Memory allocation failed.\n");

     

    return -1;

    }

    InitializeSListHead(pListHead);

    Running = 1;

    uintptr_t lcHandle = _beginthread(doWork, 1024*1024, NULL);

     

    // Insert 10 items into the list.

     

    for( int i= 1; i<=Count ; i += 1 )

    {

    pProgramItem = (PPROGRAM_ITEM)_aligned_malloc(

    sizeof(PROGRAM_ITEM),

    MEMORY_ALLOCATION_ALIGNMENT);

     

    if( NULL == pProgramItem )

    {

    printf(

    "Memory allocation failed.\n");

     

    return -1;

    }

    pProgramItem->Signature = i;

    pFirstEntry = InterlockedPushEntrySList(pListHead,

    &(pProgramItem->ItemEntry));

    }

     

     

    while (Running == 1)

    {

    Sleep(100);

    }

     

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

    pListEntry = InterlockedFlushSList(pListHead);

    pFirstEntry = InterlockedPopEntrySList(pListHead);

     

    if (pFirstEntry != NULL)

    {

    printf(

    "Error: List is not empty.\n");

     

    return -1;

    }

    _aligned_free(pListHead);

     

    return 1;

    }

     

     

    • Spostato Rahul V. Patil mercoledì 23 maggio 2012 20:48 Windows SDK related API (From:Parallel Computing in C++ and Native Code)
    •