This is how I see threads. Please do correct me if I'm wrong:

A thread is a part of a process that runs a sequence of program code. There can be many threads within a process that run different parts of a program (one thread manages keyboard input, one manages auto-saving, one manages printing etc.). The process allocates resources while the thread(s) are responsible for getting tasks executed by being scheduled for CPU time.

My question is:

Are threads always bound to their own process or can they exchange resources with other threads outside of their process?

  • 1
    $\begingroup$ A thread can write the resources to a file, which every process can access... If you consider this an "exchange". $\endgroup$
    – kennytm
    May 13 '16 at 17:40
  • $\begingroup$ Inter-process Communication is what you're looking for. $\endgroup$
    – gardenhead
    Oct 14 '16 at 16:41

Threads are used to handle tasks simultaneously. For example, you keep your computation running in a thread, while another thread is waiting for keyboard input.

Threads inherit the privileges of the process they run in.
If the parent process is authorized to access another process's resources than the thread can do this.
The operating system assigns the process privileges when starting that process.

Whether your threads can obtain resources or not depends on the implementation of threads in your operating systems. In Linux, with some languages such as C, your threads are able to share memory with other processes, and even threads can create other threads. Basically, threads' access to memory is the same as your process. So, threads can create a shared memory with another process and keep communicating with other processes via shared memory space.

  • 2
    $\begingroup$ The operating system probably has something to say about threads trying to access the memory of other processes! $\endgroup$ May 15 '16 at 2:45
  • $\begingroup$ I don't see how this is language dependent. I can call API functions like CreateRemoteThread using managed runtimes like C# just as easy as from C or VB for that matter. $\endgroup$
    – Johan
    May 16 '16 at 15:24
  • $\begingroup$ Processes can communicate with other processes, in a more or less restricted way. Threads are completely irrelevant to this. Of course there is always at least one thread running. $\endgroup$
    – gnasher729
    Jun 16 '16 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.