Question

In: Computer Science

Run the following code and explain what the code does Run this code at least twice...

  1. Run the following code and explain what the code does

  • Run this code at least twice and take screenshots
  • Explain what the code does
  • What is the running result supposed to be
  • What caused this issue?

#include <thread>

#include <iostream>

using namespace std;

const unsigned int NTHREADS = 20;

const int ITERS = 10000000;

int counter;

void increment()

{

       for (int i = 0; i < ITERS; i++)

                     counter++;

             

}

void decrement()

{

       for (int i = 0; i<ITERS; i++)

                     counter--;

             

}

int main()

{

       cout << "The counter is " << counter << endl;

      

              thread *threads = new thread[NTHREADS];

      

              for (unsigned int i = 0; i<NTHREADS; i++)

              if (i % 2 == 0)

              threads[i] = thread(increment);

       else

              threads[i] = thread(decrement);

      

              for (unsigned int i = 0; i<NTHREADS; i++)

              threads[i].join();

      

              cout << "The counter is " << counter << endl;

      

              return 0;

}

  1. Mutex

A mutex (mutual exlusion) allows us to encapsulate blocks of code that should only be executed in one thread at a time.

Apply mutex to solve the issue in the previous code. Show your revised code and running result.

Solutions

Expert Solution


Related Solutions

Run the following Java code and explain what it does. Show the image created by the...
Run the following Java code and explain what it does. Show the image created by the code. public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g;...
Run the following RISC-V codes separately and explain what each code does and how you interpret...
Run the following RISC-V codes separately and explain what each code does and how you interpret the register results: Code 1. addi x3, x3, 1 slli x3, x3, 62 addi x4, x4, 7 mul x5, x4, x3 mulh x6, x4, x3 Code 2. addi x3, x3, 1 slli x3, x3, 63 addi x4, x4, 1 mul x5, x4, x3 mulhsu x6, x4, x3 Code 3. addi x3, x3, 1 slli x3, x3, 63 addi x4, x4, 1 mul x5, x4,...
The following code will generate a Decision Tree. You need to run the code and explain...
The following code will generate a Decision Tree. You need to run the code and explain the tree. After you get the Tree. You need to explain how does it draw like that. install.packages("rpart.plot") # install package rpart.plot ########################################## # section 7.1.1 Overview of a Decision Tree ########################################## library("rpart") library("rpart.plot") # Read the data setwd("c:/data/") banktrain <- read.table("bank-sample-test.csv",header=TRUE,sep=",") ## drop a few columns to simplify the tree drops<-c("age", "balance", "day", "campaign", "pdays", "previous", "month") banktrain <- banktrain [,!(names(banktrain) %in% drops)]...
Run the following code and explain the results (expectation of the array). Test it on the...
Run the following code and explain the results (expectation of the array). Test it on the following arrays: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] [1.0, 4.0, 6.0,2.0 , 3.0, 5.0] [5.0, 3.0, 2.0, 6.0, 4.0, 1.0] [6.0, 5.0, 4.0, 3.0, 2.0, 1.0] // Java code to calculate expected // value of an array import java.io.*; class GFG { // Function to calculate expectation static float calc_Expectation(float a[], float n) { // Variable prb is for probability of each // element...
What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE...
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE teo[                                 <ELEMENT teo((stations| databases)+)>                                 <ELEMENT stations(stationName stationLocation sensors*)> <ELEMENT databases(databaseName databaseType )> <ELEMENT stationName(#PCDATA)> <ELEMENT stationLocation(#PCDATA)> <ELEMENT sensors (sensorName phenomenon)+> <ELEMENT sensorName(#PCDATA)> <ELEMENT phenomenon(#PCDATA)> <ELEMENT databaseName(#PCDATA)> <ELEMENT databaseType(#PCDATA)> <!ATTLIST stations boundingBox CDATA #required> ]>
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
Sample Code and Models Run each of the models below and explain the code function and...
Sample Code and Models Run each of the models below and explain the code function and your findings for each system, do they agree/disagree with what you understand and why ?? Matlab Code % Winter 2018 Control Engineering % Lab No.3 - Root Locus problems % Mark Clarke clear s = tf('s') K = 1150; %Proportional Controller Gain, May need to be altered? % Enter Model 1 % This is a model of a simple 2nd order with no zeros...
(C++) Why does my code not continue looping? it only loops twice. Can someone take a...
(C++) Why does my code not continue looping? it only loops twice. Can someone take a look at my code so far: #include <iostream> #include <cmath> #include <cstdlib> #include <string> #include <ctime> using namespace std; void cls(void); void cls(void) { system("cls||clear"); return; } int main() { int BankBalance = 0; char quit; int wager = 0; int inputWager = 0; int sum = 0; int diceRoll = 0; int rollPoint = 0; int point = 0; int dice1 = 0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT