Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问全局变量以修改值 我有一个C++程序,带有全局变量,如外部变量双变量>代码> 我想改变这个变量的值,当它由另一个C++程序运行时。 如何通过其他程序访问该变量并更改其值_C++ - Fatal编程技术网

访问全局变量以修改值 我有一个C++程序,带有全局变量,如外部变量双变量>代码> 我想改变这个变量的值,当它由另一个C++程序运行时。 如何通过其他程序访问该变量并更改其值

访问全局变量以修改值 我有一个C++程序,带有全局变量,如外部变量双变量>代码> 我想改变这个变量的值,当它由另一个C++程序运行时。 如何通过其他程序访问该变量并更改其值,c++,C++,例如: #include <iostream> extern double myvariable = 0.0; int main() { //waiting for modify value by another programs while (myvariable == 0.0) { } cout << myvariable << endl; return 0; } #包括 外部双myvaria

例如:

#include <iostream>
extern double myvariable = 0.0;



int main() {

    //waiting for modify value by another programs
    while (myvariable == 0.0)
    {
    }

    cout << myvariable << endl;


    return 0;
}
#包括
外部双myvariable=0.0;
int main(){
//等待其他程序修改值
while(myvariable==0.0)
{
}
不能从…开始。
您需要在两个进程中创建命名共享内存,并将变量存储在该空间中。这将允许两个进程访问变量

在父进程中:

  //Construct managed shared memory
  managed_shared_memory segment(create_only, "MySharedMemory", 65536);

  //Create an object of MyType initialized to {0.0, 0}
  MyType *instance = segment.construct<MyType>
     ("MyType instance")  //name of the object
     (0.0, 0);            //ctor first argument
//构造托管共享内存
托管共享内存段(仅创建“MySharedMemory”,65536);
//创建初始化为{0.0,0}的MyType对象
MyType*实例=段.construct
(“MyType实例”)//对象的名称
(0.0,0);//第一个参数
在子进程中:

  managed_shared_memory segment(open_only, "MySharedMemory");

  std::pair<MyType*, managed_shared_memory::size_type> res;

  //Find the object
  res = segment.find<MyType> ("MyType instance");
  //Length should be 1
  if(res.second != 1) return 1;
managed_共享_内存段(仅开放“MySharedMemory”);
std::pair res;
//找到目标
res=segment.find(“MyType实例”);
//长度应为1
如果(res.second!=1)返回1;

这是服务器进程:

#include <boost/interprocess/windows_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <cstring>

int main ()
{
   using namespace boost::interprocess;
   try{
      //Create a native windows shared memory object.
      windows_shared_memory shm (create_only, "shared_memory", read_write, 1000);

      //Map the whole shared memory in this process
      mapped_region region(shm, read_write);

      //Write all the memory to 1
      std::memset(region.get_address(), 1, 1000);

      //Launch the client process and wait until finishes...
      //...
   }
   catch(interprocess_exception &ex){
      std::cout << ex.what() << std::endl;
      return 1;
   }
   return 0;
}
#包括
#包括
#包括
#包括
int main()
{
使用名称空间boost::interprocess;
试一试{
//创建本机windows共享内存对象。
windows共享内存shm(仅创建“共享内存”,读写,1000);
//在此过程中映射整个共享内存
映射的_区域(shm,读_写);
//将所有内存写入1
std::memset(region.get_address(),11000);
//启动客户端进程并等待完成。。。
//...
}
捕获(进程间异常和ex){

std::你想让另一个程序来修改它吗?我想通过内存中的共享变量在两个程序之间建立关联。你可能想做一个函数来更改它,这将是操作系统特有的。是的,我想用另一个程序来更改该值
#include <boost/interprocess/windows_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <cstring>

int main ()
{
   using namespace boost::interprocess;
   try{
      //Open already created shared memory object.
      windows_shared_memory shm(open_only, "shared_memory", read_only);

      //Map the whole shared memory in this process
      mapped_region region (shm, read_only);

      //Check that memory was initialized to 1
      const char *mem = static_cast<char*>(region.get_address());
      for(std::size_t i = 0; i < 1000; ++i){
         if(*mem++ != 1){
            std::cout << "Error checking memory!" << std::endl;               
            return 1;
         }
      }

      std::cout << "Test successful!" << std::endl;
   }
   catch(interprocess_exception &ex){
      std::cout << "Unexpected exception: " << ex.what() << std::endl;
      return 1;
   }

   return 0;
}