SlideShare a Scribd company logo
1 of 8
Download to read offline
int dequeue() {
uint64_t cur_slot = front++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 0 || flag / 2 != order) {
//if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) {
this_thread::yield();
} else {
int ret = slot[cur_slot].key;
slot[cur_slot].flag++;
return ret;
}
}
}
Commented out if statement has potential hazard(unexpected behavior).

Queue will suffer infinite loop if slot[cur_slot].flag's value has been
modified by other dequeuing thread during the execution of if statement.



The full code can be found here
Cause of Possible Infinite Loop in Wait-free Queue
void enqueue(int key) {
uint64_t cur_slot = rear++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 1 || flag / 2 != order) {
this_thread::yield();
} else {
slot[cur_slot].key = key;
slot[cur_slot].flag++;
break;
}
}
}
data
■
flag 1
Q(0) Q(1) Q(2) Q(3) Q(4)
...
4
I will briefly demonstrate possible hazard scenario with 4 threads.

Assume all threads already called enqueue/dequeue function.

Their cur_slot value is showed in gray box.

Let's ignore all other function calls between them.

Current running thread & code section will be highlighted by red box.
int dequeue() {
uint64_t cur_slot = front++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 0 || flag / 2 != order) {
//if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) {
this_thread::yield();
} else {
int ret = slot[cur_slot].key;
slot[cur_slot].flag++;
return ret;
}
}
}
void enqueue(int key);
... ...
4 9 9Assume size = 5
enq(■) deq() enq(■) deq()
...
4
First thread successfully enqueued ■, now second & fourth thread enters
the if statement simultaneously.
int dequeue() {
uint64_t cur_slot = front++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 0 || flag / 2 != order) {
//if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) {
this_thread::yield();
} else {
int ret = slot[cur_slot].key;
slot[cur_slot].flag++;
return ret;
}
}
}
void enqueue(int key);
... ...
4 9 9
slot[cur_slot].flag
!= 0
must continue if
statement...
enq(■) deq() enq(■) deq()
data
■
flag 1
Q(0) Q(1) Q(2) Q(3) Q(4)
Assume size = 5
enq(■) deq() enq(■) deq()
...
4
int dequeue() {
uint64_t cur_slot = front++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 0 || flag / 2 != order) {
//if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) {
this_thread::yield();
} else {
int ret = slot[cur_slot].key;
slot[cur_slot].flag++;
return ret;
}
}
}
void enqueue(int key);
... ...
4 9 9
slot[cur_slot].flag
!= 0
must continue if
statement...
slot[cur_slot].flag
!= 0
must continue if
statement...
They both realized that `slot[cur_slot].flag == 0` is false,

so they need to check second part(slot[cur_slot].flag / 2 != order)
data
■
flag 1
Q(0) Q(1) Q(2) Q(3) Q(4)
Assume size = 5
...
4
second thread realized that `slot[cur_slot].flag / 2 != order` is false.
Because(obviously) both side has the value of 0.

Now it proceeds dequeuing(■).
int dequeue() {
uint64_t cur_slot = front++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 0 || flag / 2 != order) {
//if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) {
this_thread::yield();
} else {
int ret = slot[cur_slot].key;
slot[cur_slot].flag++;
return ret;
}
}
}
void enqueue(int key);
... ...
4
slot[cur_slot].flag /
2 != order is false!
enq(■) deq() enq(■) deq()
data
■
flag 1
Q(0) Q(1) Q(2) Q(3) Q(4)
Assume size = 5 9 9
...
After second thread's dequeue. Now the Q(4) is empty and waiting for the
enqueue. But instead of enqueue thread, fourth thread (which was yielded
after executing half of if statement) is scheduled in.
int dequeue() {
uint64_t cur_slot = front++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 0 || flag / 2 != order) {
//if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) {
this_thread::yield();
} else {
int ret = slot[cur_slot].key;
slot[cur_slot].flag++;
return ret;
}
}
}
void enqueue(int key);
... ...
enq(■) deq() enq(■) deq()
data
flag 2
Q(0) Q(1) Q(2) Q(3) Q(4)
Assume size = 5 4 4 9 9
...
Because second thread has increased flag value, unexpectedly

