Pthread Statistics Text Count

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.