複数のスレッドがグローバルキューにアクセスするマルチスレッドプログラム(クライアントサーバープログラムですが、この質問には必ずしも関係ありません)があります。2つのキューがあります:msgs_inc
そしてclients_msg
、タイプはqueue<msgInfo>
、msgInfo
私のクラスです。
最初のスレッドは、クライアントからメッセージを受信し、次のことを行います(関連するスニペット)。
msgInfo message_processed(message_fr_client); //line 1: takes in string
msgs_inc.push(message_processed); //line 2
2番目のスレッドは、から取得してmsgs_inc
処理し、にプッシュすることになっていますclients_msg
。
msgInfo msg_from_queue(msgs_inc); //line 3: retrieve from front of queue
msgs_inc.pop(); //line 4
clients_msg.push(msg_from_queue); //line 5
3番目のスレッドはから取得しますclients_msg
。その後は不要になります。
msgInfo msg_from_queue(clients_msg); //line 6: retrieve from front of queue
clients_msg.pop(); //line 7
私の質問は:
- 3行目で、このコンストラクター(以下で詳しく説明します)は、コピーコンストラクターと標準コンストラクターとして知られていますか?
msgInfo
「インスタンス化」を2回、つまりプッシュする前に1回、取得する前にもう一度行うのは間違っていますか?代わりにポインタなどを使用する必要がありますか?非効率に感じるかもしれませんが、他の方法はわかりません。- デストラクタを適用するのはいつですか?必要がなくなった7行目以降にのみ適用しますか、それとも
msgInfo
情報を使用して別のインスタンスをすでに作成しているため、4行目でデストラクタを再度適用する必要がありますか?
この退屈さについての私の謝罪-私はこれについての情報を見つけることができず、具体的な結論をまとめることができません。
これは私のクラスです:
class msgInfo
{
public:
msgInfo();
msgInfo(std::string); //creating instance fr string rxed fr client
msgInfo(std::map<int, std::map<int, std::queue<msgInfo>>>, int, int); //creating instance for string to be sent to client
~msgInfo();
private:
int source_id;
int dest_id;
int priority;
std::string payload;
std::list<int> nodePath;
};
3行目と6行目で使用されているコンストラクター:
msgInfo::msgInfo(std::queue<msgInfo> outgoing_msg)
{
source_id = outgoing_msg.front().source_id;
dest_id = outgoing_msg.front().dest_id;
priority = outgoing_msg.front().priority;
payload = outgoing_msg.front().payload;
nodePath = outgoing_msg.front().nodePath;
}