Home > Software design >  Is there any way to isolate two thread memory space?
Is there any way to isolate two thread memory space?

Time:04-19

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)

CodePudding user response:

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...

CodePudding user response:

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)

  •  Tags:  
  • c
  • Related