Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8436627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:13:51+00:00 2026-06-10T07:13:51+00:00

I have been asked to implement a lock free queue in c using compare

  • 0

I have been asked to implement a lock free queue in c using compare and exchange, however my knowledge of pointers is fairly limited.

I have been using the following code to test my (as yet incomplete) dequeue implementation, but I believe it is looping infinitely because I’m not terribly sure how to properly use pointers/address of operator.

I have been given this CAS function to use, since I know nothing of assembler.

long __cdecl compare_exchange(long *flag, long oldvalue, long newvalue)
{
    __asm
    {
        mov ecx, flag
        mov eax, oldvalue
        mov ebx, newvalue
        lock cmpxchg [ecx], ebx
        jz iftrue
    }
    return 0;
    iftrue: return 1;
}

My current (relevant) code is as follows…

typedef struct QueueItem
{
    int data;
    struct QueueItem* next;
}item;

struct Queue
{
    item *head;
    item *tail;
}*queue;

int Dequeue()
{
    item *head;

    do
    {
        head = queue->head;
        if(head == NULL)
            return NULL_ITEM;
        printf("%d, %d, %d\n", (long *)queue->head, (long)&head, (long)&head->next);
    }
    while(!compare_exchange((long *)queue->head, (long)&head, (long)&head->next)); // Infinite loop.

    return head->data;
}

int main(int argc, char *argv[])
{
    item i, j;

    queue = (struct Queue *) malloc(sizeof(struct Queue));

    // Manually enqueue some data for testing dequeue.
    i.data = 5;
    j.data = 10;
    i.next = &j;
    j.next = NULL;

    queue->head = &i;

    printf("Dequeued: %d\n", Dequeue());
    printf("Dequeued: %d\n", Dequeue());
}

Should I be using the not operator inside of the do while loop? If I don’t use that operator, I get an output of “Dequeued 5” x2, which suggests the swap isn’t happening and that I should use the not. If so, where am I going wrong? I’d put money on it being a pointer/address operator issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:13:53+00:00Added an answer on June 10, 2026 at 7:13 am

    There is confusion on the pointers and values. Here is corrected code:

     do
     {
        head = queue->head;
        if(head == NULL)
            return 0;
        printf("%d, %d, %d %d\n", (long *)queue->head, (long)head, (long)head->next, head->data);
      }  while (!compare_exchange((long *)&queue->head,   (long)head, (long)head->next));
    

    You were trying to write what queue->head was pointing to, not the value of queue->head itself.

    In addition, to make it work properly on multicore, I believe you need to define the head as volatile.

    struct Queue
    {
       volatile item *head;
       item *tail;
    }*queue;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been asked to implement some email address validation on a web app
I have been asked by my supervisor to develop a web application using either
I have been asked to implement a javascript floodlight tag onto my site which
I have an old website and I've been asked to implement a small forum
I have been asked to implement a cross site login solution in Zend Framework.
for a programming assignment I have been asked to implement a solution to the
We're learning B-trees in class and have been asked to implement them in code.
I've been asked to implement what amounts to a license-dongle using TPM for an
We have been asked to implement a Red Black Tree in java, but not
I've been asked to implement a "C program with Classes". Does C have classes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.