I have an online service which is single thread, since whole program share one memory pool(use new or malloc),a module might destroy memory which leads to another module work incorrectly, so I want to split whole program into two part, each part runs on a thread, is it possible to isolate thread memory like multiprocess so I can check where is the problem? (splitting it into multiprocess cost a lot of time and risky so I don't want to try)
-
1There is the option to have thread local memory, yes. – πάντα ῥεῖ Apr 18 '22 at 12:29
-
4Based on your description, it seems that using multiple processes is the only option to achieve what you want. On Linux and most other POSIX operating systems, you can use [`fork`](https://man7.org/linux/man-pages/man2/fork.2.html). I do not believe that using thread-local storage will be sufficient, because it is still possible for one misbehaving thread to interfere with the other misbehaving thread. – Andreas Wenzel Apr 18 '22 at 13:26
-
Careless programming will harm even single-threaded code. All multithreaded programs use the same memory. The real solution is to *avoid* careless coding, avoid pointer arithmetic, use containers and std::strings instead of pointers etd. Avoid raw threads and prefer constructs that don't require access to shared memory. Tasks, futures, workers all make multithreading a lot easier and safer – Panagiotis Kanavos Apr 18 '22 at 15:23
-
If it is for debugging only and you are willing to accept less performance during debugging, there are ways to easier find memory corruption like special memory heaps and memory checking functions, which can be run very often. Or debuggers, which can rewind the execution. – Sebastian Apr 18 '22 at 17:14
-
1@AndreasWenzel I confirm. TLS / TSD is just a way to make SIMD code without passing a structure of "private" data to each thread... And it works exactly like passing such a struct. Threads are still not isolated at all, by principle and design. Such an isolation exist only at process level, as you mentionned it. – Wisblade Apr 18 '22 at 20:38
2 Answers
As long as you'll use threads, memory can be easily corrupted since, BY DESIGN, threads are sharing the same memory. Splitting your program across two threads won't help in ANY manner for security - it can greatly help with CPU load, latency, performances, etc. but in no way as an anti memory corruption mechanism.
So either you'll need to ensure a proper development and that your code won't plow memory where it must not, or you use multiple process - those are isolated from each other by operating system.
You can try to sanitize your code by using tools designed for this purpose, but it depends on your platform - you didn't gave it in your question. It can go from a simple Debug compilation with MSVC under Windows, up to a Valgrind analysis under Linux, and so on - list can be huge, so please give additional informations.
Also, if it's your threading code that contains the error, maybe rethink what you're doing: switching it to multiprocess may be the cheapest solution in the end - don't be fooled by sunk cost, especially since threading can NOT protect part 1 against part 2 and reciprocally...

- 1,483
- 4
- 13
Isolation like this is quite often done by using separate processes.
The downsides of doing that are
- harder communication between the processes (but thats kind of the point)
- the overhead of starting processes is typically a lot larger than starting thread. So you would not typically spawn a new process for each request for example.
Common model is a lead process that starts a child process to do the request serving. The lead process just monitors the health of the worker
Or you could fix your code so that it doesnt get corrupted (AN easy thing to say I know)

- 48,078
- 23
- 82
- 145