首页 > 编程语言 >C++写得统计线程利用率的小工具

C++写得统计线程利用率的小工具

时间:2023-05-26 15:32:33浏览次数:41  
标签:void CThreadUsage C++ _. mutex pthread usage 线程 写得


thread_usage.h

#ifndef __THREAD_USAGE__
#define __THREAD_USAGE__

#include <fstream>
#include <string>
#include <map>
#include <pthread.h>
#include <sys/time.h>

namespace thread
{
class CThreadUsage
{
public:
    CThreadUsage(unsigned int printCount, std::string strFilename);
    ~CThreadUsage();

    void AddThread(pthread_t pthread_id);
    void Add();
    
private:  
    std::map<pthread_t, int> usage_;
    unsigned int printCount_;
    std::ofstream output_;
    unsigned int count_;
    pthread_mutex_t mutex_;
	timeval tstart_;
    timeval tend_;
    
	void Print();
    void Clear();
};


}

#endif //__THREAD_USAGE__



thread_usage.cpp


#include "thread_usage.h"

using namespace thread;

CThreadUsage::CThreadUsage(unsigned int printCount, std::string strFilename)
    :printCount_(printCount),
     output_(strFilename.c_str()),
     count_(0)
{
    pthread_mutex_init(&mutex_, NULL);
}

CThreadUsage::~CThreadUsage()
{
    output_.close();
    pthread_mutex_destroy(&mutex_);
}

void CThreadUsage::AddThread(pthread_t pthread_id)
{
    pthread_mutex_lock(&mutex_);
    usage_.insert(std::make_pair<pthread_t, int>(pthread_id, 0));//将每个线程的线程ID加入map.
    pthread_mutex_unlock(&mutex_);
}

void CThreadUsage::Add()
{
    pthread_mutex_lock(&mutex_);
    ++usage_[pthread_self()];
    ++count_;
	if (1 == count_)
	{
		gettimeofday(&tstart_, NULL);
	}
    else if (printCount_ == count_)
    {
		gettimeofday(&tend_, NULL);
        Print();//打印
        Clear();//清空
    }
    pthread_mutex_unlock(&mutex_);
}

void CThreadUsage::Print()
{
	double linStart = ((double)tstart_.tv_sec * 1000000 + (double)tstart_.tv_usec);   //unit S
	double linEnd = ((double)tend_.tv_sec * 1000000 + (double)tend_.tv_usec);         //unit S
	double delta = (linEnd-linStart)/1000;                                          //Micro S
	
    output_ << "========== Begin ========" << "\n"
			<< "Total take: " << delta << " ms, avarage " << delta/printCount_ << "ms per.\n"
            << "Thread_ID\t" << "Usage" << std::endl;
	
	
	
    for (std::map<pthread_t, int>::iterator iBegin = usage_.begin();
         iBegin != usage_.end(); ++iBegin)
    {
        output_ << iBegin->first << "\t" << iBegin->second << std::endl;
    }
    output_ << "=========== End =========" << std::endl;
}

void CThreadUsage::Clear()
{
    count_ = 0;
    for (std::map<pthread_t, int>::iterator iBegin = usage_.begin();
         iBegin != usage_.end(); ++iBegin)
    {
        iBegin->second = 0;
    }
}



测试函数:

#include "thread_usage.h"

thread::CThreadUsage oThreadUsage(100, "num.txt");//每处理完100个线程,就打印一次每个线程处理的任务数量

void* pthread_func(void* argv)
{
    pthread_detach(pthread_self());
    oThreadUsage.AddThread(pthread_self());
    while (1)
    {
        oThreadUsage.Add();
        sleep(1);
    }
    return NULL;
}

int main()
{
    pthread_t thread[10];
    
    for (int iIndex = 0; iIndex < 10; ++iIndex)
    {
        pthread_create(&thread[iIndex], NULL, pthread_func, NULL);
    }

    while(1);
}




标签:void,CThreadUsage,C++,_.,mutex,pthread,usage,线程,写得
From: https://blog.51cto.com/u_16131207/6356848

相关文章

  • How to initialize a static const map in c++?
    #include<map>usingnamespacestd;structA{staticmap<int,int>create_map(){map<int,int>m;m[1]=2;m[3]=4;m[5]=6;returnm;}staticconstma......
  • Java大文件分片上传/多线程上传源代码
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上注释了:上传文件实体类:看得出来,实体类中已经有很多我们需要的功能了,还有实用的属性。如MD5秒传的信息。pub......
  • C++ 注释
     程序的注释是解释性语句,您可以在C++代码中包含注释,这将提高源代码的可读性。所有的编程语言都允许某种形式的注释。C++支持单行注释和多行注释。注释中的所有字符会被C++编译器忽略。C++注释一般有两种:// -一般用于单行注释。/*...*/ -一般用于多行注释。......
  • Java大文件分片上传/多线程上传源码
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。这次项目的需求:支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,ie8,ie9,Chrome,Firefox,360安全浏览器,并且刷新浏览器后仍然能够续传,重启浏览器(关闭......
  • Java大文件分片上传/多线程上传代码
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。首先我们需要了解的是上传文件三要素:1.表单提交方式:post(get方式提交有大小限制,post没有)2.表单的enctype属性:必须设置为multipart/form-data.3.表单必须......
  • C++外卖点餐系统[2023-05-26]
    C++外卖点餐系统[2023-05-26]选题九:外卖点餐系统7.基本要求:[1]编写一个外卖点餐系统,实现对客户、店铺、订单及配送人员等信息的管理。[2]客户信息包括:客户姓名、联系方式、地址等;店铺信息包括:其菜品和价格评分等;配送人员信息包括:姓名,联系方式、评分等:订单信息包括:编......
  • 深度解析线程的正确停止方法
    深度解析线程的正确停止方法一、解惑1.什么情况下,线程需要被停止?线程和任务被创建和启动之后,大部分情况下都是自然运行到结束的,自然停止,但有些情况会需要用到停止线程,如:用户主动取消服务被快速关闭运行出错或超时情况下等线程都需要被停止这些情况都需要主动来......
  • 【java】同步异步和多线程编程
    Java基本概念并发基于时间段内的,同时发生(处理多个任务的能力,时间段)存在同步和互斥的问题(任务之间的时序问题)同步:前一个处理的结果作为下一个处理的资源(互相之间有依赖)互斥:不能同时使用临界资源。解决时序问题的机制:锁,信号量,原子操作Java中的多线程机制并行(完全......
  • c++ condition_variable wait notify_one
    #include<chrono>#include<condition_variable>#include<ctime>#include<curl/curl.h>#include<curl/easy.h>#include<fstream>#include<future>#include<iostream>#include<iomanip>#include<m......
  • 使用Java 锁机制实现多线程售票案例
    本文首发自「慕课网」,想了解更多IT干货内容,程序员圈内热闻,欢迎关注"慕课网"及“慕课网公众号”!作者:王军伟Tech|慕课网讲师1.前言本文主要是使用Java的锁机制对多线程售票案例进行实现。售票案例多数情况下主要关注多线程如何安全的减少库存,也就是剩余的票数,当票数为0时,停止......