zoukankan      html  css  js  c++  java
  • c++ 随机数 取值范围 多线程

     https://www.cnblogs.com/exciting/p/11162855.html

    #include <random>  
    
    std::random_device rd;                         // A function object for generating seeds
      std::mt19937 gen(rd());
      std::uniform_int_distribution<> dis(1, 6000);//取值 1-6000

    使用:
     dis(gen)

    线程延迟随机多少秒内启动:

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string.h>
    #include <thread>
    #include <random>
    
    using namespace std;
    using std::cout;
    
    class HelloWorld {
    public:
        HelloWorld(string url)
        {
            std::random_device rd;                         // A function object for generating seeds
            std::mt19937 gen(rd());
            std::uniform_int_distribution<> dis(1, 6000);//
    
            const int len = 100;
            std::thread threads[len];
            std::cout << "Spawning 5 threads...
    ";
            for (int i = 0; i < len; i++) {
                threads[i] = std::thread(&HelloWorld::thread_task, this, url, dis(gen));
            }
    
            std::cout << "Done spawning threads! Now wait for them to join
    ";
            for (auto& t : threads) {
                t.join();
            }
            std::cout << "All threads joined.
    ";
        }
    
        void thread_task(string url, int time) {
            std::this_thread::sleep_for(std::chrono::milliseconds(time));
            std::cout << "hello thread "
                << std::this_thread::get_id()
                << " paused " << time << " milliseconds" << std::endl;
        }
    };
    
    int main() {
     
        HelloWorld hw("url");
    
        return EXIT_SUCCESS;
    }

    输入任意, 不停的加入新线程:

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string.h>
    #include <thread>
    #include <random>
    
    using namespace std;
    using std::cout;
    
    const int len = 10;
    std::vector<std::thread> threads(10);
    
    class HelloWorld {
    public:
        HelloWorld(string url)
        {
            std::random_device rd;                         // A function object for generating seeds
            std::mt19937 gen(rd());
            std::uniform_int_distribution<> dis(1, 6000);//
    
    
            std::cout << "Spawning 5 threads...
    ";
            for (int i = 0; i < len; i++) {
                threads.push_back( std::thread(&HelloWorld::thread_task, this, url, dis(gen)));
            }
    /*
            std::cout << "Done spawning threads! Now wait for them to join
    ";
            for (auto& t : threads) {
                t.join();
            }
            std::cout << "All threads joined.
    ";
    
    */
        }
    
        void thread_task(string url, int time) {
            std::this_thread::sleep_for(std::chrono::milliseconds(time));
            std::cout << "hello thread "
                << std::this_thread::get_id()
                << " paused " << time << " milliseconds" << std::endl;
        }
    };
    
    int main() {
        //输入一个键就不停的循环加入
        char c = 'a';
        while (c != 'q') {
            HelloWorld hw("url");
            cout << "loop:";
            cin >> c;
        }
    
        return EXIT_SUCCESS;
    }
  • 相关阅读:
    学习ASP.NET Core(11)-解决跨域问题与程序部署
    学习ASP.NET Core(10)-全局日志与xUnit系统测试
    学习ASP.NET Core(09)-数据塑形与HATEOAS及内容协商
    学习ASP.NET Core(08)-过滤搜索与分页排序
    学习ASP.NET Core(07)-AOP动态代理与日志
    学习ASP.NET Core(06)-Restful与WebAPI
    学习ASP.NET Core(05)-使用Swagger与Jwt认证
    基于NACOS和JAVA反射机制动态更新JAVA静态常量非@Value注解
    DES 加密解密 文件工具类
    springboot-mybatis双数据源配置
  • 原文地址:https://www.cnblogs.com/bigben0123/p/13437838.html
Copyright © 2011-2022 走看看