0

クラスをフロントアプリケーションに公開するC++プロトコルスタックdll(シリアル通信用)があります。公開されたクラスは次のようになります。

class ProtocolStack
{
public :
    void OpenRequest(Params);
    void SendData(Params);
    void CloseRequest(Params);
};

ProtocolStack クラスは、以下に示すように、PhysicalLayer のようなさまざまなレイヤーを作成します。

class PhysicalLayer
{
private:
    int Baud_rate;
    string PortName;
public:
    void Send(string);
    void Receive();
 };

異なるシステムからデータを並行して読み取るために、同じプロトコル スタックを使用したいと考えています。(異なるポートから) 並列読み取り用にプロトコル スタックの単一のオブジェクトを作成した場合、同じ関数が異なるスレッドから同時に呼び出されると、各関数呼び出しに割り当てられるデータ セグメントは異なりますか?

物理層のプライベート変数が破損します。右?

各レイヤーで各チャネル (並列接続) のすべてのデータを維持する必要がありますか?

データを並列処理したい。スタックの別のオブジェクトを作成するか、各レイヤーの接続に関連するすべてのデータを維持する以外に他の方法はありますか?

4

1 に答える 1

4

The fact that the code is in a DLL is not relevant. Code is code. Regarding data sharing, data is only shared between threads if the code chooses to do so. If the code uses stack based or allocates its own heap based memory, then data is not shared. If the code stores its data at global scope, then data is shared between threads. That's the just the same if the code is written by you, or hosted in a third party library in a DLL.

The question boils down to the threading rules of the code. The code may not support threaded usage at all. The code may support threaded usage, so long as you follow certain rules. They only way to be sure is to consult the documentation of the code, hoping of course that the others actually documented their threading model.


In a comment to the question you say:

My question is whether separate data attributes will be stored for same object when functions from the dll are called simultaneously.

No they will not. If you have two threads referring to the same object, then modifications of that object's data made by one thread will be visible by the other thread.

于 2013-11-06T07:39:51.423 に答える