C+= concurrent_queue 安全執行緒測試

來源:互聯網
上載者:User

標籤:

#include <include/tbb/tbb.h>#include <pthread.h>#include <iostream>using namespace std;#define NUM_THREAD 10class 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]

C+= concurrent_queue 安全執行緒測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.