Question
write another C++ program called avg_thread.cpp that calculates the average temperature in your living room over a period of time based on the data from
write another C++ program called avg_thread.cpp that calculates the average temperature
in your living room over a period of time based on the data from two IoT sensors.
However, this time make use of threads instead of processes. To
achieve this, implement the program using the POSIX Thread API. This API species
a set of interfaces (functions, header les) for threaded programming commonly known as
Pthreads. A single process can then contain multiple threads, all of which are executing
the same program. Assume that the specied text le is located in the same directory
as your program. The average temperature value should be formatted to two decimal
places.
Implementation
The following steps show how your program should work:
1. Your program should rst request the name of a temperature text file.
2. It then reads the input file line by line and stores the temperatures in a suitable
global data structure.
3. When the reading of the input file is complete, the program creates a new thread
with the pthread create() function.
4. The main thread then sums up the temperature data from sensor 1 in a global
variable.
5. At the same time, the new thread sums up the temperature data from sensor 2 in
the same global variable.
6. When the processing of the temperature data is complete, the program calculates
and prints the average temperature by dividing the global sum variable by the
total number of samples in the input file.
Important!
You have to use the specied C++ program names.
You will need to use the command line option -pthread with the g++ compiler
to successfully compile your program with the Pthread library.
You need to use a single globally dened sum variable.
Textfile provided:
temp_data.txt
LivingTemp1,17.8 LivingTemp2,17.9 LivingTemp1,18.1 LivingTemp2,18.2 LivingTemp1,18.5 LivingTemp2,18.6 LivingTemp1,19.0 LivingTemp2,19.0 LivingTemp1,19.5 LivingTemp2,19.6 LivingTemp1,20.0 LivingTemp2,20.1 LivingTemp1,20.6 LivingTemp2,20.6 LivingTemp1,19.8 LivingTemp2,19.8 LivingTemp1,19.4 LivingTemp2,19.5 LivingTemp1,19.0 LivingTemp2,19.1 LivingTemp1,18.5 LivingTemp2,18.6 LivingTemp1,18.0 LivingTemp2,18.1
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