How to make any C ++ library I make thread safe?

First of all, I am pretty good at C ++ and understand the basics of streaming and thread synchronization. I also want to write my own memory allocator as my favorite project and read that they should be thread safe.

I understand what the term "thread safe" means, but I have no idea how to make C ++ code safe for threads.

Are there any practical examples or tutorials on how to make code safe?

In a memory allocation scenario, does this essentially mean that all mutating functions are marked as critical sections? Or is there something else?

+5
source share
1 answer

Same as all problems with threads: make sure that when one thread changes something, no other thread accesses it. For a memory allocation system, I would suggest that you need a way to make sure that you are not allocating the same memory block to 2 threads at the same time. Regardless of whether the entire search completes or allows multiple searches, but blocks when the distribution table needs to be updated (which may result in the search result becoming invalid, requiring a different search), it is up to you.

+2
source

All Articles