我正在使用下面的代碼進行線程安全訪問變量。線程安全get(訪問器方法)
int gnVariable;
void getVariableValue(int *pnValue)
{
acquireLock(); //Acquires the protection mechanism
*pnValue = gnVariable;
releaseLock(); //Releasing the protection mechanism
}
我想我API的簽名更改爲更方便用戶
int getVariableValue(void);
我應該如何重寫功能 - 使得API的用戶不必理會鎖定/解鎖細節?
我不明白你的問題。您建議的API都不提供鎖定細節。 – zvrba 2010-05-17 06:24:21
如果更改爲第二個版本 - int getVariable(void) - ,API的用戶將不得不手動釋放/解鎖信號量。如果你不這樣做Samuel和Naveen建議 – sonofdelphi 2010-05-17 07:51:01