You'll write a C++ program to demonstrate thread synchronization. Your main function should first create an empty file called *sync.txt*. Then it will create two separate threads: *Thread-A* and *Thread-B*. Both threads will open *sync.txt* and write to it simultaneously.   *Thread-A* will write the numbers 0 through 9, a total of fifty-thousand times in nested `for` loops, then exit. In other words, print the numbers 0 through 9 over and over again, with each run on a separate line, for 50,000 lines. Here's an example of one such line: ```text 0 1 2 3 4 5 6 7 8 9 ```   Somewhat similarly, *Thread-B* will write the letters A through Z fifty-thousand times in nested `for` loops, then exit. In other words, print the letters A through Z over and over again, with each run on a separate line, for 50,000 lines. Here's an example of one such line:   ```text A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ```   Do not write each line as one long string. You must write each character to the file, one by one (i.e., using multiple calls ). After writing a character to the file, write a space too, so the characters appear visually separated.   This means each thread would use a doubly-nested `for` loop. The outer loop would run 50,000 times (once per line), and the inner loop would run either 26 times (once per character) or 10 times (once per digit).   Hopefully, you understand this might create a race condition. Because *Thread-A* and *Thread-B* will both attempt to write individual characters to the same file at the same time, we might expect to see an undesirable mixing of the two types of lines, rather than each line remaining intact.   ## Solving With a Critical Section   We'll solve the previous dilemma with a *critical section* in our code. You will use a mutex lock to control synchronization between the two threads, such that each line of numbers or letters will render uninterrupted, even with multiple competing writes to the file. 1. We ***don't*** want to mix letters and numbers on a single line. Each line should only contain letters *or* numbers, but not both.   2. We ***do*** want line types to change sometimes. In other words, you might see a line of letters followed by a line of numbers. Or you may later see a line of numbers followed by a line of letters. You wouldn't want to see 100 lines of letters followed by 100 lines of numbers.   Try to make your *critical section* as small as possible while still solving the race condition for the above criteria.   ### Overview of beginWriting()   You'll quickly see the following prototype in your starter code:   ```cpp void beginWriting(bool use_mutexes); ```   At minimum, this function should perform the following steps:   1. Take the incoming argument `use_mutexes` and assign its value to the global variable `G_USE_MUTEXES`.   2. Create both threads.   3. Join both threads.   ### Overview of Thread Runner Functions   You will also see two prototypes for your thread runner functions:   ```cpp void* threadAEntry(void* param); void* threadBEntry(void* param); `` 1. Each individual line contains only letters *OR* numbers, but not both. 2. There should not be 100 lines of one type, followed by 100 lines of another type. Some lines should be of a different type than the last.   Here's an shortened example of what a successfully written file might look like (although yours may be slightly different):   ```text 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 ```   ## Confirming Failure   As stated above, no line should have a mix of letters *and* numbers. Here is an example that shows your *critical section* isn't working, or perhaps needs to cover a larger area:   ```text A 0 1 B C 2 3 4 D E 5 F 6 7 G 8 H 9 I J K L M N O P Q R S T U V W X Y Z ```   Also, you should not see a block of 50,000 lines of any particular type in a row. We expect to see the lines switch up a bit. Here's the general idea (some lines omitted and parenthetical added for explanation), which may indicate your *critical section* covers *too much* code.   ```text A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (letter lines repeat a total of 50,000 times) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 (number lines repeat a total of 50,000 times) ```

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
You'll write a C++ program to demonstrate thread synchronization. Your main function should first create an empty file called *sync.txt*. Then it will create two separate threads: *Thread-A* and *Thread-B*. Both threads will open *sync.txt* and write to it simultaneously.

 

*Thread-A* will write the numbers 0 through 9, a total of fifty-thousand times in nested `for` loops, then exit. In other words, print the numbers 0 through 9 over and over again, with each run on a separate line, for 50,000 lines. Here's an example of one such line:
```text
0 1 2 3 4 5 6 7 8 9
```

 

Somewhat similarly, *Thread-B* will write the letters A through Z fifty-thousand times in nested `for` loops, then exit. In other words, print the letters A through Z over and over again, with each run on a separate line, for 50,000 lines. Here's an example of one such line:

 

```text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
```

 

Do not write each line as one long string. You must write each character to the file, one by one (i.e., using multiple calls ). After writing a character to the file, write a space too, so the characters appear visually separated.

 

This means each thread would use a doubly-nested `for` loop. The outer loop would run 50,000 times (once per line), and the inner loop would run either 26 times (once per character) or 10 times (once per digit).

 

Hopefully, you understand this might create a race condition. Because *Thread-A* and *Thread-B* will both attempt to write individual characters to the same file at the same time, we might expect to see an undesirable mixing of the two types of lines, rather than each line remaining intact.

 

## Solving With a Critical Section

 

We'll solve the previous dilemma with a *critical section* in our code. You will use a mutex lock to control synchronization between the two threads, such that each line of numbers or letters will render uninterrupted, even with multiple competing writes to the file.
1. We ***don't*** want to mix letters and numbers on a single line. Each line should only contain letters *or* numbers, but not both.

 

2. We ***do*** want line types to change sometimes. In other words, you might see a line of letters followed by a line of numbers. Or you may later see a line of numbers followed by a line of letters. You wouldn't want to see 100 lines of letters followed by 100 lines of numbers.

 

Try to make your *critical section* as small as possible while still solving the race condition for the above criteria.

 

### Overview of beginWriting()

 

You'll quickly see the following prototype in your starter code:

 

```cpp
void beginWriting(bool use_mutexes);
```

 

At minimum, this function should perform the following steps:

 

1. Take the incoming argument `use_mutexes` and assign its value to the global variable `G_USE_MUTEXES`.

 

2. Create both threads.

 

3. Join both threads.

 

### Overview of Thread Runner Functions

 

You will also see two prototypes for your thread runner functions:

 

```cpp
void* threadAEntry(void* param);
void* threadBEntry(void* param);
``
1. Each individual line contains only letters *OR* numbers, but not both.
2. There should not be 100 lines of one type, followed by 100 lines of another type. Some lines should be of a different type than the last.

 

Here's an shortened example of what a successfully written file might look like (although yours may be slightly different):

 

```text
0 1 2 3 4 5 6 7 8 9
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
```

 

## Confirming Failure

 

As stated above, no line should have a mix of letters *and* numbers. Here is an example that shows your *critical section* isn't working, or perhaps needs to cover a larger area:

 

```text
A 0 1 B C 2 3 4 D E 5 F 6 7 G 8 H 9 I J K L M N O P Q R S T U V W X Y Z
```

 

Also, you should not see a block of 50,000 lines of any particular type in a row. We expect to see the lines switch up a bit. Here's the general idea (some lines omitted and parenthetical added for explanation), which may indicate your *critical section* covers *too much* code.

 

```text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
(letter lines repeat a total of 50,000 times)
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
(number lines repeat a total of 50,000 times)
```
 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education