Print consonant and vowels My assignment is to to create two threads running functions (called vow and cons). The threads take turns printing the respective words of the phrase supplied from a text file. The main thread shouldn't post anything (only cons and vow functions should print). I can't use synchronization primitives such as mutexes for thread coordination. But I can use yield(). I think I might have my answer below but I also might be way off and I didn't use yield() at all.

Could someone please take a look and tell me if this meets the requirements I listed above.



phrase.txt:


  In computer architecture, multithreading is the ability of a central processing unit (CPU) or a single core in a multi-core processor to execute multiple processes or threads concurrently, appropriately supported by the operating system.




#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;thread&gt;

void cons(std::string currWord){
    std::cout &lt;&lt; "cons: " &lt;&lt; currWord &lt;&lt; "\n";
}

void vow(std::string currWord){
    std::cout &lt;&lt; "vow: " &lt;&lt; currWord &lt;&lt; "\n";
}

bool checkVowel(std::string word) {
    char vowels[10] = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
    for(int i = 0; i &lt; 10; i++) {
        if(word[0] == vowels[i]) {
            return true;
        }
    }
    return false;
}

int main() {
    std::string currWord;
    std::ifstream input;
    input.open("phrase.txt");

    if(input.is_open()) {

    currWord = std::string();

    while(!input.eof()) {
            input &gt;&gt; currWord;
        if(checkVowel(currWord)) {
            std::thread vowThread(vow, currWord);
            vowThread.join();
        }
        else {
            std::thread consThread(cons, currWord);
            consThread.join();
        }
    }

    input.close();
    }

    else {
        std::cout &lt;&lt; "Error: File not opened";
    }

    return 0;
}

bool checkVowel(std::string);
void vow(std::string);
void cons(std::string);
Your code is a little confusing.  You're using names like currWord and passing a word to checkVowel, which is then checking for 'aeiou' etc.  I think of vowels as letters, so on the face of it you appear to be checking whole words against individual vowel letters, which probably shouldn't match.  That said, maybe the file is ordered such that it's actually only reading letters at a time.  Either way, your code is unclear.

As has been said in the comments, whilst you've technically used threads to perform the printing, it's unlikely that this is the strategy that was expected to solve the problem.  There are many approaches that you could take to solving the problem, one of which might be something like this:

start ConThread
start VowelThread

Open File
ForEach word in file
    ForEach letter in word
        if Vowel
            StoreLetter
            Set VowelFlag
        else
            StoreLetter
            Set ConFlag
        while ReadNextLetterFlag not set give up control
        clear ReadNextLetterFlag

Shutdown...


The vowel/Con threads then consist of something like:

While not shutdown
    While ConFlag not set give up control
    clear ConFlag
    Retrieve letter
    Output to console
    Set ReadNextLetterFlag


Giving up control of the running thread could be achieve using yield (although this isn't particularly efficient) and you may want to pair it with sleep.

For flags, you probably want to look at either atomic and/or volatile variables.