In: Computer Science
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line.
For example: prompt$: ./vowcon Operating Systems Class at CSUN
The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can also send the phrase or its parts to the threads. The threads should print only the words of the phrase as follows:
The vow thread function should print all the words that start with a vowel
The con thread function should print all the words starting with a consonant.
Note that the main thread (running main()) should not print anything itself, the output should be printed by the threads that it creates. The main() can decide which thread to begin executing first, or it can leave it to the threads to sort this out. The order of words in the phrase should not be changed in the print. Your program should work for any phrase of any reasonable length, not just the one given in the example. The output of your program should look similar to the following
$ ./vowcon Operating Systems Class at CSUN
vow: Operating
con: Systems
con: Class
vow: at con: CSUN
In this part you are *not allowed* to use synchronization primitives such as semaphores, mutexes, mutex locks such as pthread_mutex_lock and pthread_mutex_unlock and conditional variables such as pthread_cond_wait and pthread_cond_signal etc. from pthreads library for thread coordination. You do not neet to use sched_yield() to relinquish control of the CPU, but you can use it if you like. You will have to may also investigate some of the other pthread functions available to you for this project, and use them if you like. Although you do not actually need them.
#include<bits/stdc++.h>
using namespace std;
int a=0;
vector<string> arr;
void vow(){
for(int i=0;i<arr.size();i++){
while(a == 0);
if(arr[i][0] == 'a' or arr[i][0] == 'e' or arr[i][0] == 'i' or arr[i][0] == 'o' or arr[i][0] == 'u')
cout<<"vow: "<<arr[i]<<endl;
a = 0;
}
}
void con(){
for(int i=0;i<arr.size();i++){
while(a == 1);
if(!(arr[i][0] == 'a' or arr[i][0] == 'e' or arr[i][0] == 'i' or arr[i][0] == 'o' or arr[i][0] == 'u'))
cout<<"con: "<<arr[i]<<endl;
a = 1;
}
}
int main(int argc , char* argv[]){
for(int i=1;i<argc;i++){
arr.push_back(string(argv[i]));
}
std::thread first(vow);
std::thread second (con);
first.join();
second.join();
}