Answered step by step
Verified Expert Solution
Link Copied!

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 Lock-Free Sorted Concurrent Linked List and retain the compare-and-swap. Use a worker-crew thread pool with C++11 fork/join multithreading. It should utilize 100,000 total list actions. All 100,000 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. 1) Node data between 0 and 99 inclusive. 2) An action of insertion (10% probability), removal (10% probability), lookup/retrieval (80% probability). After generating those two, the action is performed. For example, suppose a random number generator creates 37 and 1. The 37 is the data, and 1 can mean remove. The thread searches down the link list for a node containing 37, and upon finding one, attempts to delete it. If 37 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 allowed).Code: #include
#include
#include
#include
#include
#include
template
class LockFreeSortedList {
private:
struct Node {
T data;
std::atomic next;
Node(T val) : data(val), next(nullptr){}
};
std::atomic head;
public:
LockFreeSortedList() : head(nullptr){}
~LockFreeSortedList(){
Node* curr;
while ((curr = head.load())!= nullptr){
head.store(curr->next.load());
delete curr;
}
}
bool insert(T value){
Node* newNode = new Node(value);
newNode->next = nullptr;
Node* curr = head.load(std::memory_order_relaxed);
Node* prev = nullptr;
while (true){
if (!curr || curr->data >= value){
if (prev){
newNode->next = curr;
if (prev->next.compare_exchange_strong(curr, newNode)){
return true;
}
curr = prev->next.load(std::memory_order_relaxed);
} else {
newNode->next = head.load(std::memory_order_relaxed);
if (head.compare_exchange_strong(curr, newNode)){
return true;
}
curr = head.load(std::memory_order_relaxed);
}
} else {
prev = curr;
curr = curr->next.load(std::memory_order_relaxed);
}
}
}
bool remove(T value){
Node* curr = head.load(std::memory_order_relaxed);
Node* prev = nullptr;
while (curr && curr->data < value){
prev = curr;
curr = curr->next.load(std::memory_order_relaxed);
}
if (!curr || curr->data != value){
return false;
}
Node* next = curr->next.load(std::memory_order_relaxed);
if (prev){
if (!prev->next.compare_exchange_strong(curr, next)){
return false;
}
} else {
if (!head.compare_exchange_strong(curr, next)){
return false;
}
}
delete curr;
return true;
}
bool contains(T value){
Node* curr = head.load(std::memory_order_relaxed);
while (curr && curr->data < value){
curr = curr->next.load(std::memory_order_relaxed);
}
return curr && curr->data == value;
}
};
void performRandomOperation(LockFreeSortedList& list, int num_operations){
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0,99);
std::uniform_int_distribution<> actionDis(1,100);
for (int i =0; i < num_operations; ++i){
int data = dis(gen);
int action = actionDis(gen);
if (action <=10){
list.insert(data);
} else if (action >10 && action <=20){
list.remove(data);
} else {
list.contains(data);
}
}
}
int main(int argc, char* argv[]){
if (argc !=2){
std::cerr << "Usage: "<< argv[0]<<""<< std::endl;
return 1;
}
int num_threads = std::stoi(argv[1]);
int num_operations_per_thread =100000/ num_threads;
LockFreeSortedList list;
std::vector threads;
auto start = std::chrono::high_resolution_clock::now();
for (int i =0; i < num_threads; ++i){
threads.emplace_back(performRandomOperation, std::ref(list), num_operations_per_thread);
} for (auto& thread : thread : threads){
thread.join();
} auto end = std::chrono::high_resolution_clock::now();

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

=+What category does this metric represent?

Answered: 1 week ago