pthread统计文本次数
2016-05-06 00:54
183 查看
该程序使用pthread来统计某一文本中每个单词出现次数。每一个thread处理一行字符串。
使用一个map<string, size_t> word_count作为全局变量。
kernel function 中,使用pthread_mutex_lock来控制对全局变量word_count的改变。使用stringstream来处理字符串。
输入:
first sentence.
second sentence,
third sentence.
fourth sentence.
five sentence
six sentence
seven sentence
输出:
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
运行:
cat paragraph.txt | ./a.out
代码:
使用一个map<string, size_t> word_count作为全局变量。
kernel function 中,使用pthread_mutex_lock来控制对全局变量word_count的改变。使用stringstream来处理字符串。
输入:
first sentence.
second sentence,
third sentence.
fourth sentence.
five sentence
six sentence
seven sentence
输出:
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
运行:
cat paragraph.txt | ./a.out
代码:
#include <pthread.h> #include <map> #include <string> #include <iostream> #include <sstream> #include <algorithm> #include <vector> using namespace std; #define LINE_PER_THREAD 1 pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; map<string, size_t> word_count; struct para { int tidx; string str; }; //kernel function void * wordCount (void *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]; pthread_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]; pthread_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; } }
相关文章推荐
- 用VBScript写合并文本文件的脚本
- CMD命令行将当前磁盘所有文件名写入到文本文件的方法
- C#读写文本文件的方法
- 使用VBS访问外部文本文件一些方法和脚本实例代码
- VBS文本文件操作实现代码
- C#处理文本文件TXT实例详解
- C#读写指定编码格式的文本文件
- 文本文件编码方式区别
- C语言中使用lex统计文本文件字符数
- C#实现获取文本文件的编码的一个类(区分GB2312和UTF8)
- php删除文本文件中重复行的方法
- 效率较高的php下读取文本文件的代码[原创]_php技巧_脚本之家
- C#读取文本文件到listbox组件的方法
- java中关于文本文件的读写方法实例总结
- PHP通过header实现文本文件下载的代码
- MySQL文本文件导入及批处理模式应用说明
- php中用文本文件做数据库的实现方法
- WinForm导出文件为Word、Excel、文本文件的方法
- 简单的用java实现读/写文本文件的示例
- c#检测文本文件编码的方法