独自のスレッドで5秒に1回実行される1つのアクティビティを持つクラスが必要です。これはWebサービスのため、エンドポイントを指定する必要があります。オブジェクト実行時に、メインスレッドはエンドポイントを変更できます。これは私のクラスです:プロセス間オブジェクトの受け渡し
class Worker
{
public:
void setEndpoint(const std::string& endpoint);
private:
void activity (void);
mutex endpoint_mutex;
volatile std::auto_ptr<std::string> newEndpoint;
WebServiceClient client;
}
newEndpointオブジェクトはvolatile宣言する必要がありますか?私は確かに読んでいくつかのループ(コンパイラがそれを最適化しないようにするため)だったが、ここで私は知らない。
各実行では、新しいエンドポイント(新しいエンドポイントがある場合はそれをクライアントに渡して再接続手順を実行する)をチェックし、その作業を行います。
void Worker::activity(void)
{
endpoint_mutex.lock(); //don't consider exceptions
std::auto_ptr<std::string>& ep = const_cast<std::auto_ptr<string> >(newEndpoint);
if (NULL != ep.get())
{
client.setEndpoint(*ep);
ep.reset(NULL);
endpoint_mutex.unlock();
client.doReconnectionStuff();
client.doReconnectionStuff2();
}
else
{
endpoint_mutex.unlock();
}
client.doSomeStuff();
client.doAnotherStuff();
.....
}
IはnewEndpointオブジェクトがもはや変更できないので、私はCONSTメソッドを呼び出すことができるようにする揮発性クラス仕様を削除することを意味するミューテックスをロックします。 (別のスレッドから呼び出される)
setEndpoint方法:
void Worker::setEndpoint(const std::string& endpoint)
{
endpoint_mutex.lock(); //again - don't consider exceptions
std::auto_ptr<std::string>& ep = const_cast<std::auto_ptr<string> >(newEndpoint);
ep.reset(new std::string(endpoint);
endpoint_mutex.unlock();
}
はこの事スレッドは安全ですか?そうでない場合は、何が問題なのですか?揮発性のnewEndpointオブジェクトが必要ですか?