The program uses Pthread to count the occurrences of each word in a text. Each thread handles a single line of string.
Use a map<string, size_t> word_count as a global variable.
Kernel function, use Pthread_mutex_lock to control changes to the global variable Word_count. Use StringStream to process strings.
Input:
First sentence.
Second sentence,
Third sentence.
Fourth sentence.
Five sentence
Six sentence
Seven sentence
Output:
First occurs 1 time
Five occurs 1 time
Fourth occurs 1 time
Second occurs 1 time
Sentence occurs 7 times
Seven occurs 1 time
Six occurs 1 time
Third occurs 1 time
Makefile
A.out:map.o
g++-std=c++0x-o A.out-lpthread MAP.O
Map.o:map.cpp
g++-std=c++0x-c Map.cpp
Run:
Cat Paragraph.txt |./a.out
Code:
#include <pthread.h> #include <map> #include <string> #include <iostream > #include <sstream> #include <algorithm> #include <vector>using namespace std; #define line_per_thread1pthread_mutex_t count_mutex = pthread_mutex_ initializer;map<string, size_t> word_count;struct para {int tidx;string str;};/ /kernel functionvoid * wordcount (VOID&NBSP;*PT) {struct para *local = (struct para *) pt;string local_str = local->str;pthread_mutex_lock (& Count_mutex); Stringstream ss (LOCAL_STR); String token;while (Ss >> token) ++word_count[ Token];p thread_mutex_unlock (&count_mutex);} Int main () {String word;vector<string> vstr;int num_lines = 0;while (cin && getline (Cin, word) && !cin.eof ()) {Num_linEs++;word.erase (Remove (Word.begin (), word.end (), ', '), word.end ()), Word.erase (Remove (Word.begin (), word.end (), '. '), word.end ()); Vstr.push_back (word);} int num_threads = (num_lines + line_per_thread - 1) / LINE_PER_ Thread;pthread_t threads[num_threads];for (int i = 0; i < num_threads; i++) {Struct para *str_para = new para (); str_para->tidx = i;str_ Para->str = vstr[i];p thread_create (&threads[i], NULL, wordCount, (void *) str_para);} for (int i = 0; i < num_threads; i++) pthread_join (Threads[i], NULL); map<string, size_t>::iterator it;for (It = word_count.begin (); it != word_count.end (); ++it) {cout << it->first << " occurs " << it->second << ((it->second > 1) ? " times" : " time") << endl;}}
This article from "Hu A Knife" blog, declined reprint!
Pthread Statistics Text Count