0

言語: C++/C Android Makefile システム: https://developer.android.com/ndk/guides/android_mk

共有ライブラリを開くアプリケーションがあり、foo.sofoo.so 内で他の 3 つの共有ライブラリを開きbar1.sobar2.so3つのbar3.so異なるスレッド (pthread_create) で同じアプリケーション/プロセスで開きます。pid は同じですが、スレッド ID は bar1、bar2、bar3 で異なります

bar1.sobar2.so、のそれぞれにafterを使用しbar3.soEntry呼び出される関数があり、さまざまなタイプのオブジェクトがライブラリ内に作成されます。dlsymdlopenbar1 2 3.so

bar1.sobar2.soまた、いくつかの共通の構造体/ルーチンを持つ、bar3.so呼び出された別の共有ライブラリへの動的リンクもあります。明示的に「dlopen」されていません。bar1.so、bar2.so、bar3.so の make ファイルで、必要な共有ライブラリとして指定します。のそれぞれにシングルトン構造を作成したい、およびとの間で読み書きできる。baz.sobaz.sobaz.sobaz.sobar1.sobar2.sobar3.so

baz.sobutbar1.soで静的オブジェクトを作成しようとしましたbar2.sobar3.so、構造の個別のコピーを取得しているようです。共有されていません。個別のコピーとは、以下のコード スニペットによると、 bar2.so を読み取るsharedData.aと、22 ではなく 10 であることを意味します。

baz.so: 
Filename: sharedobject.h -> All code is defined in sharedobject.h file in baz.so
struct Config {
    int a;
    bool b;
    // Constructor
    Config (int val1, bool val2) {
        a = val1;
        b = val2;
    }
};
class SharedObject {
public:
    Config sharedData;
    SharedObject() {
       // Initialize some common data here which will be read
       // and written to
       sharedData.a = 10;
       sharedData.b = 20;
    }
    static SharedObject* GetInstance() {
        static SharedObject singletonObjectInstance;
        return &singletonObjectInstance;
    }
    // Data update function called by either of bar1, bar2 or bar3.so 
    static void UpdateData(Config &data) {
        // Lock : Proper Locking is there but not shown in code
        SharedObject::GetInstance()->sharedData.a = data.a;
        // Unlock
    } 
}
Android.mk:
LOCAL_SRC_FILES :=  // Empty fields 
LOCAL_INC_FILES :=  // Empty Fields
LOCAL_MODULE := baz
include $(BUILD_SHARED_LIBRARY) # This will build baz.so
bar1.so: Let's say Thread1 under ProcessX updates some data
Filename: classx.cpp
#include "sharedobject.h"
   void ClassX::WriteToCommonData() {
      SharedObject *pSharedObject= SharedObject::GetInstance();
      print("Object Address:%p", pSharedObject); // These addresses are not same in bar1 and bar2 which is the problem
      Config data(22, false);
      SharedObject::UpdateData(data);
   }
In Android.mk of bar1.so
Linking is done by specifying shared lib: -> https://developer.android.com/ndk/guides/android_mk#local_shared_libraries
LOCAL_SRC_FILES := classx.cpp
LOCAL_SHARED_LIBRARIES := baz.so
LOCAL_CFLAGS :=   # No Special flags used.
LOCAL_MODULE := bar1
include $(BUILD_SHARED_LIBRARY) # This will build bar1.so 

bar2.so: Lets's say Thread2 under same ProcessX wants to read it now
Filename: classy.cpp
#include "sharedobject.h"
   void ClassY::ReadFromCommonData() {
       SharedObject *pSharedObject= SharedObject::GetInstance();
       print("Object Address:%p", pSharedObject); // These addresses are not same in bar1 and bar2 which is the problem
       // Read values updated by bar1.so
       print(pSharedObject->sharedData.a); // It is 10, not 22
   } 
In Android.mk

LOCAL_SHARED_LIBRARIES := baz.so
LOCAL_SRC_FILES := classy.cpp
LOCAL_CFLAGS :=   # No Special flags used.
LOCAL_MODULE := bar2
include $(BUILD_SHARED_LIBRARY) # This will build bar2.so 

すべてのバー ライブラリが同じプロセスの下にありますが、それでも共有メモリ API を使用する必要がありますか?

これらのリンクを確認しましたが、私の質問には答えていないようです: 2 つのアプリケーション間で単一の共有ライブラリ (*.so) インスタンスを共有する方法 共有ライブラリ構造

4

2 に答える 2