首页 > 其他分享 >kimi写代码:tls singleton

kimi写代码:tls singleton

时间:2024-08-06 14:16:57浏览次数:4  
标签:tls std singleton ThreadLocalSingleton cout kimi include

#include <iostream>
#include <mutex>
#include <string>
#include <thread>

class ThreadLocalSingleton {
   private:
    ThreadLocalSingleton() {
        std::cout << "created for thread " << std::this_thread::get_id()
                  << std::endl;
    }
    ThreadLocalSingleton(const ThreadLocalSingleton&) = delete;
    ThreadLocalSingleton& operator=(const ThreadLocalSingleton&) = delete;

   public:
    static ThreadLocalSingleton& getInstance() {
        thread_local ThreadLocalSingleton instance;
        return instance;
    }
    void doSomething(std::string msg) {
        std::cout << msg << " from thread " << std::this_thread::get_id()
                  << std::endl;
    }
};

thread_local unsigned int rage = 1;
std::mutex cout_mutex;

void increase_rage(const std::string& thread_name) {
    ++rage;  // modifying outside a lock is okay; this is a thread-local
             // variable
    ThreadLocalSingleton::getInstance().doSomething(thread_name);
    std::lock_guard<std::mutex> lock(cout_mutex);
    std::cout << "Rage counter for " << thread_name << ": " << rage << '\n';
}

int main() {
    std::thread a(increase_rage, "a"), b(increase_rage, "b");

    {
        ThreadLocalSingleton::getInstance().doSomething("main");
        std::lock_guard<std::mutex> lock(cout_mutex);
        std::cout << "Rage counter for main: " << rage << '\n';
    }

    a.join();
    b.join();
}

// https://www.online-cpp.com/

// created for thread 140557953695048
// main from thread 140557953695048
// Rage counter for main: 1

// created for thread 140557951163168
// b from thread 140557951163168
// Rage counter for b: 2

// created for thread 140557951306528
// a from thread 140557951306528
// Rage counter for a: 2


标签:tls,std,singleton,ThreadLocalSingleton,cout,kimi,include
From: https://www.cnblogs.com/qqiwei/p/18345042

相关文章

  • 设计模式 - Singleton pattern 单例模式
    文章目录定义单例模式的实现构成构成UML图单例模式的六种实现懒汉式-线程不安全懒汉式-线程安全饿汉式-线程安全双重校验锁-线程安全静态内部类实现枚举实现总结其他设计模式文章:最后定义单例模式是一种创建型设计模式,它用来保证一个类只有一个实例,并且提供一个......
  • Kimi的PPT生成功能体验
    Kimi的PPT生成功能体验我们只需要输入@PPT可以切换PPT模板在线编辑大纲生成PPT文件,下载后结论总体生成流程还算便利,缺点是1)模板还较少,2)后期容易出现类同。今天先到这儿,希望对AIGC,云原生,技术领导力,企业管理,系统架构设计与评估,团队管理,项目管理,产品管理,信息安全,团队建设......
  • Transport Layer Security for UDP&TCP(TLS/DTLS1.2)
    参考文章:https://blog.csdn.net/alwaysrun/article/details/89076492 https://www.jianshu.com/p/fd0a624d0912 https://cloud.tencent.com/developer/article/1928677文档:https://www.rfc-editor.org/rfc/rfc6347  https://www.rfc-editor.org/rfc/rfc52461.SSL/TL......
  • 协议-TLS协议-客户端TLS解密的实现原理
    参考来源:练习实践-TLS协议01-Wireshark对https数据的解密练习实践-TLS协议01-客户端curl配合sslkey文件实现解密极客时间:网络排查案例课-实战二:应用层真实案例揭秘篇-20丨TLS加解密:如何解密HTTPS流量?客户端如何做TLS解密?这里说的客户端,包括了Chrome、Firefox等浏览......
  • SSL/TLS 深入浅出
    SSL,https(HTTPoverSSL),X.509,SSL证书,证书申请/导入/签发,等名词,想必有一定工作经验的小伙伴,一定都会略有耳闻,或者至少也听神边大神念叨过。虽然司空见惯,但是能够比较系统理清其中关系,能够从整体到局部深入浅出讲解下的人,估计至少也是十里挑一。反正没人给我讲,我只好......
  • 常用设计模式-单例模式(Singleton pattern)
    常用设计模式-单例模式(Singletonpattern)一、单例模式目的使用单例模式第一步要了解其作用,单例理解为一个实例(oneinstance)。保证一个类只有一个它的实例。在实际开发中,如线程池,数据库连接对象等。二、实现思路为了保证oneclassoneinstance①则需要保证实例全局唯一,保......
  • 解锁智能阅读新纪元:Kimi浏览器助手,让学习与工作效率飞跃提升
    Kimi浏览器助手:一键提升你的学习和工作效率!01浏览器新伙伴最近,国产大模型Kimi推出了一款令人瞩目的浏览器助手。这款助手以其便捷性和实用性,迅速成为提升学习和工作效率的利器。想象一下,当你在阅读海量资料时,只需点击一下,Kimi就能帮你总结文章要点,这是多么令人兴奋的事情!02......
  • kimi写代码:处理msgrcv返回E2BIG
    #include<stdio.h>#include<sys/ipc.h>#include<sys/msg.h>#include<string.h>#include<errno.h>typedefstruct{longmtype;charmtext[1024];//假设消息文本的最大长度为1024字节}message;intmain(){key_tkey=ftok(&......
  • AI办公自动化007:用kimi批量加密PDF文件
    文章目录一、介绍二、输入内容三、输出内容一、介绍使用kimichat实现对PDF文件进行加密二、输入内容在kimichat中输入提示词:你是一个Python编程专家,要完成一个编写Python脚本的任务,具体步骤如下:联网检索PyPDF2库的最新使用方法;打开文件夹:D:\2024-05读......
  • kimi写代码:c++ 线程池
    https://kimi.moonshot.cn/share/cqaberkdvond1bljn8sg在这个示例中:线程池创建了固定数量的工作线程。enqueue方法用于将任务添加到队列,并返回一个std::future对象,可用于获取任务的结果。每个工作线程在循环中等待任务分配,并在接收到任务后执行它。当线程完成分配的任务后......