C++ Program to Show Thread Interface and Memory Consistency Errors Last Updated : 29 Jul, 2022 Comments Improve Suggest changes Like Article Like Report C++ allows Multithreading by using the 'thread' header file. The program acts as one thread but to increase program execution time/performance we can use threads to run parts of the program concurrently. But it may lead to issues of memory consistency errors and may not give us the proper output. Threads are used to improve the performance of applications by running processes parallel to each other. The thread may share the same resource or reference pointer. Two or more threads may refer to the same object or share some common resource and they try to update or make changes independently on shared resource data which can leave data inconsistent. Example: In the below C++ program, two threads are used to use the same functions. First, it should run for thread 1 and then for thread 2. But to show memory consistency for sharing the same resource/function output is not consistent. C++14 // C++ program to show // memory consistency #include <bits/stdc++.h> #include <thread> using namespace std; class thread_obj { public: void operator()(int x) { for (int i = 0; i < 50; i++) { cout << "Thread " << x << "\n"; } } }; // Driver code int main() { // thread 1 thread th1(thread_obj(), 1); // thread 2 thread th2(thread_obj(), 2); // wait for thread1 to join th1.join(); // wait for thread2 to join th2.join(); return 0; } Output - You can see inconsistency in output (It is machine Dependent) Example 2: In the below C++ program, an attempt will be made to access the same value from different threads as one can see memory consistency errors as both threads will run concurrently. C++ // C++ program to show memory // consistency error #include <iostream> #include <thread> using namespace std; int x = 100; void thread_function() { for(int i = 0; i < 50; i++) {Â x--; cout << "Thread 1 " << x << "\n"; } } // Driver code int main() { std::thread t(&thread_function); for(int i = 0;i < 100; i++) { x++; cout << "main thread " << x << "\n"; } return 0; } Output - You can see inconsistency in output (It is machine Dependent) Comment More infoAdvertise with us Next Article C++ Program to Show Thread Interface and Memory Consistency Errors S sugam0519 Follow Improve Article Tags : C++ Programs C++ Practice Tags : CPP Similar Reads C++ Program to Show Types of Errors In any programming language errors is common. If we miss any syntax like parenthesis or semicolon then we get syntax errors. Apart from this we also get run time errors during the execution of code. In a similar way the errors are classified as below: Syntax Errors Runtime Errors Logical Errors Link 3 min read Reasons for a C++ program crash We sometimes come across abnormal crash of C++ programs. Below are some possible reasons which may cause C++ to crash abnormally. Segmentation Fault: It is the major reason for program to crash. These are may be the reasons for the such cause:Attempting to access memory location that doesn't exist i 3 min read How to Create a Thread in C++? A thread is a basic element of multithreading which represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to create a thread in C++. How to Create a Thread in C++?In C++, the std::thread is a class template that is used t 2 min read How to Join a Thread in C++? In C++, a thread is a basic element of multithreading that represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to join a thread in C++. How to Join a Thread in C++?Joining a thread is a means to wait for the thread to c 2 min read C++ Program to Show Runtime Exceptions A runtime error occurs while the program is running. Because this is not a compilation error, the compilation will be completed successfully. Here, we will learn how to handle runtime exceptions in C++. There are 5 types of runtime exceptions discussed here: Division by zero. Segmentation faults. La 3 min read Like