implement two level lock
I have writeen a dll to store some info in memroy, there are many threads want to visit the data at the same time, so I use a read/write lock to sync. And I export some function to the user, in the function, I require the lock, then get data, then exit and release the lock. For example, like this function.
开发者_运维技巧void GetData(data)
{
//require lock
//get data
//release lock
}
my users may call GetData many times at there function, for example,
void ProcessData()
{
// do something 1
GetData(data1);
// do something 2
GetData(data2);
...
}
at the same time, other thread may change the data in my dll by call other export function of my dll,so data may change between GetData(data); and GetData(data2); but my user want my data never change at ProcessData because of data1 may releated to data2 in this situation. And I don't want to expose my lock in my dll. Is there a way to implement something like this, Thx!
Do you want people to be able to write data while ProcessData is running? Or should people who write be blocked until ProcessData is done?
If yes, then you can export 2 functions that will let ProcessData lock out any writers.
LockOutWriters() { // lock }
WriteData() { // blocks if above is locked }
UnLockWriters() { // unlock }
If no, then you'll need to take a snapshot of the data at the time GetData() is called and make sure that whenever ProcessData calls GetData, it pulls data from that snapshot.
精彩评论