1

我有一个带有静态方法的类,它有一个局部静态变量。我希望该变量被计算/评估一次(我第一次调用该函数)并且对于任何后续调用,它不再被评估。怎么做?这是我的课:

template<
    typename T1 = int, unsigned N1 = 1,
    typename T2 = int, unsigned N2 = 0,
    typename T3 = int, unsigned N3 = 0,
    typename T4 = int, unsigned N4 = 0,
    typename T5 = int, unsigned N5 = 0,
    typename T6 = int, unsigned N6 = 0,
    typename T7 = int, unsigned N7 = 0,
    typename T8 = int, unsigned N8 = 0,
    typename T9 = int, unsigned N9 = 0,
    typename T10 = int, unsigned N10 = 0,
    typename T11 = int, unsigned N11 = 0,
    typename T12 = int, unsigned N12 = 0,
    typename T13 = int, unsigned N13 = 0,
    typename T14 = int, unsigned N14 = 0,
    typename T15 = int, unsigned N15 = 0,
    typename T16 = int, unsigned N16 = 0>
struct GroupAlloc
{
    static const uint32_t sizeClass;
    static uint32_t getSize()
    {
        static uint32_t totalSize = 0;

        totalSize += sizeof(T1)*N1;
        totalSize += sizeof(T2)*N2;
        totalSize += sizeof(T3)*N3;
        totalSize += sizeof(T4)*N4;

        totalSize += sizeof(T5)*N5;
        totalSize += sizeof(T6)*N6;
        totalSize += sizeof(T7)*N7;
        totalSize += sizeof(T8)*N8;

        totalSize += sizeof(T9)*N9;
        totalSize += sizeof(T10)*N10;
        totalSize += sizeof(T11)*N11;
        totalSize += sizeof(T12)*N12;

        totalSize += sizeof(T13)*N13;
        totalSize += sizeof(T14)*N14;
        totalSize += sizeof(T15)*N15;
        totalSize += sizeof(T16)*N16;

        totalSize = 8*((totalSize + 7)/8);

        return totalSize;
    }
};

编辑:

感谢大家的及时帮助。对所有人+1。我选择了 Tyler McHenry 的答案,因为它不需要任何比较,纯静态函数评估。我将需要此代码用于分配器,因此避免另一个“如果”应该更好。再次感谢!

编辑:

gf 的答案被证明是最好的答案,因为它在编译时处理分配并将程序从线程安全头痛和显式初始化中拯救出来。但是,我尊重以前的最佳答案。我将在此处给予信用,而不是更改刻度线。感谢大家的帮助!

4

4 回答 4

4

制作另一个进行计算的静态函数,并将其用于变量的初始化,例如

static uint32_t computeSize() 
{
  uint32_t init_totalSize;

  // Lots of code

  return init_totalSize;
}

static uint32_t getSize()
{
  static uint32_t totalSize = computeSize();
  return totalSize;
}

静态变量保证只被初始化一次(第一次使用包含它们的函数)。

编辑:但这不是线程安全的。本页详细解释了原因

为了使其线程安全,将totalSize(对 的调用computeSize)的初始化包装在临界区是不够的,因为静态变量初始化是“编译器魔法”,它可以是变量在运行期间随时进行初始化getSize使用之前的调用,甚至在函数的第一条语句之前。您需要做的是防止多个线程getSize同时调用,这可以通过另一种间接级别来完成,例如

static uint32_t computeSize() 
{
  uint32_t init_totalSize;

  // Lots of code

  return init_totalSize;
}

static uint32_t real_getSize()
{
  static uint32_t totalSize = computeSize();
  return totalSize;
}

static uint32_t getSize()
{
  uint32_t totalSize;
  /* --- Enter Critical Section (acquire lock) -- */
  totalSize = real_getSize();
  /* --- Exit Critical Section (release lock) -- */
  return totalSize;
}

这可以防止两个线程同时进入包含静态变量的函数,并确保其初始化将发生在临界区。

于 2010-03-20T12:00:50.997 回答
3

将计算移到辅助函数中:

static uint32_t totalSize = calculateTotalSize();

totalSize只有在初始化时才会调用辅助函数。

有点晚了,但你为什么要在这里进行(可能)运行时计算?使用编译时常量,你甚至不会有任何线程问题:

template<
  typename T1, unsigned N1,
  typename T2, unsigned N2,
  /* ... */
>
struct totalSize {
    static const uint32_t sum = 
        sizeof(T1)*N1
      + sizeof(T2)*N2
      /* ... */
      ;
    static const uint32_t value =
        8*((sum + 7)/8);
};

uint32_t GroupAlloc::getSize() {
    return totalSize<T1,N1,T2,N2,/*...*/>::value;
}
于 2010-03-20T11:59:31.077 回答
2

就像是:

static uint32_t getSize()
{
    static uint32_t totalSize = 0;
    static bool computed = 0;
    if(computed)
      return totalSize;
    computed = 1;
    // ... go on with your computation

会成功的。请注意,它不是线程安全的。

于 2010-03-20T12:01:33.660 回答
2
static uint32_t totalSize = 0;    // initialisation performed once only
if ( totalSize == 0 ) {
        totalSize += sizeof(T1)*N1;
        totalSize += sizeof(T2)*N2;
        totalSize += sizeof(T3)*N3;
        totalSize += sizeof(T4)*N4;
        // etc
}
于 2010-03-20T12:02:16.953 回答