`slot[cur_slot].flag / 2 != order` returns false(value == 1).

Dequeue has been finished even before execution of proper matching
enqueue function call(■). Now fourth thread receives garbage value(■).
int dequeue() {
uint64_t cur_slot = front++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 0 || flag / 2 != order) {
//if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) {
this_thread::yield();
} else {
int ret = slot[cur_slot].key;
slot[cur_slot].flag++;
return ret;
}
}
}
void enqueue(int key);
... ...
Assume size = 5
enq(■) deq() enq(■) deq()
slot[cur_slot].flag /
2 != order is false!
data
flag 2
Q(0) Q(1) Q(2) Q(3) Q(4)
4 4 9 9
...
After two unwilling dequeue functions are done, third thread will never
proceed if statement. Because `flag % 2` will always return true.

Next dequeuing thread will also yield forever because of `flag / 2 != order`
will remain true forever.
... ...
enq(■) deq() enq(■) deq()
data
flag 3
Q(0) Q(1) Q(2) Q(3) Q(4)
int dequeue() {
uint64_t cur_slot = front++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 0 || flag / 2 != order) {
//if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) {
this_thread::yield();
} else {
int ret = slot[cur_slot].key;
slot[cur_slot].flag++;
return ret;
}
}
}
void enqueue(int key) {
uint64_t cur_slot = rear++;
int order = cur_slot / size;
cur_slot = cur_slot % size;
while (true) {
uint64_t flag = slot[cur_slot].flag;
if (flag % 2 == 1 || flag / 2 != order) {
this_thread::yield();
} else {
slot[cur_slot].key = key;
slot[cur_slot].flag++;
break;
}
}
}
deq()
14
flag % 2
== 1 is true!

yield!
flag / 2 != order

is true!

yield!
Assume size = 5 4 4 9 9

More Related Content

Recently uploaded

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 

