首页 > 其他分享 >标准库版thread group

标准库版thread group

时间:2024-01-28 15:12:08浏览次数:25  
标签:end thread guard 库版 threads group include

使用标准库替代boost中的thread group

转载修改自 秋天的栗树:用std::thread替换实现boost::thread_group

  • 使用unique_ptr替换了其中的auto_ptr(已不受支持);
  • 添加了<functional> <algorithm>两个头文件;
#include <list>
#include <mutex>
#include <thread>
#include <memory>
#include <algorithm>
#include <functional>
namespace std
{
    //兼容boost::thread_group
    //使用std::thread代替boost::thread,std::mutex代替boost::shared_mutex
    class thread_group
    {
    private:
        thread_group(thread_group const&);
        thread_group& operator=(thread_group const&);
    public:
        thread_group() {}
        ~thread_group()
        {
            for(auto it=threads.begin(),end=threads.end();    it!=end;++it)
            {
                delete *it;
            }
        }

        template<typename F>
        thread* create_thread(F threadfunc)
        {
            lock_guard<mutex> guard(m);
            unique_ptr<thread> new_thread(new thread(threadfunc));
            threads.push_back(new_thread.get());
            return new_thread.release();
        }

        void add_thread(thread* thrd)
        {
            if(thrd)
            {
                lock_guard<mutex> guard(m);
                threads.push_back(thrd);
            }
        }

        void remove_thread(thread* thrd)
        {
            lock_guard<mutex> guard(m);
            auto it=std::find(threads.begin(),threads.end(),thrd);
            if(it!=threads.end())
            {
                threads.erase(it);
            }
        }

        void join_all()
        {
            lock_guard<mutex> guard(m);
            for(auto it=threads.begin(),end=threads.end();it!=end;++it)
            {
                (*it)->join();

            }
        }

        size_t size() const
        {
            lock_guard<mutex> guard(m);
            return threads.size();
        }

    private:
        list<thread*> threads;
        mutable mutex m;
    };
}

标签:end,thread,guard,库版,threads,group,include
From: https://www.cnblogs.com/hdming/p/17992870

相关文章

  • MySQL 8.0.26 新增参数 group_replication_view_change_uuid
    MySQL8.0.26新增参数group_replication_view_change_uuidGreatSQL[root@localhost][test]>showglobalvariableslike'group_replication_view_change_uuid';+------------------------------------+-----------+|Variable_name|V......
  • 鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之CheckboxGroup组件
    鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之CheckboxGroup组件一、操作环境操作系统: Windows10专业版、IDE:DevEcoStudio3.1、SDK:HarmonyOS3.1+编辑二、CheckboxGroup组件提供多选框组件,通常用于某选项的打开或关闭。子组件无。接口CheckboxGroup(options?:{group?:string})创......
  • pthread_detach函数
     线程分离状态:指定该状态,线程主动与主控线程断开关系。使用pthread_exit或者线程自动结束后,其退出状态不由其他线程获取,而直接自己自动释放。网络、多线程服务器常用。    进程若有该机制,将不会产生僵尸进程。僵尸进程的产生主要由于进程死后,大部分资源被释放,一点残留资......
  • java中的ThreadLocal
    1.ThreadLocal的基本使用在Java的多线程并发执行过程中,为了保证多个线程对变量的安全访问,可以将变量放到ThreadLocal类型的对象中,使变量在每个线程中都有独立值,不会出现一个线程读取变量时而被另一个线程修改的现象。ThreadLocal类通常被翻译为线程本地变量类或者线程局部变......
  • C# AsyncLocal 是如何实现 Thread 间传值
    一:背景1.讲故事这个问题的由来是在.NET高级调试训练营第十期分享ThreadStatic底层玩法的时候,有朋友提出了AsyncLocal是如何实现的,虽然做了口头上的表述,但总还是会不具体,所以觉得有必要用文字+图表的方式来系统的说一下这个问题。二:AsyncLocal线程间传值1.线程间传值途径在......
  • Permission denied: user=hive, access=EXECUTE, inode=“/tmp“:root:supergroup:drw
    在执行Hadoop的创建目录、写数据等情况,可能会出现该异常,而在读文件的时候却不会报错,这主要是由于系统的用户名不同导致的,由于我们进行实际开发的时候都是用Windows操作系统,而编译后的JAVA程序是部署在Linux上的。而Windows的用户名一般都是自定义的或者是administrator,Linux的用户......
  • Mygin实现分组路由Group
    本篇是Mygin第五篇目的实现路由分组为什么要分组分组控制(GroupControl)是Web框架应该提供的基础功能之一,对同一模块功能的开发,应该有相同的前缀。或者对一部分第三方接口,统一需要加解密等功能。分组后很方便。例如:对于任务模块,统一前缀为/task除去/user/login接口,都......
  • Microsoft 365 解决方案:Security Group与Conditional Access强强联手限制不同用户的MF
    51CTO博客链接:https://blog.51cto.com/u_13637423多重身份验证(MFA)为登录流程增加了一层保护。访问帐户或应用时,用户需要提供额外的身份验证,例如扫描指纹或输入手机收到的验证码,确保你用于高风险帐户的凭据可以防止网络钓鱼和渠道入侵。MicrosoftEntra中的MFA会要求使用以下......
  • laravel collect结果集group分组合并数据
    1、需求将相同apply_id的apply_remark用;拼接$r=[['apply_id'=>1,'apply_remark'=>'xxx'],['apply_id'=>1,'apply_remark'=>'xxx2'],['apply_id'=>2......
  • [Design Pattern] Intro: Three groups of patterns
    Creationalpatternsprovideobjectcreationmechanismsthatincreaseflexibilityandreuseofexistingcode.Factorymethod:Providesaninterfaceforcreatingobjectsinasuperclass,butallowsubclasstoalterthetypeofobjectsthatwillbecreated.......