• c++ break while


    #include <iostream>
    #include <vector>
    #include <pthread.h>
    #include "destory_free_while.h"
    using namespace std;
    
    Worker::Worker() {
        shutdown = false;
        num_thread = 10;
    }
    void Worker::start() {
        tids.resize(num_thread);
        for (int i = 0; i <num_thread ; ++i) { 
            if (pthread_create(&tids[i], NULL, sleeping, this) == 0) {
                cout << "thread start complete" << endl;
            }
        }
    }
    
    Worker::~Worker() {
        cout << "begin destory instance" << endl;
        shutdown = true;
        for(unsigned int i=0; i<tids.size(); i++) {
            pthread_join(tids[i], NULL);
        }
    }
    
    
    void* Worker::sleeping(void* arg) {
        Worker* work = (Worker*)arg;  
        while(true) {
            cout << "." << flush;
            sleep(1);
            if(work->shutdown) {
                cout << " destory instance , thread quit" << endl;
                break;
            }
    
        }
    }
    
    /* vim: set ts=4 sw=4 sts=4 tw=100 */
    
    #ifndef DESTORY_FREE_WHILE_H
    #define DESTORY_FREE_WHILE_H
    #include <vector>
    #include <pthread.h>
    class Worker {
    public:
        Worker();
        ~Worker();
        void start();
    public:
        static void* sleeping(void* arg);
    public:
        std::vector<pthread_t> tids;
        int num_thread;
        bool shutdown; 
    };
    
    #endif  // DESTORY_FREE_WHILE_H
    
    #include <iostream>
    #include "destory_free_while.h"
    using namespace std;
    
    int main() {
        Worker worker;
        worker.start();
        sleep(20);
        cout << "program exit" << endl;
    }
  • 相关阅读:
    Excel入门
    夹缝中求和
    移动撤销
    CSP2020-j2 T4 方格取数
    CSP2020-j2 T3表达式(expr)
    直播 获奖(live)
    优秀的拆分(power)
    P5661 公交换乘
    P1160 队列安排
    P1083 借教室
  • 原文地址:https://www.cnblogs.com/i80386/p/4496785.html
Copyright © 2020-2023  润新知