Recently uploaded (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Cause of Infinite Loop in Wait-free Bounded Queue

  • 1. int dequeue() { uint64_t cur_slot = front++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 0 || flag / 2 != order) { //if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) { this_thread::yield(); } else { int ret = slot[cur_slot].key; slot[cur_slot].flag++; return ret; } } } Commented out if statement has potential hazard(unexpected behavior). Queue will suffer infinite loop if slot[cur_slot].flag's value has been modified by other dequeuing thread during the execution of if statement. 
 The full code can be found here Cause of Possible Infinite Loop in Wait-free Queue void enqueue(int key) { uint64_t cur_slot = rear++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 1 || flag / 2 != order) { this_thread::yield(); } else { slot[cur_slot].key = key; slot[cur_slot].flag++; break; } } }
  • 2. data ■ flag 1 Q(0) Q(1) Q(2) Q(3) Q(4) ... 4 I will briefly demonstrate possible hazard scenario with 4 threads. Assume all threads already called enqueue/dequeue function.
 Their cur_slot value is showed in gray box.
 Let's ignore all other function calls between them.
 Current running thread & code section will be highlighted by red box. int dequeue() { uint64_t cur_slot = front++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 0 || flag / 2 != order) { //if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) { this_thread::yield(); } else { int ret = slot[cur_slot].key; slot[cur_slot].flag++; return ret; } } } void enqueue(int key); ... ... 4 9 9Assume size = 5 enq(■) deq() enq(■) deq()
  • 3. ... 4 First thread successfully enqueued ■, now second & fourth thread enters the if statement simultaneously. int dequeue() { uint64_t cur_slot = front++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 0 || flag / 2 != order) { //if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) { this_thread::yield(); } else { int ret = slot[cur_slot].key; slot[cur_slot].flag++; return ret; } } } void enqueue(int key); ... ... 4 9 9 slot[cur_slot].flag != 0 must continue if statement... enq(■) deq() enq(■) deq() data ■ flag 1 Q(0) Q(1) Q(2) Q(3) Q(4) Assume size = 5
  • 4. enq(■) deq() enq(■) deq() ... 4 int dequeue() { uint64_t cur_slot = front++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 0 || flag / 2 != order) { //if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) { this_thread::yield(); } else { int ret = slot[cur_slot].key; slot[cur_slot].flag++; return ret; } } } void enqueue(int key); ... ... 4 9 9 slot[cur_slot].flag != 0 must continue if statement... slot[cur_slot].flag != 0 must continue if statement... They both realized that `slot[cur_slot].flag == 0` is false,
 so they need to check second part(slot[cur_slot].flag / 2 != order) data ■ flag 1 Q(0) Q(1) Q(2) Q(3) Q(4) Assume size = 5
  • 5. ... 4 second thread realized that `slot[cur_slot].flag / 2 != order` is false. Because(obviously) both side has the value of 0. Now it proceeds dequeuing(■). int dequeue() { uint64_t cur_slot = front++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 0 || flag / 2 != order) { //if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) { this_thread::yield(); } else { int ret = slot[cur_slot].key; slot[cur_slot].flag++; return ret; } } } void enqueue(int key); ... ... 4 slot[cur_slot].flag / 2 != order is false! enq(■) deq() enq(■) deq() data ■ flag 1 Q(0) Q(1) Q(2) Q(3) Q(4) Assume size = 5 9 9
  • 6. ... After second thread's dequeue. Now the Q(4) is empty and waiting for the enqueue. But instead of enqueue thread, fourth thread (which was yielded after executing half of if statement) is scheduled in. int dequeue() { uint64_t cur_slot = front++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 0 || flag / 2 != order) { //if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) { this_thread::yield(); } else { int ret = slot[cur_slot].key; slot[cur_slot].flag++; return ret; } } } void enqueue(int key); ... ... enq(■) deq() enq(■) deq() data flag 2 Q(0) Q(1) Q(2) Q(3) Q(4) Assume size = 5 4 4 9 9
  • 7. ... Because second thread has increased flag value, unexpectedly `slot[cur_slot].flag / 2 != order` returns false(value == 1). Dequeue has been finished even before execution of proper matching enqueue function call(■). Now fourth thread receives garbage value(■). int dequeue() { uint64_t cur_slot = front++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 0 || flag / 2 != order) { //if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) { this_thread::yield(); } else { int ret = slot[cur_slot].key; slot[cur_slot].flag++; return ret; } } } void enqueue(int key); ... ... Assume size = 5 enq(■) deq() enq(■) deq() slot[cur_slot].flag / 2 != order is false! data flag 2 Q(0) Q(1) Q(2) Q(3) Q(4) 4 4 9 9
  • 8. ... After two unwilling dequeue functions are done, third thread will never proceed if statement. Because `flag % 2` will always return true. Next dequeuing thread will also yield forever because of `flag / 2 != order` will remain true forever. ... ... enq(■) deq() enq(■) deq() data flag 3 Q(0) Q(1) Q(2) Q(3) Q(4) int dequeue() { uint64_t cur_slot = front++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 0 || flag / 2 != order) { //if (slot[cur_slot].flag % 2 == 0 || slot[cur_slot].flag / 2 != order) { this_thread::yield(); } else { int ret = slot[cur_slot].key; slot[cur_slot].flag++; return ret; } } } void enqueue(int key) { uint64_t cur_slot = rear++; int order = cur_slot / size; cur_slot = cur_slot % size; while (true) { uint64_t flag = slot[cur_slot].flag; if (flag % 2 == 1 || flag / 2 != order) { this_thread::yield(); } else { slot[cur_slot].key = key; slot[cur_slot].flag++; break; } } } deq() 14 flag % 2 == 1 is true!
 yield! flag / 2 != order
 is true!
 yield! Assume size = 5 4 4 9 9