与AI合作 -- 单例工厂2遗留的问题:bard的错误

2024-01-10 17:52

本文主要是介绍与AI合作 -- 单例工厂2遗留的问题:bard的错误,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题

上一节我们针对函数内静态变量初始化在多线程环境中要不要用锁保护提出了疑问,代码如下:


class Singleton {
public:static Singleton& getInstance() {std::lock_guard<std::mutex> lock(mutex); // Acquire lock for thread safetystatic Singleton instance; // Local static variable for thread safetyreturn instance;

结论

不必。C++11已经自己加锁了。

__cxa_guard_acquire

为了简单,我们先简单写个试验程序:

class Product {
public:Product(){std::cout<<"Product constructor"<<std::endl;}
};void test(){std::cout<<"I am in test()"<<std::endl;static Product pro;std::cout<<"leaving test()"<<std::endl;
}int main() {std::thread first(test);std::thread second(test);first.join();second.join();test();return 0;
}

直接用GDB或者objdump看下test函数对应的汇编:

(gdb) disass test
Dump of assembler code for function test():0x0000000000400e26 <+0>:     push   %rbp0x0000000000400e27 <+1>:     mov    %rsp,%rbp0x0000000000400e2a <+4>:     push   %r120x0000000000400e2c <+6>:     push   %rbx0x0000000000400e2d <+7>:     mov    $0x401808,%esi0x0000000000400e32 <+12>:    mov    $0x6030c0,%edi0x0000000000400e37 <+17>:    callq  0x400ca0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>0x0000000000400e3c <+22>:    mov    $0x400c40,%esi0x0000000000400e41 <+27>:    mov    %rax,%rdi0x0000000000400e44 <+30>:    callq  0x400cd0 <_ZNSolsEPFRSoS_E@plt>0x0000000000400e49 <+35>:    movzbl 0x202390(%rip),%eax        # 0x6031e0 <_ZGVZ4testvE3pro>0x0000000000400e50 <+42>:    test   %al,%al0x0000000000400e52 <+44>:    sete   %al0x0000000000400e55 <+47>:    test   %al,%al0x0000000000400e57 <+49>:    je     0x400e86 <test()+96>0x0000000000400e59 <+51>:    mov    $0x6031e0,%edi0x0000000000400e5e <+56>:    callq  0x400d20 <__cxa_guard_acquire@plt>0x0000000000400e63 <+61>:    test   %eax,%eax0x0000000000400e65 <+63>:    setne  %al0x0000000000400e68 <+66>:    test   %al,%al0x0000000000400e6a <+68>:    je     0x400e86 <test()+96>0x0000000000400e6c <+70>:    mov    $0x0,%r12d0x0000000000400e72 <+76>:    mov    $0x6031d9,%edi0x0000000000400e77 <+81>:    callq  0x40103a <Product::Product()>0x0000000000400e7c <+86>:    mov    $0x6031e0,%edi0x0000000000400e81 <+91>:    callq  0x400c70 <__cxa_guard_release@plt>0x0000000000400e86 <+96>:    mov    $0x401817,%esi0x0000000000400e8b <+101>:   mov    $0x6030c0,%edi0x0000000000400e90 <+106>:   callq  0x400ca0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>0x0000000000400e95 <+111>:   mov    $0x400c40,%esi0x0000000000400e9a <+116>:   mov    %rax,%rdi0x0000000000400e9d <+119>:   callq  0x400cd0 <_ZNSolsEPFRSoS_E@plt>0x0000000000400ea2 <+124>:   jmp    0x400ec1 <test()+155>0x0000000000400ea4 <+126>:   mov    %rax,%rbx0x0000000000400ea7 <+129>:   test   %r12b,%r12b0x0000000000400eaa <+132>:   jne    0x400eb6 <test()+144>0x0000000000400eac <+134>:   mov    $0x6031e0,%edi0x0000000000400eb1 <+139>:   callq  0x400c60 <__cxa_guard_abort@plt>0x0000000000400eb6 <+144>:   mov    %rbx,%rax0x0000000000400eb9 <+147>:   mov    %rax,%rdi0x0000000000400ebc <+150>:   callq  0x400d10 <_Unwind_Resume@plt>0x0000000000400ec1 <+155>:   pop    %rbx0x0000000000400ec2 <+156>:   pop    %r120x0000000000400ec4 <+158>:   pop    %rbp0x0000000000400ec5 <+159>:   retq

简单提一下_ZGVZ4testvE3pro demangle一下(第一次值是0,初始化后是1):

[mzhai@singletonFactoryAi]$ c++filt _ZGVZ4testvE3pro
guard variable for test()::pro

 着重看下面的__cxa_guard_acquire

看到__cxa_guard_acquire、__cxa_guard_release没?中间夹着Product::Product()

即使没见过这两个函数,也能猜到和static变量初始化有关,防止多个线程同时初始化一个静态变量. 这是我把__cxa_guard_acquire输入搜索引擎搜到的:

C++ constructor guards for static instances

Introduction

The libstd++ ABI requires implementations of the following functions:

int __cxa_guard_acquire(__guard *g);

void __cxa_guard_release(__guard *g);

void __cxa_guard_abort(__guard *g);

These functions are not intended to be ever used by the programmer, instead the C++ compiler will emit code calling them if statically allocated class instances are used. In a multi-threaded environment special care needs to be taken to prevent race conditions while initializing and using such instances. This modules provides them.

Usage

This module is intended to be used by platforms that want to provide C++ support, but the used standard C++ library does not provide these guards. In this case, adding this module will do the trick. The programmer / user should never interact with any of the functions.

Note that on some platforms the type __guard is defined differently from the "generic" definition, most notably ARM. For those platforms a header named cxx_ctor_guards_arch.h needs to be created containing the correct typedef and the preprocessor macro CXX_CTOR_GUARDS_CUSTOM_TYPE needs to be defined.

https://opensource.apple.com/source/libcppabi/libcppabi-14/src/cxa_guard.cxx 

int __cxxabiv1::__cxa_guard_acquire(uint64_t* guard_object)
{// Double check that the initializer has not already been runif ( initializerHasRun(guard_object) )return 0;// We now need to acquire a lock that allows only one thread// to run the initializer.  If a different thread calls// __cxa_guard_acquire() with the same guard object, we want // that thread to block until this thread is done running the // initializer and calls __cxa_guard_release().  But if the same// thread calls __cxa_guard_acquire() with the same guard object,// we want to abort.  // To implement this we have one global pthread recursive mutex // shared by all guard objects, but only one at a time.  

用GDB 模拟race condition

如果读者感兴趣,可以调试一下我们给出的程序:

(gdb) b __cxa_guard_acquire       使得两个线程都停在__cxa_guard_acquire
Breakpoint 2 at 0x7ffff7acf980
(gdb) r
Continuing.
Thread 2 "a.out" hit Breakpoint 2, 0x00007ffff7acf980 in __cxa_guard_acquire () from /lib64/libstdc++.so.6Thread 3 "a.out" hit Breakpoint 2, 0x00007ffff7acf980 in __cxa_guard_acquire () from /lib64/libstdc++.so.6
Quit(gdb) where
Selected thread is running.(gdb) info thread                2、3线程都停在了__cxa_guard_acquireId   Target Id                                   Frame
* 1    Thread 0x7ffff7fe1740 (LWP 2468153) "a.out" (running)2    Thread 0x7ffff6eb9700 (LWP 2468165) "a.out" 0x00007ffff7acf980 in __cxa_guard_acquire () from /lib64/libstdc++.so.63    Thread 0x7ffff66b8700 (LWP 2468166) "a.out" 0x00007ffff7acf980 in __cxa_guard_acquire () from /lib64/libstdc++.so.6(gdb) b Product::Product thread 2       
Breakpoint 3 at 0x401046: file static_var_in_func.cpp, line 36.
(gdb) thread apply 2 c            让第二个线程获得锁,先停在静态变量初始化中
Thread 2 (Thread 0x7ffff6eb9700 (LWP 2468165)):
Continuing.
Thread 2 "a.out" hit Breakpoint 3, Product::Product (this=0x6031d9 <test()::pro>) at static_var_in_func.cpp:36
36                      std::cout<<"Product constructor"<<std::endl;(gdb) thread apply 3 c            让第三个线程(还没获得锁)继续运行,应该卡在__cxa_guard_acquire 
Thread 3 (Thread 0x7ffff66b8700 (LWP 2468166)):
Continuing.
^C
Thread 1 "a.out" received signal SIGINT, Interrupt.
0x00007ffff78226cd in __pthread_timedjoin_ex () from /lib64/libpthread.so.0
(gdb) thread 3
[Switching to thread 3 (Thread 0x7ffff66b8700 (LWP 2468166))](running)
(gdb) where
Selected thread is running.
(gdb) interrupt
(gdb)
Thread 3 "a.out" stopped.
0x00007ffff72759bd in syscall () from /lib64/libc.so.6
Quit
(gdb) where                       正如预期,第三个线程卡在获得锁上
#0  0x00007ffff72759bd in syscall () from /lib64/libc.so.6
#1  0x00007ffff7acfa3f in __cxa_guard_acquire () from /lib64/libstdc++.so.6
#2  0x0000000000400e63 in test () at static_var_in_func.cpp:42
#3  0x0000000000401205 in std::__invoke_impl<void, void (*)()> (__f=@0x616008: 0x400e26 <test()>) at /usr/include/c++/8/bits/invoke.h:60
#4  0x0000000000401085 in std::__invoke<void (*)()> (__fn=@0x616008: 0x400e26 <test()>) at /usr/include/c++/8/bits/invoke.h:95
#5  0x00000000004016d4 in std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul> (this=0x616008) at /usr/include/c++/8/thread:244
#6  0x00000000004016aa in std::thread::_Invoker<std::tuple<void (*)()> >::operator() (this=0x616008) at /usr/include/c++/8/thread:253
#7  0x000000000040168e in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run (this=0x616000) at /usr/include/c++/8/thread:196
#8  0x00007ffff7afbb23 in execute_native_thread_routine () from /lib64/libstdc++.so.6
#9  0x00007ffff78211ca in start_thread () from /lib64/libpthread.so.0
#10 0x00007ffff7275e73 in clone () from /lib64/libc.so.6

AI的回答

mzhai: Assume there is a static variable A which is an object of a class, this static variable is defined in a function named funA. When is A initialized? and is there a race condition if two threads call funA concurrently?

ChatGPT: In C++, static variables inside functions (local static variables) are initialized the first time the program execution encounters their declaration within that function. They're initialized in a thread-safe manner in compliance with the C++11 standard.

For instance, if you have a function funA with a static variable A inside it, like this:

void funA() { static MyClass A; // ... } 

When funA is called for the first time, the static MyClass A; line will be executed. This initialization occurs once, and subsequent calls to funA won't reinitialize A. This initialization is thread-safe in C++11 and later, meaning the standard guarantees that it will be properly handled in a multi-threaded context. There won't be a race condition during this initialization.

Bard

Here's a breakdown of the initialization and potential race condition:

1. Initialization of static variable A:

  • When: The static variable A will be initialized the first time control flow reaches its declaration within funA, even if it's not explicitly used in that particular call.
  • How: The constructor of the class will be invoked to initialize A's members.
  • Subsequent calls: In subsequent calls to funA, the already initialized instance of A will be used, and the constructor won't be called again.

2. Race condition in multi-threaded environment:

  • Yes, a race condition can occur if two threads call funA concurrently before A has been initialized.
  • Scenario:
    • Both threads enter funA for the first time.
    • Both threads see that A hasn't been initialized yet.
    • Both threads attempt to create an instance of A, leading to multiple initializations and potential data corruption or crashes.

 可见在这方面Bard不如Chat GPT正确。

他山之石

后来搜到一篇外文博客,读者可以作为参考。Adventures in Systems Programming: C++ Local Statics - In Pursuit of Laziness

这篇关于与AI合作 -- 单例工厂2遗留的问题:bard的错误的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/591602

相关文章

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

AI绘图怎么变现?想做点副业的小白必看!

在科技飞速发展的今天,AI绘图作为一种新兴技术,不仅改变了艺术创作的方式,也为创作者提供了多种变现途径。本文将详细探讨几种常见的AI绘图变现方式,帮助创作者更好地利用这一技术实现经济收益。 更多实操教程和AI绘画工具,可以扫描下方,免费获取 定制服务:个性化的创意商机 个性化定制 AI绘图技术能够根据用户需求生成个性化的头像、壁纸、插画等作品。例如,姓氏头像在电商平台上非常受欢迎,

从去中心化到智能化:Web3如何与AI共同塑造数字生态

在数字时代的演进中,Web3和人工智能(AI)正成为塑造未来互联网的两大核心力量。Web3的去中心化理念与AI的智能化技术,正相互交织,共同推动数字生态的变革。本文将探讨Web3与AI的融合如何改变数字世界,并展望这一新兴组合如何重塑我们的在线体验。 Web3的去中心化愿景 Web3代表了互联网的第三代发展,它基于去中心化的区块链技术,旨在创建一个开放、透明且用户主导的数字生态。不同于传统

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

AI一键生成 PPT

AI一键生成 PPT 操作步骤 作为一名打工人,是不是经常需要制作各种PPT来分享我的生活和想法。但是,你们知道,有时候灵感来了,时间却不够用了!😩直到我发现了Kimi AI——一个能够自动生成PPT的神奇助手!🌟 什么是Kimi? 一款月之暗面科技有限公司开发的AI办公工具,帮助用户快速生成高质量的演示文稿。 无论你是职场人士、学生还是教师,Kimi都能够为你的办公文

Andrej Karpathy最新采访:认知核心模型10亿参数就够了,AI会打破教育不公的僵局

夕小瑶科技说 原创  作者 | 海野 AI圈子的红人,AI大神Andrej Karpathy,曾是OpenAI联合创始人之一,特斯拉AI总监。上一次的动态是官宣创办一家名为 Eureka Labs 的人工智能+教育公司 ,宣布将长期致力于AI原生教育。 近日,Andrej Karpathy接受了No Priors(投资博客)的采访,与硅谷知名投资人 Sara Guo 和 Elad G

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

购买磨轮平衡机时应该注意什么问题和技巧

在购买磨轮平衡机时,您应该注意以下几个关键点: 平衡精度 平衡精度是衡量平衡机性能的核心指标,直接影响到不平衡量的检测与校准的准确性,从而决定磨轮的振动和噪声水平。高精度的平衡机能显著减少振动和噪声,提高磨削加工的精度。 转速范围 宽广的转速范围意味着平衡机能够处理更多种类的磨轮,适应不同的工作条件和规格要求。 振动监测能力 振动监测能力是评估平衡机性能的重要因素。通过传感器实时监

缓存雪崩问题

缓存雪崩是缓存中大量key失效后当高并发到来时导致大量请求到数据库,瞬间耗尽数据库资源,导致数据库无法使用。 解决方案: 1、使用锁进行控制 2、对同一类型信息的key设置不同的过期时间 3、缓存预热 1. 什么是缓存雪崩 缓存雪崩是指在短时间内,大量缓存数据同时失效,导致所有请求直接涌向数据库,瞬间增加数据库的负载压力,可能导致数据库性能下降甚至崩溃。这种情况往往发生在缓存中大量 k