最近在文件保存、將一文件內容寫入另一文件中經常會遇到線程沖突的問題,如,將文件A的內容寫入文件B,再將A刪除,這樣的操作將會涉及以下幾個步驟:
(1) 打開B文件,通過文件流初始化DataWriter對象
(2) 打開A文件,通過文件流初始化DataReader對象
(3) 通過DataReader對象加載文件A的內容
(4) 通過DataWriter將內容寫入
(5) DataWriter保存
(6) DataWriter刷新
以上每一步都必須一環套一環,若沒有確保當前對A、B文件的操作完全結束,而在其它線程又開始操作這些文件,就會經常出現線程沖突。
下面提供對以上步驟一個簡單示例:
task<IRandomAccessStream^>(StorageFileB->OpenAsync(FileAccessMode::ReadWrite))
.then([ this](IRandomAccessStream^ raStream)
{
IOutputStream^ outStream = raStream->GetOutputStreamAt( 0);
DataWriter^ bbrw = ref new DataWriter(outStream);
task<IRandomAccessStream^>(StorageFileA->OpenAsync(FileAccessMode::Read))
.then([bbrw, this](IRandomAccessStream^ readStream)
{
DataReader^ dataReader = ref new DataReader(readStream);
task<unsigned int>(dataReader->LoadAsync((unsigned int)readStream->Size)).then([ this,bbrw,dataReader](unsigned int numBytesLoaded)
{
Array<unsigned char, 1>^ fileContent = ref new Array<unsigned char, 1>(numBytesLoaded);
dataReader->ReadBytes(fileContent);
bbrw->WriteBytes(fileContent);
task<unsigned int> store(bbrw->StoreAsync());
store.then([bbrw] (unsigned int)
{
return bbrw->FlushAsync();
}).then([=]( bool flushSucceeded)
{
bbrw->DetachStream();
.then([ this](IRandomAccessStream^ raStream)
{
IOutputStream^ outStream = raStream->GetOutputStreamAt( 0);
DataWriter^ bbrw = ref new DataWriter(outStream);
task<IRandomAccessStream^>(StorageFileA->OpenAsync(FileAccessMode::Read))
.then([bbrw, this](IRandomAccessStream^ readStream)
{
DataReader^ dataReader = ref new DataReader(readStream);
task<unsigned int>(dataReader->LoadAsync((unsigned int)readStream->Size)).then([ this,bbrw,dataReader](unsigned int numBytesLoaded)
{
Array<unsigned char, 1>^ fileContent = ref new Array<unsigned char, 1>(numBytesLoaded);
dataReader->ReadBytes(fileContent);
bbrw->WriteBytes(fileContent);
task<unsigned int> store(bbrw->StoreAsync());
store.then([bbrw] (unsigned int)
{
return bbrw->FlushAsync();
}).then([=]( bool flushSucceeded)
{
bbrw->DetachStream();
Do anything….
});
});
});
});
});
});
});
});
最后一個bbrw->DetachStream()非常重要哦,它是用來分離DataWriter關聯流的,如果我們不將之分離,再一次操作文件流時也會容易發生沖突。
如果在項目中會多次同一個文件,一定要確保不要同時操作同一個文件,否則很容易發生線程沖突。若是在不同的模塊要操作同一個文件,可以通過信號量來處理。