Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Adjust the following C + + code to meet the requirements and avoid the segmentation error. Requirements: Write a Lock - Free Sorted Concurrent Linked
Adjust the following C code to meet the requirements and avoid the segmentation error. Requirements: Write a LockFree Sorted Concurrent Linked List and retain the compareandswap. Use a workercrew thread pool with C forkjoin multithreading. It should utilize total list actions. All iterations of
the parallel for loop should be simultaneously attempting to insert, read, or delete linked list items. For each iteration, a thread should randomly decide two items. Node data between and inclusive. An action of insertion probability removal probability lookupretrieval probability After generating those two, the action is performed. For example, suppose a random number generator creates and The is the data, and can mean remove. The thread searches down the link list for a node containing and upon finding one, attempts to delete it If is not found, then after the list is iterated, the action is considered complete. Additionally, upon inserting the list can contain duplicates of the same value. Upon removal, only remove the first discovered instance. Upon searching, only find the first discovered instance we are assuming duplicates are not allowedCode: #include
#include
#include
#include
#include
#include
template
class LockFreeSortedList
private:
struct Node
T data;
std::atomic next;
NodeT val : dataval nextnullptr
;
std::atomic head;
public:
LockFreeSortedList : headnullptr
~LockFreeSortedList
Node curr;
while curr head.load nullptr
head.storecurrnext.load;
delete curr;
bool insertT value
Node newNode new Nodevalue;
newNodenext nullptr;
Node curr head.loadstd::memoryorderrelaxed;
Node prev nullptr;
while true
if curr currdata value
if prev
newNodenext curr;
if prevnext.compareexchangestrongcurr newNode
return true;
curr prevnext.loadstd::memoryorderrelaxed;
else
newNodenext head.loadstd::memoryorderrelaxed;
if headcompareexchangestrongcurr newNode
return true;
curr head.loadstd::memoryorderrelaxed;
else
prev curr;
curr currnext.loadstd::memoryorderrelaxed;
bool removeT value
Node curr head.loadstd::memoryorderrelaxed;
Node prev nullptr;
while curr && currdata value
prev curr;
curr currnext.loadstd::memoryorderrelaxed;
if curr currdata value
return false;
Node next currnext.loadstd::memoryorderrelaxed;
if prev
if prevnext.compareexchangestrongcurr next
return false;
else
if head.compareexchangestrongcurr next
return false;
delete curr;
return true;
bool containsT value
Node curr head.loadstd::memoryorderrelaxed;
while curr && currdata value
curr currnext.loadstd::memoryorderrelaxed;
return curr && currdata value;
;
void performRandomOperationLockFreeSortedList& list, int numoperations
std::randomdevice rd;
std::mt genrd;
std::uniformintdistribution dis;
std::uniformintdistribution actionDis;
for int i ; i numoperations; i
int data disgen;
int action actionDisgen;
if action
list.insertdata;
else if action && action
list.removedata;
else
list.containsdata;
int mainint argc, char argv
if argc
std::cerr "Usage: argv std::endl;
return ;
int numthreads std::stoiargv;
int numoperationsperthread numthreads;
LockFreeSortedList list;
std::vector threads;
auto start std::chrono::highresolutionclock::now;
for int i ; i numthreads; i
threads.emplacebackperformRandomOperation std::reflist numoperationsperthread;
for auto& thread : thread : threads
thread.join;
auto end std::chrono::highresolutionclock::now;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started