How to make C (P/invoke) code called from C# “Thread-safe”

后端 未结 4 707
长情又很酷
长情又很酷 2021-02-08 06:43

I have some simple C-code which uses a single global-variable. Obviously this is not thread-safe, so when I call it from multiple threads in C# using P/invoke, things screw up.

4条回答
  •  日久生厌
    2021-02-08 06:58

    You can either make sure what you only call _someFunction once at a time in your C# code or change the C code to wrap the access to the global variable in a synchronization primitive like a critical section.

    I would recommend changing the C# code rather than the C code, as the C# code is multi-threaded, not the C code.

提交回复
热议问题