4 ms·
Generally, threads are difficult to use because of what is known as the shared data problem. With threads, generally some global data structure is shared among
by CitizenKane 17y ago
Generally, threads are difficult to use because of what is known as the shared data problem. With threads, generally some global data structure is shared among all the threads. These threads write to and read from this data structure, and those operations are interleaved.
This means that if not careful, when a thread is reading data, that data could be corrupted by a another thread writing data into the same data structure. This in turn means that threads need to lock the data structure when they are accessing it so that other threads can't get to it.
This leads to a whole host of potential problems that can be very difficult to debug because many of them end up depending on subtle timing differences.
Processes avoid this because they can't have shared data. So instead the problem is broken up between processes and message passing is used to share data in between those processes. It's something that generally makes life easier to understand and steers clear of problems that can be very difficult to reproduce and debug.
- blasdel 17y agoProcesses don't share mutable data, not without explicitly using IPC. In nearly all Unixes, both processes share their entire address spaces after the fork(), just immutably -- pages are only copied when they're written to by one of the processes, otherwise they are shared till exit. Copy-on-write is what makes fork() tractable performance-wise.
- easp 17y agoCopy-on-write is also why people overestimate Apache memory consumption so dramatically and then get into the hassle of running php apps under fcgi to save a trivial amount of memory.