Use separate threads or programs?

2

I'm developing an application that involves three (perhaps more) processes that run concurrently: one HTTP server, one logger and another that will run the primary code.

A priori, a simple solution to do this is to use three threads, one for each task in the program. 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 lies in this question: what is the difference between these two approaches? Which would be the most recommendable, considering that the third one 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 .

    
asked by anonymous 13.11.2014 / 12:49

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 goes beyond the scope of the question.)

I do not see advantages in using threads in your use case, because exchanging information between threads involves synchronization issues is always a problem in itself. It sounds easy but it never is. The cost of doing this safely is the same as passing messages between separate processes (the use of shared memory, which is actually 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, since the death of a thread kills the whole process, including other threads.

b) Separating tasks into separate processes increases security.

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

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

    
29.05.2016 / 21:06
-1

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

    
13.03.2016 / 04:18