1つのオプションは、マスタプロセスとスレーブプロセスの両方に同じオブジェクトのインスタンスを作成させることです。マスタープロセスはこの '共有'オブジェクトを変更する唯一のプロセスなので、スレーブプロセスに「共有」オブジェクトに対する変更を警告する必要があります。これを行うには、マスタープロセスがスレーブプロセスで共有オブジェクトへの変更を伝えるために使用するメッセージングシステムをセットアップできます。ここでの欠点は、スレーブプロセスがマスターオブジェクトと同期していないときに共有オブジェクトを参照する可能性があることですが、これはレプリケーションの一般的な問題です。また、RPCオーバーレイを使用して、マスター/スレーブアプリケーションの開発/保守を容易にすることができます。
私はこのデザインの非常に高いレベルの例を以下に提供しようとします。実際のコードと擬似コードを並べて利用することを私に許してください。私はここでは、両方のマスター/スレーブのコードで定義されます私たちの共有オブジェクト
struct sharedobj {
int var1;
};
だ完全にコードこれたくありませんでしたが、また、それだけでコメント:)
で作られたくありませんでしたここで、共有オブジェクトと伝播を更新マスター・プロセスの一例は、ここで
int counter = 0;
sharedobj mysharedobj;
while(true){
//update the local version first
mysharedobj.var1 = counter++;
//then call some function to push these changes to the slaves
updateSharedObj(mysharedobj);
}
を変更だスレーブにマスタの変更を伝播する機能です。
updatedSharedObj(sharedobj obj){
//set up some sort of message that encompasses these changes
string msg = "var1:" + the string value of obj.var1;
//go through the set of slave processes
//if we've just done basic messaging, maybe we have a socket open for each process
while(socketit != socketlist.end()){
//send message to slave
send(*socketit, msg.c_str(),msg.length(),0);
}
}
これらの変更を受け取り、その '共有'オブジェクトを更新するスレーブコードは次のとおりです。他のスレッドで実行されている可能性が最も高いため、スレーブはオブジェクトの更新を停止して確認する必要なく実行できます。
while(true){
//wait on the socket for updates
read(mysock,msgbuf,msgbufsize,0);
//parse the msgbuf
int newv1 = the int value of var1 from the msg;
//if we're in another thread we need to synchronize access to the object between
//update thread and slave
pthread_mutex_lock(&objlock);
//update the value of var1
sharedobj.var1 = newv1;
//and release the lock
pthread_mutex_unlock(&objlock);
}
私はそれを使用していないが、あなたは[Boost.Interprocess](HTTPを意識することは言及しません://www.boost。 org/doc/libs/1_53_0/doc/html/interprocess.html)。 – BoBTFish
あなたは '共有メモリ'を検索したいかもしれません。 [ここではSO上の質問](http://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c) 'C'(魔女はC++でも動くかもしれない) – A4L