首页 > 其他分享 >atomically try catch and avoid throw exception

atomically try catch and avoid throw exception

时间:2023-05-08 23:14:05浏览次数:35  
标签:std exception now chrono avoid h1 try time include

#include <assert.h>
#include <atomic>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <thread>
#include <unistd.h>
#include <uuid/uuid.h>

std::string get_time_now()
{
    std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    struct tm tm_info = *localtime(&raw_time);
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S");
    return ss.str();
}

void divideDemo(int num = 0)
{
    for (int i = -10; i < 10; i++)
    {
        try
        {
            double d = (double)num / (double)i;
            if (d != std::numeric_limits<double>::infinity())
            {
                std::cout << std::setprecision(10) << std::fixed << "In divide " << (double)num / (double)i << std::endl;
            }
        }
        catch (...)
        {            
        }
    }
}

void multiplyX(int num)
{
    try
    {
        for (int i = -10; i < 10; i++)
        {
            std::cout << "In multiply " << i*num << std::endl;
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }
}

void mt_try_catch(int num)
{
    try
    {
        std::thread t1(divideDemo, num);
        t1.join();
        std::thread t2(multiplyX, num);        
        t2.join();
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }

    std::cout << get_time_now() << ",finish in " << __FUNCTION__ << std::endl;
}

int main(int args, char **argv)
{
    mt_try_catch(atoi(argv[1]));
}
//Compile:
g++ -std=c++2a -I. *.cpp -o h1 -luuid -lpthread;
//Run
./h1 10;

 

标签:std,exception,now,chrono,avoid,h1,try,time,include
From: https://www.cnblogs.com/Fred1987/p/17383453.html

相关文章

  • java.lang.NullPointerException解决方案,以及发生的原因。
    其实解决方案,倒是挺简单的,eclipse的console里,报错的地方点一下,就知道在哪一行了。真正要注意的是怎么避免这种情况的发生。通常情况下,这种错,发生在取数据的过程,比方从数据库查询数据,亦或者说是,发生在数据传输的过程中。譬如1packagetest;23importjava.util.List;......
  • MyBatis-Plus和PageHelper冲突导致Factory method sqlSessionFactory threw exception
    springboot开始引入了mybaits-plus。后来想引入pagehelper进行分页,引入之后报错ErrorstartingApplicationContext.Todisplaytheconditionsreportre-runyourapplicationwith'debug'enabled.13:48:24.428ERRORo.s.boot.SpringApplication[845]-Applicationrun......
  • XUnit —— Record.Exception —— Stop Using Assert.Throws in Your BDD Unit Tests
    原文:https://www.richard-banks.org/2015/07/stop-using-assertthrows-in-your-bdd.htmlStopUsingAssert.ThrowsinYourBDDUnitTests I’msurewe’veallseenthe Assert.Throws assertionintestcodebynow,butjustincaseyouhaven’t,here'sasimple......
  • java.io.FileNotFoundException: class path resource [bean.xml] cannot be opened b
    出现这个报错Exceptioninthread"main"org.springframework.beans.factory.BeanDefinitionStoreException:IOExceptionparsingXMLdocumentfromclasspathresource[bean.xml];nestedexceptionisjava.io.FileNotFoundException:classpathresource[bean.......
  • [docker]mysql的docker镜像中docker-entrypoint-initdb.d目录的妙用
    docker-entrypoint-initdb.d是Docker官方MySQL镜像中的一个目录,用于初始化数据库。在该目录下,可以放置一些SQL脚本文件,MySQL会在容器启动时自动执行这些脚本文件,用于创建用户、创建数据库、创建表等操作。具体来说,当MySQL镜像启动时,其entrypoint.sh脚本会检查是否存在......
  • 通过IIS设置来解决System.BadImageFormatException错误
    工作时换了新电脑,然后运行发布后MVC程序就报错:    直接运行Code是OK。错误的原因肯定是64位系统调用了32bit的dll。尝试修改project的Targe为x86,还是无法解决问题。最后查看资料,将应用程序池修改为启用32bit就可以了。 ......
  • Jenkins 执行Docker build错误Got permission denied while trying to connect to the
    问题: 解决方法:这个报错为权限问题1.把jenkins用户,加到docker用户组 #如果没有docker用户组,先创建用户组:groupadddocker#添加jenkins用户到用户组:sudousermod-a-Gdockerjenkins-a<追加>必须与-G选项一起使用,把用户追加到某些组中。-G<群组>修改用户所属的......
  • 第4-0讲,entry 输入控件
    当你需要在页面上输入信息时,比如用户名和密码,那么你需要使用Entry控件importtkinterastkfromtkinterimportmessagebox#创建窗体window=tk.Tk()#设置窗体标题window.title("标签属性")#设置窗体的图标window.iconbitmap('../images/米饭.ico')#设置窗体大小windo......
  • java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/
    Java使用AES/CBC/PKCS7Padding加解密时会报错,因为原生JDK不支持。1.在jdk中的jre\lib\security修改java.security文件,替换security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider2./jdk/jre/lib/ext下添加jar包bcprov-jdk15on-1.58.jar ......
  • FAQ-virtualbox安装centOS7 报错 not syncing fatal exception
    1、假设你的虚拟机的名字为:CentOS7提示:已经建完(未启动)2、在命令行窗户执行:VBoxManagemodifyvmCentOS7--spec-ctrlonVBoxManage命令所在的目录为:c:\ProgramFiles\Oracle\VirtualBox>3、启动CentOS7......