Use separate threads or programs?

Asked

Viewed 156 times

2

I’m developing an application that involves three (maybe more) processes that run simultaneously: an HTTP server, a logger and another that will run the main code.

A priori, a simple solution to do this is by using three threads, one for each program task. This is a very simple solution that allows me to exchange information between threads in a very quiet way, if necessary.

However, these three tasks can be performed quite independently and could be done in three different programs, rather than three threads in a single program.

My question then is: what is the difference between these two approaches? Which would be the most recommendable, considering that the third is the main thread?

Considerations: the application will run on a very pure Linux system (only with the essential installed) and the hardware is a Beaglebone Black.

  • 2

    It depends a little on the language used but mine default usually be separate. Use threads only when it does not pay to pass data to another process, where the processing needs a very fast response and the passage between processes will cost a lot. It can help: http://answall.com/questions/1946/%C3%89-always-guaranteed-that-a-applies%C3%A7%C3%A3o-com-m%C3%Baltiplas-threads-spin-plus-r%C3%A1pido-que

  • @bigown I should use C++11 threads if I’m going to do it with threads. I’ll check the link. Thank you.

  • Managed to solve your problem?

  • @Eduardoseixas Basically it’s a question of projects. There’s not much difference between approaches. The main different is communication, using threads you share the same context and memory in OS, separate programs not.

  • Got it. So create an answer and mark it as accepted, so your question is not orphaned and can help other people.

2 answers

1


It is always advisable to use separate processes. (Nowadays, with the trend of microservices, the "fashion" is to use even separate virtual machines, but this is beyond the scope of the question.)

I see no advantage in using threads in your use case, because the exchange of information between threads involves synchronization issues is always a problem in itself. It seems easy but never is. The cost of doing this safely is the same as passing messages between separate processes (the use of shared memory, which in fact is faster, could be justified in scientific applications, which is not the case).

Some advantages of separate processes:

a) if a process dies, it can be restarted without affecting others, already the death of a thread kills the whole process, including the other threads.

b) separation of tasks in separate processes increases safety.

c) Each process is easier to reuse in another application.

d) Updating a software (e.g. the HTTP server) can be done without stopping the others.

-1

It depends a lot on how the interactions between programs or threads will be. This is basically what was said in the comments: "The main different is communication, using threads you share the same context and memory in the OS, separate programs do not." This implies that if you do it in separate programs, to exchange information and data through a file, for example, or even through sockets, but in a local way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.