STL的相关知识2

相关函数:

1.is_array
2.is_class
3.is_function
4.is_pointer
5 is_rvalue_reference
6 is_abstract
7 is_const
add_const
remove_const
add_pointer
remove_pointer
add_lvalue_reference
add_rvalue_reference
remove_reference

#include<iostream>
#include<type_traits>
using namespace std;

template<class T>class TD;
//decltype
//auto
//template
template<class T>
void test(T&t){
  cout<<is_array<T>::value<<endl;
  cout<<is_pointer<T>::value<<endl;
cout<<is_rvalue_reference<decltype(t)>::value<<endl;
//TD<T> object;
using TYPE=typename remove_pointer<T>::type;//模板类型推导必须用typename修饰
cout<<is_name<int*,TYPE>::value<<endl;
}
template<class T>
void test(T&&t){
  cout<<is_array<T>::value<<endl;
  cout<<is_pointer<T>::value<<endl;
cout<<is_rvalue_reference<decltype(t)>::value<<endl;
}
int main(){
   int a[]={1,2,3};
   int*b=a;
   test(a);
   test(b);
   
return 0;
}

线程池实现:

#include<iostream>
#include<condition_variable>
#include<mutex>
#include<thread>
#include<functional>
#include<queue>
#include<vector>
using namespace std;

#define USE_MUTEX
//#define USE_CONDITION

class ThreadTask{
public:
    template<class PTR, class ...ARGS>
    ThreadTask(PTR ptr, ARGS ...args){
        _fun = bind(ptr, forward<ARGS>(args)...);
    }

    void runTask(){
        _fun();
    }

private:
    std::function<void()> _fun;
};

class ThreadPool{
public:
    ThreadPool(int max):_max(max){

    }
    
    virtual ~ThreadPool(){
        this->stop();

        while (!_tasks.empty())
        {
            auto t = _tasks.front();
            delete t;
            _tasks.pop();
        }
    }

    void start(){
        time = clock();

        for(int i=0; i<_max; i++){
            _threads.push_back(new thread(&ThreadPool::work, this));
        }
    }
    void stop(){
        for(auto t : _threads){
            t->join();
            delete t;
        }
        _threads.clear();
    }

    void work(){
        while (true)
        {
            ThreadTask* task = getOneTask();
            if(task){
                task->runTask();
                
                delete task;
                
            }else{
                #ifdef USE_MUTEX
                    std::this_thread::sleep_for(chrono::microseconds(1));
                #endif
            }
        }
    }

    void addTask(ThreadTask* task){
        #ifdef USE_CONDITION
            unique_lock<mutex> lock(_mtx);
        #endif
        #ifdef USE_MUTEX
            _mtx.lock();
        #endif

        _tasks.push(task);

        #ifdef USE_MUTEX
            _mtx.unlock();
        #endif

        #ifdef USE_CONDITION
            _condition.notify_one();
        #endif
    }

    ThreadTask* getOneTask(){
        ThreadTask* p = nullptr;
    
    #ifdef USE_CONDITION
        unique_lock<mutex> lock(_mtx);//这里_mtx.lock();
    #endif
    #ifdef USE_MUTEX
        _mtx.lock();
    #endif

        if(_tasks.empty()){
            cout << "time = " << clock() - time << endl;
            stop();

    #ifdef USE_CONDITION
           _condition.wait(lock);
    #endif
        }
        else{
            p = _tasks.front();
            _tasks.pop();
        }

    #ifdef USE_MUTEX
        _mtx.unlock();
    #endif

        return p;//这里_mtx.unlock();
    }

private:
    int _max;
    std::queue<ThreadTask*> _tasks;
    std::vector<std::thread*> _threads;
    mutex _mtx;
    condition_variable _condition;
    long time;
};

void add(int& a){
    int n =10;
    while (n--)
    {
        a++;
        // cout << "a = " << a << " thread_id = " << this_thread::get_id() << endl;
//        this_thread::sleep_for(chrono::milliseconds(100));
    }
}

int main(){
    ThreadPool pool(5);
    int n = 10000;
    for(int i=0; i<n; i++){
        pool.addTask(new ThreadTask(add, i));
    }
    pool.start();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值