C+= concurrent_queue 线程安全测试

更推荐使用:http://www.boost.org/doc/libs/1_56_0/doc/html/boost/lockfree/queue.html


#include <include/tbb/tbb.h> #include <pthread.h> #include <iostream> using namespace std; #define NUM_THREAD 10 class person { public: person() { } person(int name, unsigned int thread_num){ this->name = name; this->thread_num = thread_num; } void print() { printf("%u\t%u\n", thread_num, name); } private: int name; unsigned int thread_num; }; tbb::strict_ppl::concurrent_queue<person*> que; void* consumer(void* arg) { cout << arg << endl; while(true) { if(que.empty()) { cout << '.' << flush; sleep(1); } person* p; if(que.try_pop(p)) { p->print(); } } } void* producer(void* arg) { cout << arg << endl; unsigned int thread_num =(unsigned int)pthread_self(); for(int i=0; i<10; i++) { person* p = new person(i, thread_num); que.push(p); } } int main() { pthread_t tids_c[NUM_THREAD]; for(int i=0; i<NUM_THREAD; i++) { int ret = pthread_create(&tids_c[i], NULL, consumer, NULL); if(ret != 0) { cout << "start consumer failed" << endl; return -1; } } pthread_t tids[NUM_THREAD]; for(int i=0; i<NUM_THREAD; i++) { int ret = pthread_create(&tids[i], NULL, producer, NULL); if(ret != 0) { cout << "start produer failed" << endl; return -1; } } for(int i=0; i<NUM_THREAD; i++){ pthread_join(tids[i], NULL); pthread_join(tids_c[i], NULL); } cout << "exit" << endl; } /* vim: set ts=4 sw=4 sts=4 tw=100 */
dic = {}
with open('log.txt') as f:
    while True:
        line = f.readline()
        if not line:
            break;
        line = line.strip()

        arr = line.split('\t')
        key = arr[1]

        if key not in dic:
            dic[key] = 1
        else:
            dic[key] += 1

    for key in dic:
        print key, dic[key]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值