In my project, I need a mechanism to share a variable between the MAIN_MODULE and SIDE_MODULE. Additionally, the side module should access this variable during initialization. I discovered that if both modules are compiled with the USE_PTHREADS=1 flag, they share the same virtual memory. However, I am unable to pass the variable's address from the main module to the side module during its initialization.
To work around this, I opted to use a constant memory address to store the address of the shared variable. When the main module starts, it sets the shared variable's address at this constant location. This allows the side module, upon loading, to read the constant address and access the shared variable.
I'm curious if there is a more efficient or elegant solution to this problem?
Reproduce demo
Here is a demo to reproduce my way to solve the problem:
In a.cpp:
Background
In my project, I need a mechanism to share a variable between the MAIN_MODULE and SIDE_MODULE. Additionally, the side module should access this variable during initialization. I discovered that if both modules are compiled with the USE_PTHREADS=1 flag, they share the same virtual memory. However, I am unable to pass the variable's address from the main module to the side module during its initialization.
To work around this, I opted to use a constant memory address to store the address of the shared variable. When the main module starts, it sets the shared variable's address at this constant location. This allows the side module, upon loading, to read the constant address and access the shared variable.
I'm curious if there is a more efficient or elegant solution to this problem?
Reproduce demo
Here is a demo to reproduce my way to solve the problem: In a.cpp:
In b.cpp:
Use the following commands to compile them:
Using shared memory may need a HTTP server. Here is a simple way to setup it:
Use
python3 xxx.py
to setup a https server.If everything works well, you should get the following output in console:
Version of emscripten/emsdk: