What resources do threads in the same process share?

The environment shared by threads includes: process code segment, process public data (using these shared data, threads can easily communicate with each other), process open file descriptors, signal handlers, process current directory and Process user ID and process group ID.

 
   While processes have many of these commonalities, they also have their own personalities. With these personalities, threads can achieve concurrency. These personalities include:

   1. Thread ID
      Each thread has its own thread ID, which is unique in this process. Process uses this to mark
   Know the thread.
 
   2. Since the value of the register group
      runs concurrently between threads, each thread has its own different running clues.
   When the process switches to another thread,   the state of the register set of the original thread must be saved, so that
   The thread can be resumed in the future when it is switched back to.
 
   3. The stack
      stack of the thread is necessary to ensure that the thread runs independently.
      Thread functions can call functions, and the called functions can be nested in layers, so the thread
   Must have its own function stack,   so that function calls can be executed normally without being affected by other threads
   ring.

   4. Error return code
      Because there are many threads running at the same time in the same process, a thread may make a system call
   After the errno value is set, and the   thread has not yet handled the error, another thread is at this time
   The scheduler is put into operation, so that the error value may be modified.
      So, different threads should have their own error return code variables.

   5. Thread's signal mask code
      Since each thread is interested in different signals, the thread's signal mask code should be determined by the thread itself.
   己管理。但所有的线程都  共享同样的信号处理器。

   6.线程的优先级
      由于线程需要像进程那样能够被调度,那么就必须要有可供调度使用的参

   数,这个参数就是线程的 优先级。

7.线程存储

在多线程程序中,所有线程共享程序中的变量。现在有一全局变量,所有线程都可以使用它,改变它的值。而如果每个线程希望能单独拥有它,那么就需要使用线程存储了。表面上看起来这是一个全局变量,所有线程都可以使用它,而它的值在每一个线程中又是单独存储的。这就是线程存储的意义。

参考API:

pthread_key_create

pthread_setspecific

pthread_getspecific


转自:http://blog.chinaunix.net/u1/43706/showart_1161524.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325951520&siteId=291194637