Word Count with Mutex
Two threads each receive a sentence and count the words in it. The threads add their result to a shared global counter, protected by a mutex. Otherwise two concurrent writes can lose increments.
A word is any maximal run of non-space characters. Each thread counts locally first, then takes the lock once at the end to add its contribution to the total. We don't hold the mutex for long.
Input
- Line 1: the first sentence (up to 5·10^4 characters).
- Line 2: the second sentence (up to 5·10^4 characters).
- Words are separated by one or more spaces, and a sentence may start or end with spaces. Everything else (letters, digits, punctuation) belongs to a word. There are no tabs, and each sentence holds at least one word.
Output
- A single line:
Total words: Xwhere X is the total number of words across both sentences.
Examples
Input:
hello world
the code is clean
Output:
Total words: 6
Input:
one two three four
five six
Output:
Total words: 6
Use pthread_create, pthread_join and pthread_mutex_lock/unlock from pthread.h.