多线程变慢

#ifndef __SYNC_QUEUE_HPP__
#define __SYNC_QUEUE_HPP__

#include <queue>
#include <string>
#include <boost/shared_ptr.hpp>
#include <chrono>
#include <ctime>

using namespace std;
using boost::shared_ptr;

typedef struct ImgFeatureElement
{
    std::string m_imgPath;
    std::vector<float> m_imgFeature;
    std::string m_id;
    int m_errCode;
}ImgFeatureElement;

class ElapsedTime
{
    public:
    ElapsedTime(std::string func, int th);
    ~ElapsedTime();
    double GetElapsedTime();
    private:
        std::chrono::time_point<std::chrono::system_clock> start;
        std::chrono::time_point<std::chrono::system_clock> end;
        int thread_id;
        std::string m_func;
};

template<typename T>
class SyncQueue {
 public:
  explicit SyncQueue();

  void push(const T& t);

  T pop();

  size_t size() const;

 protected:
  class sync;
  std::queue<T> queue_;
  boost::shared_ptr<sync> sync_;

};

#endif

##########################################################################

#include <boost/thread.hpp>
#include "sync_queue.hpp"

ElapsedTime::ElapsedTime(std::string func, int th):m_func(func),thread_id(th)
{   
    start = std::chrono::system_clock::now();
}   

ElapsedTime::~ElapsedTime()
{   
    end = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);
    std::cout << "finished  at " << std::ctime(&end_time) << std::endl;
    std::cout << m_func << thread_id <<"\033[33m melapsed time: " << elapsed_seconds.count() << "s\033[0m\n";
}

double ElapsedTime::GetElapsedTime()
{
    end = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = end-start;
    return elapsed_seconds.count();
}

template<typename T>
class SyncQueue<T>::sync {
 public:
  mutable boost::mutex mutex_;
  boost::condition_variable condition_push;  
  boost::condition_variable condition_pop;
};

template<typename T>
SyncQueue<T>::SyncQueue()
    : sync_(new sync()) {
}

template<typename T>
void SyncQueue<T>::push(const T& t) {
  boost::mutex::scoped_lock lock(sync_->mutex_);

  while (!queue_.empty()) {
    sync_->condition_push.wait(lock);
  }
  queue_.push(t);
  sync_->condition_pop.notify_one();  
  lock.unlock();

}

template<typename T>
T SyncQueue<T>::pop() {
  boost::mutex::scoped_lock lock(sync_->mutex_);

  while (queue_.empty()) {
    sync_->condition_pop.wait(lock);
  }

  T t = queue_.front();
  queue_.pop();
  sync_->condition_push.notify_one();
  return t;
}

template<typename T>
size_t SyncQueue<T>::size() const {
  boost::mutex::scoped_lock lock(sync_->mutex_);
  return queue_.size();
}

template class SyncQueue<ImgFeatureElement>;

template class SyncQueue<int>;

################################################################################

#include <iostream>
#include <thread>
#include "sync_queue.hpp"

class DeamonThread
{
    public:
        DeamonThread(){}
        ~DeamonThread(){}
        int deal_event()
        {   
            for (int i = 0; i < 1000; ++i)
            {   
                std::cout<<"      "<<i;
            }   
            std::cout<<std::endl;
        }   
        void forward(int& vecIdx)
        {   
            m_inQueue.push(1);
            deal_event();
            m_inQueue.pop();
        }   
    private:
        SyncQueue<int> m_inQueue;

};


class ThreadPool
{
    public:
       ThreadPool(int vecSize):m_dt(vecSize)
        {   
            int vecIdx = 0;
            for (vecIdx = 0; vecIdx < vecSize; ++vecIdx)
            {   
                m_dt[vecIdx] = new DeamonThread;
            }   
        }   
       ~ThreadPool()
        {   
            int vecIdx = 0;
            for(;vecIdx < m_dt.size(); ++vecIdx)
            {   
                delete m_dt[vecIdx];
            }   
        }   
       DeamonThread* sche(DeamonThread*& dt, int& arrIdx)
       {   
            arrIdx = vecCount % m_dt.size();
            dt = m_dt[arrIdx];
            ++vecCount;
            return dt;
       }   
        void forward(int& vecIdx)
        {
            DeamonThread* dt = NULL;
            sche(dt, vecIdx);
            dt->forward(vecIdx);
        }
    private:
        std::vector<DeamonThread*> m_dt;
        int vecCount;
};
void main_blockqueue_thread(ThreadPool& d, double& time)
{
    int vecIdx = 0;
    ElapsedTime et(__FUNCTION__,__LINE__);
    d.forward(vecIdx);
    time = et.GetElapsedTime();
    return ;
}


double calcAllTime(double* thtime, int th_user)
{
    double alltime = 0;

    for (int i = 0; i < th_user; ++i)
    {
        if (0==(i % 10))
        {
            std::cout <<std::endl;
        }
            std::cout<<"["<< i <<":"<< thtime[i] << "]\t";

        alltime = alltime + thtime[i];
    }
    std::cout<<std::endl<<"alltime:"<<alltime<<std::endl;
    return 0;
}

