1

共有ライブラリに次の問題があります

共有ライブラリを作成する

class A
{
  static int callCount;
  A() { callCount++; }
}
int A:callCount = 0;
class Main
{
  Main()
  {
    A a1();
    A a2();
  }
}

この共有ライブラリをさらに何度もロードするプロセスを作成し、callCount がプロセス全体ではなく共有ライブラリのみに属するようにしたい

dlopen("shared.so", RTLD_LAZY);
// after some code i can construct Main() 
// and now i will open the shared object again
dlopen("shared.so", RTLD_LAZY);
// now if i construct Main from the new library i want to have a new 
// initialized callCount eq 0 but its 2

どうすればこの問題を解決できますか

4

1 に答える 1

0

実行時に読み込まれるライブラリの読み込み、特に再読み込みのセマンティクスは、プラットフォームと実装に非常に固有です。どのような種類のグローバル状態にも依存せず、代わりに明示的な初期化関数をライブラリに渡すことをお勧めします。たぶんそうです:

library_context_t context;

/* dlopen, dlsym, ... */

library_init(&context);

library_do_thing(&context);

// ...

library_destroy(&context);

// maybe unload, or reuse

特に Linux は、すでにロードされているライブラリをリロードしません。

于 2012-09-05T09:41:38.820 に答える