In: Computer Science
Create a simple C++ application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0. For your created code, provide a detailed analysis of appropriate concepts that could impact your application. Specifically, address:
Performance issues with concurrency
Vulnerabilities exhibited with use of strings
Security of the data types exhibited.
Points to consider:
Code
#include
#include
using namespace std;
int count = 0;
void countDown()
{
while(count > 0)
{
count--;
cout<<"Count Down:
"<
}
void countUp()
{
while(count < 20)
{
count++;
cout<<"Count Up:
"<
}
int main()
{
thread t1(countUp);
thread t2(countDown);
t1.join();
t2.join();
}
1. Performance issues with concurrency.
With the concurrency, there could be sometimes a reduction in
performance because there could be multiple processes that we are
trying to execute on the same core, there are so many context
switches involved. Thus there could be a reduction in the speed of
the task.
2. Vulnerabilities exhibited with the use of strings
As we can append any length of characters to the strings, thus there is a vulnerability with the strings. Thus we should define the strings with a specific number of characters. So that there will be no append of characters to the string.
3. As the data can be used by multiple threads in the case of multithreading thus there is a need to make sure that the data type is not changed by any of the function and other function who is using the same data type is considering it as other data type.