Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 18, Problem 11PP
Program Plan Intro
Thread counting
Program Plan:
- Include required header file for thread and chrono seconds.
- Define function “countingThread” with two arguments stop value pointer and count value pointer.
- Performs “while” loop. This loop performs upto the stop value.
- User need to wait one second using “chrono” seconds.
- Increment the count value pointer
- If the count value is equal to “0”, then display count value.
- Performs “while” loop. This loop performs upto the stop value.
- Define main function.
- Initializes the stop value in “bool” type.
- Initializes the count to “1”.
- Generate thread with counting thread function, stop value and count.
- Initializes the number to “0”.
- Performs “do-while” loop. This loop will execute upto, the number is less than count
- Read the number from user.
- Assign stop count value to “true”.
- Wait for thread to complete by using “join” function.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Implement a solution to the critical section problem with threads using semaphores. you must add a third counting thread which counts by 1 each time it enters its critical section to 3,000,000. Each counts to 3,000,000 for a total of 9,000,000.
In this assignment, you are provided with working code that does the following: 1. You input a sentence (containing no more than 50 characters). 2. The program will read the sentence and put it into an array of characters. 3. Then, it creates one thread for each character in the sentence. 4. The goal of the program is to capitalize each letter that has an odd index. The given program actually does this, but lacks the synchronization of the threads, so the output is not correct. You will need to provide the synchronization using mutex locks. Specifically, you are to (1) declare the mutex lock, (2) initialize the mutex lock, (3) lock and unlock the mutex lock at an appropriate location that results in the code working as expected, and (4) destroy the mutex lock. Be sure to place the mutex locks so that your program works correctly every time. Do not remove code or functions – you are to add the synchronization pieces only. When compiling using the GNU C compiler, be sure to include the…
Design a food ordering system where your python program will run two threads,
Place Order: This thread will be placing an order and inserting that into a queue. This thread places new order every 0.5 second. (hint: use time.sleep(0.5) function)
Serve Order: This thread will serve the order. All you need to do is pop the order out of the queue and print it. This thread serves an order every 2 seconds. Also start this thread 1 second after place order thread is started.
Pass following list as an argument to place order thread,
orders = ['pizza','samosa','pasta','biryani','burger']
This problem is a producer,consumer problem where place_order thread is producing orders whereas serve_order thread is consuming the food orders. Use Queue class.
Chapter 18 Solutions
Problem Solving with C++ (10th Edition)
Ch. 18.1 - If v is a vector, what does v.begin() return? What...Ch. 18.1 - If p is an iterator for a vector object v, what is...Ch. 18.1 - Suppose v is a vector of ints. Write a for loop...Ch. 18.1 - Suppose the vector v contains the letters 'A',...Ch. 18.1 - Suppose the vector v contains the letters 'A',...Ch. 18.1 - Suppose you want to run the following code, where...Ch. 18.2 - Prob. 7STECh. 18.2 - Prob. 8STECh. 18.2 - Prob. 9STECh. 18.2 - Prob. 10STE
Ch. 18.2 - Prob. 11STECh. 18.2 - Prob. 12STECh. 18.2 - Prob. 13STECh. 18.2 - Prob. 14STECh. 18.2 - Prob. 15STECh. 18.2 - Prob. 16STECh. 18.3 - Prob. 17STECh. 18.3 - Prob. 18STECh. 18.3 - Prob. 19STECh. 18.3 - Suppose v is an object of the class vectorint. Use...Ch. 18.3 - Prob. 21STECh. 18.3 - Can you use the copy template function with vector...Ch. 18.3 - Prob. 23STECh. 18 - Prob. 1PCh. 18 - Prob. 2PCh. 18 - Prob. 3PCh. 18 - Prob. 4PCh. 18 - Write a program that allows the user to enter any...Ch. 18 - Prob. 3PPCh. 18 - Prob. 5PPCh. 18 - Solution to Programming Project 18.6 In this...Ch. 18 - Prob. 7PPCh. 18 - You have collected a file of movie ratings where...Ch. 18 - Prob. 9PPCh. 18 - Prob. 11PPCh. 18 - Write a program that uses regular expressions to...
Knowledge Booster
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
- 10. Write a multi-threading JAVA program having four threads:- The “Generator” thread: it takes as argument (in the constructor) aninteger N and generates an array of N integers- The “Even” thread: it takes as argument (in the constructor) an array ofintegers and counts the number of even numbers in the array- The “Odd” thread: it takes as argument (in the constructor) an array ofintegers and count the number of odd numbers in the array- The main thread:o it asks the user to enter an integer No Then, it runs the “Generator” thread to create an array of integersand waits the end of “Generator”o Then, it run “Even” and “Odd” to count the number of even andodd numbers in the generated arrayo Finally, it prints if there is more even or odd numbersNote: the main thread should wait the end of “Even” and “Odd”threads before printing the final result.arrow_forwardCreate an application in Java that creates n (this is set at the beginning of the application not read) threads in java. The i-th thread will be named "TXAM-i". Each thread job is to insert its name 3 times into an existing ArrayList stored in the Values class given below. Create the thread class and modify the below Values class such that we ensure that the values inserted by each thread are consecutive in the array. For example, if n=3 after running each thread the array may look as: ТХАМ-2, ТХАМ-2, ТХАМ-2, ТХАМ-1, ТХАМ-1, ТХАМ-1, ТХАМ-3, ТХАМ-3, ТХАМ-3 But will never look like (values are mixed): ΤΧΑΜ-2, ΤΧAM-3, ΤΧΑΜ-1, ΤΧΑM-2, TΧΑM-3, TΧΑM-1, ΤΧAM-2, TΧΑM- 1, TXAM-3 private class Values { private ArrayList deposit; public Values () { deposit = new ArrayList(); } }arrow_forwardFor programming tasks, we can use an Array or an Array List. Describe when using an Array would be a good choice, and Describe when using an Array List would be a better choice. You must start a thread before you can read and reply to other threadsarrow_forward
- This is a program in Javaarrow_forwardThis code is a simple example of how threads can be used to speed up a * program in a multi-core system. Unfortunately it doesn't function properly. * The function run on thread creation wasn't finished, and thus the program * doesn't produce any results. Finish the function calculate_sum so that the * sum is correctly calculated. #Solve it in C language. #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #define NUMTHREADS 2 struct thread_data { int *valuelist; int num_values; int sum; }; void *calculate_sum(void *); int main(void) { int values[] = {1, 3, 5, 7, 9, 2, 6, 7, 0, 10, 11, 12, 13, 14, 15, 40, 50, 60, 61, 62}; int sum = 0; int *iter = values; pthread_t tids[NUMTHREADS]; /* Calculate how much data each thread will work on */ size_t num_values; num_values = sizeof(values) / sizeof(*values); int num_values_per_thread = num_values / NUMTHREADS; /** Standard C doesn't have variable length arrays. To allocate a dynamic *…arrow_forwardThe following question is related to Threading Task-1: Write a c program that creates 5 threads and prints which thread is running and after the thread is closed, a new thread starts its execution. Each thread should run sequentially one by one. OUTPUT: thread-1 running thread-1 closed thread-2 running thread-2 closedarrow_forward
- Write a multithreaded C++, Pthread program that outputs prime numbers <= N. This program should work as follows: the user will run the program and will enter a number N on the command line. The program will then create a separate thread that outputs all the prime numbers less than or equal to N specified in the command-line argument. Please comment every line in the code with its funcionalityarrow_forwardThe Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: fib0 = 0 fib1 = 1 fibn = fibn−1 + fibn−2 Write a multithreaded program that generates the Fibonacci series using either the Java, Pthreads, or Win32 thread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in data that is shared by the threads (an array is probably the most convenient data structure).When the thread finishes execution, the parent thread will output the sequence generated by the child thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finishes, this will require having the parent thread wait for the child thread to finish.arrow_forwardWrite a multithreaded C++, Pthread program that outputs prime numbers <= N. This program should work as follows: the user will run the program and will enter a number N on the command line. The program will then create a separate thread that outputs all the prime numbers less than or equal to N specified in the command-line argument.arrow_forward
- Subject Name: Advanced Object-Oriented Programming 1. Complete the below given code (1) class NyThread Thread t: Runnable ( MyThread (String n, int pl t (2) (3) : / creates teh Thread t with nane n : // changen the priority to p Syster.out.printin ("Thread "+n+* created with priority "+p): t. (4) : // exexcute the thread t. I // The only abatarct aethed in Runnable public for (int 1=10: 1>=1: 1--) { Syaten.out.prántin ("Thread *t.getName ()+"\e i - "a) : (5) try { (6) :/ ma ke the Thead t eleep to 2 seconds } catch (Exception e) Syater.out.printin ("Exception in Child"): System.out.printin ("Thread "+t. getName ()+" 1s Exiting"): public statie void main (Stringt) arga) { MyThread t1, 12; ti - new MyThread ("R1", 7): t2 = new MyThread ("R1", 3): try ( :// 2 atatements to aake the Threads ti a v2 exit at same time (8) ) catoh (Exception e) ( Syster.out.printin("Exception in main"): System.out.printin ("Thread Main is Exiting"): 1. 2. 3. 4. 6. 7. 8. 5.arrow_forwardThe following question is related to Threading in C programming Task-3: Write a program in c that has a function that takes the name of the user and adds all the ASCII value of the characters and returns it. Now create 3 threads that run the function using 3 different user names. Now print “Youreka” if all the returned values are equal, print “Miracle” if the 2 returned values are equal, and print “Hasta la vista” if the values don’t match using another thread.arrow_forwardIn C++ This exercise involves implementing the solution to the bounded buffer/readers and writers problem using threads. You will create a program that accepts four command line arguments: run_time (the length of time the program should run) buffer_size (number of slots in the bounded buffer) num_producers (number of producer threads) num_consumers (number of consumer threads) The program will create a thread for each producer and consumer. As each thread produces or consumes a data item, it will print its status. Example Output Here are some sample runs: Not enough arguments If not enough arguments are provided, the program should print an error message and exit. $ ./bounded_buffer Wrong number of arguments. Usage: bounded_buffer <run_time> <buffer_size> <num_producers> <num_consumers> $ Invalid argument If an argument is less than or equal to 0, the program should print a message and exit $ ./bounded_buffer 10 5 1 0 ./bounded_buffer 10 5 1 0 num_consumers…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education