• <i id='UmskO'><tr id='UmskO'><dt id='UmskO'><q id='UmskO'><span id='UmskO'><b id='UmskO'><form id='UmskO'><ins id='UmskO'></ins><ul id='UmskO'></ul><sub id='UmskO'></sub></form><legend id='UmskO'></legend><bdo id='UmskO'><pre id='UmskO'><center id='UmskO'></center></pre></bdo></b><th id='UmskO'></th></span></q></dt></tr></i><div id='UmskO'><tfoot id='UmskO'></tfoot><dl id='UmskO'><fieldset id='UmskO'></fieldset></dl></div>
  • <legend id='UmskO'><style id='UmskO'><dir id='UmskO'><q id='UmskO'></q></dir></style></legend>

      <tfoot id='UmskO'></tfoot>

        <small id='UmskO'></small><noframes id='UmskO'>

        • <bdo id='UmskO'></bdo><ul id='UmskO'></ul>

      1. 如何只执行一段代码一次?

        How to execute a piece of code only once?(如何只执行一段代码一次?)
        <legend id='tYIHg'><style id='tYIHg'><dir id='tYIHg'><q id='tYIHg'></q></dir></style></legend>

        <small id='tYIHg'></small><noframes id='tYIHg'>

          <tbody id='tYIHg'></tbody>
          • <bdo id='tYIHg'></bdo><ul id='tYIHg'></ul>

                <i id='tYIHg'><tr id='tYIHg'><dt id='tYIHg'><q id='tYIHg'><span id='tYIHg'><b id='tYIHg'><form id='tYIHg'><ins id='tYIHg'></ins><ul id='tYIHg'></ul><sub id='tYIHg'></sub></form><legend id='tYIHg'></legend><bdo id='tYIHg'><pre id='tYIHg'><center id='tYIHg'></center></pre></bdo></b><th id='tYIHg'></th></span></q></dt></tr></i><div id='tYIHg'><tfoot id='tYIHg'></tfoot><dl id='tYIHg'><fieldset id='tYIHg'></fieldset></dl></div>

              1. <tfoot id='tYIHg'></tfoot>
                  本文介绍了如何只执行一段代码一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个应用程序,其中包含多个功能.每个函数都可以根据用户输入多次调用.但是,我只需要在一个函数中执行一小段代码,最初是在应用程序启动时.当稍后再次调用相同的函数时,不得执行这段特定的代码.代码在 VC++ 中.请告诉我处理这个问题的最有效方法.

                  I have an application which has several functions in it. Each function can be called many times based on user input. However I need to execute a small segment of the code within a function only once, initially when the application is launched. When this same function is called again at a later point of time, this particular piece of code must not be executed. The code is in VC++. Please tell me the most efficient way of handling this.

                  推荐答案

                  使用具有构造函数的全局静态对象(在 main 之前调用)?或者只是在例行程序中

                  Use global static objects with constructors (which are called before main)? Or just inside a routine

                  static bool initialized;
                  if (!initialized) {
                     initialized = true;
                     // do the initialization part
                  }
                  

                  很少有这种速度不够快的情况!

                  There are very few cases when this is not fast enough!

                  在多线程上下文中,这可能还不够:

                  In multithreaded context this might not be enough:

                  您可能还对 pthread_once 或 constructor 函数 __attribute__ GCC.

                  You may also be interested in pthread_once or constructor function __attribute__ of GCC.

                  对于 C++11,您可能需要 std::call_once.

                  With C++11, you may want std::call_once.

                  您可能想要使用 如果您的函数可以从多个线程调用,也许可以声明 static volatile std::atomic_bool 初始化;(但您需要小心).

                  You may want to use <atomic> and perhaps declare static volatile std::atomic_bool initialized; (but you need to be careful) if your function can be called from several threads.

                  但是这些可能在您的系统上不可用;它们在 Linux 上可用!

                  这篇关于如何只执行一段代码一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                    1. <legend id='kJCNL'><style id='kJCNL'><dir id='kJCNL'><q id='kJCNL'></q></dir></style></legend>
                        <tbody id='kJCNL'></tbody>
                    2. <tfoot id='kJCNL'></tfoot>

                        <small id='kJCNL'></small><noframes id='kJCNL'>

                        1. <i id='kJCNL'><tr id='kJCNL'><dt id='kJCNL'><q id='kJCNL'><span id='kJCNL'><b id='kJCNL'><form id='kJCNL'><ins id='kJCNL'></ins><ul id='kJCNL'></ul><sub id='kJCNL'></sub></form><legend id='kJCNL'></legend><bdo id='kJCNL'><pre id='kJCNL'><center id='kJCNL'></center></pre></bdo></b><th id='kJCNL'></th></span></q></dt></tr></i><div id='kJCNL'><tfoot id='kJCNL'></tfoot><dl id='kJCNL'><fieldset id='kJCNL'></fieldset></dl></div>

                            <bdo id='kJCNL'></bdo><ul id='kJCNL'></ul>