int main(int argc, char** argv)
{
    int vecSize = 0;
    std::cout<<"vecSize:"<<std::endl;
    std::cin>>vecSize;
    ThreadPool d(vecSize);
    int user = 10;
    double Etime[user] {0};
    std::thread* threads[user] = {0};
    for (int uidx = 0; uidx < user; ++uidx)
    {
        threads[uidx] = new std::thread(main_blockqueue_thread,std::ref(d),std::ref(Etime[uidx]));
    }

    for (int uidx = 0; uidx < user; ++uidx)
    {
        threads[uidx]->join();
    }

    calcAllTime(Etime, sizeof(Etime)/sizeof(Etime[0]));
    return 0;
}
                                                               

###################################################################

exec=exec
platform=linux

csrcs=$(wildcard *.c)
cobjs=$(patsubst %.c,out/$(platform)/%.o,$(csrcs))

cppsrcs=$(wildcard *.cpp)
cppobjs=$(patsubst %.cpp,out/$(platform)/%.o,$(cppsrcs))

CC= gcc
CXX= g++  -Wl,--no-as-needed
RM= rm  

BOOST_DIR=/usr/local/boost
BOOST_INC=-I$(BOOST_DIR)/include
BOOST_LIB_DIR=-L$(BOOST_DIR)/lib
BOOST_LIBS=-lboost_atomic
BOOST_LIBS+=-lboost_chrono
BOOST_LIBS+=-lboost_context
BOOST_LIBS+=-lboost_date_time
BOOST_LIBS+=-lboost_exception
BOOST_LIBS+=-lboost_filesystem
BOOST_LIBS+=-lboost_graph
BOOST_LIBS+=-lboost_iostreams
BOOST_LIBS+=-lboost_locale
BOOST_LIBS+=-lboost_math_c99
BOOST_LIBS+=-lboost_math_c99f
BOOST_LIBS+=-lboost_math_c99l
BOOST_LIBS+=-lboost_math_tr1
BOOST_LIBS+=-lboost_math_tr1f
BOOST_LIBS+=-lboost_math_tr1l
BOOST_LIBS+=-lboost_prg_exec_monitor
BOOST_LIBS+=-lboost_program_options
BOOST_LIBS+=-lboost_python
BOOST_LIBS+=-lboost_random
BOOST_LIBS+=-lboost_regex
BOOST_LIBS+=-lboost_serialization
BOOST_LIBS+=-lboost_signals
BOOST_LIBS+=-lboost_system
BOOST_LIBS+=-lboost_test_exec_monitor
BOOST_LIBS+=-lboost_thread
BOOST_LIBS+=-lboost_timer
BOOST_LIBS+=-lboost_unit_test_framework
BOOST_LIBS+=-lboost_wave

BOOST_LIBS+=-lboost_wserialization


BOOST_LIB_SO=-lboost_atomic
BOOST_LIB_SO+=-lboost_chrono
BOOST_LIB_SO+=-lboost_context
BOOST_LIB_SO+=-lboost_date_time
BOOST_LIB_SO+=-lboost_filesystem
BOOST_LIB_SO+=-lboost_graph
BOOST_LIB_SO+=-lboost_iostreams
BOOST_LIB_SO+=-lboost_locale
BOOST_LIB_SO+=-lboost_math_c99f
BOOST_LIB_SO+=-lboost_math_c99l
BOOST_LIB_SO+=-lboost_math_c99
BOOST_LIB_SO+=-lboost_math_tr1f
BOOST_LIB_SO+=-lboost_math_tr1l
BOOST_LIB_SO+=-lboost_math_tr1
BOOST_LIB_SO+=-lboost_prg_exec_monitor
BOOST_LIB_SO+=-lboost_program_options
BOOST_LIB_SO+=-lboost_python
BOOST_LIB_SO+=-lboost_random
BOOST_LIB_SO+=-lboost_regex
BOOST_LIB_SO+=-lboost_serialization
BOOST_LIB_SO+=-lboost_signals
BOOST_LIB_SO+=-lboost_system
BOOST_LIB_SO+=-lboost_thread
BOOST_LIB_SO+=-lboost_timer
BOOST_LIB_SO+=-lboost_unit_test_framework
BOOST_LIB_SO+=-lboost_wave
BOOST_LIB_SO+=-lboost_wserialization
all:out $(cppobjs)
        $(CXX) -o $(exec) $(cppobjs) $(BOOST_INC) $(BOOST_LIB_DIR) $(BOOST_LIBS) $(BOOST_LIB_SO) \
        $(OPENCV_LIB_DIR) $(OPENCV_LIBS) $(OPENCV_LIB_SO) \
        $(OPENCV_INC) $(BOOST_LIB_DIR) $(BOOST_LIBS) $(BOOST_LIB_SO) -lpython2.7 -lpthread
        #$(CC) -o $(exec) $(cobjs)

out/$(platform)/%.o:%.c
        $(CC) -o $@ -c $<

out/$(platform)/%.o:%.cpp
        $(CXX) -o $@ -c $< $(BOOST_INC) $(BOOST_LIB_DIR) $(BOOST_LIBS) $(BOOST_LIB_SO) \
        -DUSE_OPENCV -lpython2.7 -std=c++11

out:
        mkdir -p out/$(platform)/src

clean:
        $(RM) $(objs) out -rf


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值