locked
Is Windows::Storage::FileIO::AppendTextAsync thread safe?

    Question

  • Hello every one, I want to write some logs into a file from different thread. But I don't want to use mutex or other locks when appending contents into the file. I've tried that the logs could be recorded successfully without exceptions from different threads. The writing frequency is high from each thread so I suppose that it should be possible that different threads might want to append logs to the file at the same time. Though it works fine at present, I am not quite sure whether this function is thread safe. Could anybody give some hints/advice? Thank you very much.

    PS: if there would be any efficient logger library or code segment on Metro that I could study, please tell me. Thanks!


    • Edited by B0L Tuesday, March 05, 2013 3:59 AM
    Tuesday, March 05, 2013 3:59 AM

Answers

  • You should be fine.

    See Threading and Marshaling (C++/CX) for comments on ThreadingModel and MarshallingBehavior, then check out how the attributes for the FileIo class:

    MarshalingBehaviorAttribute(Agile) 
    StaticAttribute(Windows.Storage.IFileIOStatics, NTDDI_WIN8) 
    ThreadingAttribute(Both) 
    VersionAttribute(NTDDI_WIN8) 

    Since it has ThreadingModel=Both and MarshallingType=Agile thread safety is taken care of for you.

    --Rob

    • Marked as answer by B0L Tuesday, March 05, 2013 6:25 AM
    Tuesday, March 05, 2013 4:36 AM
    Owner

All replies

  • You should be fine.

    See Threading and Marshaling (C++/CX) for comments on ThreadingModel and MarshallingBehavior, then check out how the attributes for the FileIo class:

    MarshalingBehaviorAttribute(Agile) 
    StaticAttribute(Windows.Storage.IFileIOStatics, NTDDI_WIN8) 
    ThreadingAttribute(Both) 
    VersionAttribute(NTDDI_WIN8) 

    Since it has ThreadingModel=Both and MarshallingType=Agile thread safety is taken care of for you.

    --Rob

    • Marked as answer by B0L Tuesday, March 05, 2013 6:25 AM
    Tuesday, March 05, 2013 4:36 AM
    Owner
  • Hello, Rob, get it! Thank you very much for your reply!
    Tuesday, March 05, 2013 6:26 AM
  • Hi, Rob,

    Would you please have a look at the following thread:

    http://social.msdn.microsoft.com/Forums/windowsapps/en-US/f32a58fe-2f02-4696-8318-1bf0a223d1fd/problem-with-windowsstoragefileioappendtextasync

    Thank you very much!

    Friday, July 05, 2013 3:50 AM