Threads: Question on Sharing Memory & Performance

  • Thread starter FrostScYthe
  • Start date
  • Tags
    Threads
In summary, threads are similar to processes in that they share state information and memory. However, they have their own individual stacks and registers are not preserved during context switches. The extent to which they can be distributed across multiple processors depends on the implementation, with user space threads generally not able to do so and kernel-implemented threads typically being able to do so.
  • #1
Hiya :),
1). I understand that threads are like processes except that they share the state information, and memory.
So I would think they share the values of the registers, the memory heap, and the stack, right? would they also share global variables?

2). Also, could a multithread solution that uses multiple user threads get better performance on a multiprocessor system than on a system with only 1 processor? I think it could get better performance if indeed we assign a thread to each processor, but I’m not sure whether the user level thread can allocate this processors or not?

Thanks in advance for any help

Frost.
 
Technology news on Phys.org
  • #2
There are many different thread implementations, and they can actually differ in many respects. The most ubiquitous threading system is probably pthreads (POSIX threads), which is described here:

http://www.die.net/doc/linux/man/man7/pthreads.7.html

Note that pthreads share only the heap; they each have their own stack. Registers are not preserved across thread context switches, either.

The answer to your last question depends on the implementation. If the threads are implemented entirely in user space, they cannot be distributed across multiple processors. If the threads are implemented by the operating system kernel, on the other hand, they generally can be distributed across multiple processors.

- Warren
 
  • #3


Hi Frost,

1) You are correct in understanding that threads share state information and memory. This means that they share the values of registers, the memory heap, and the stack. However, they do not necessarily share global variables unless they are explicitly declared as shared or accessed through a shared resource.

2) In general, a multithread solution can potentially achieve better performance on a multiprocessor system compared to a single processor system. This is because the threads can be distributed across multiple processors, allowing for parallel execution and potentially reducing overall execution time. However, this also depends on the specific implementation and workload of the threads. User level threads can allocate processors, but this also depends on the operating system and its scheduling algorithms.

I hope this helps clarify your questions. Best of luck in your studies!

 

1. What are threads and how do they impact performance?

Threads are independent units of execution within a single process. They allow for multiple tasks to be performed simultaneously, increasing performance by utilizing available CPU resources more efficiently.

2. How does sharing memory between threads affect performance?

Sharing memory between threads can improve performance by reducing the amount of time and resources needed to transfer data between threads. However, it can also introduce synchronization and race condition issues that can negatively impact performance if not properly managed.

3. What is the difference between shared and local memory in threading?

Shared memory is accessible by multiple threads and can be used to exchange data between them. Local memory, on the other hand, is only accessible by a specific thread and is used for storing thread-specific data.

4. How can thread priorities affect performance?

Thread priorities determine the order in which threads are executed by the CPU. Higher priority threads are given more CPU time, which can improve performance for critical tasks. However, setting priorities too high can result in lower performance for other threads.

5. How can I improve the performance of my threaded application?

There are several ways to improve the performance of a threaded application, including optimizing thread creation and synchronization, using thread pools, and avoiding resource contention. It is also important to properly manage shared memory and thread priorities to maximize performance.

Suggested for: Threads: Question on Sharing Memory & Performance

Back
Top