Transferred from: http://blog.csdn.net/shuilan0066/article/details/7683315
Thread-shared environments include the process code snippet, the public data of the process (which leverages the shared data, the threads are easily communicating with each other), the file descriptor that the process opens, the processor of the signal, the current directory of the process, and the process user ID and process group ID.
The process has its own personality as well as its many commonalities. 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. The process uses this to markknowledge of threads. 2. Value of the Register group
Since threads are concurrently running, each thread has its own different running threads, when from a lineswitch to another thread, the state of the Register collection of the original thread must be saved toin the future, the thread can be restored when it is re-switched to. 3. The stack of threads
The stack is necessary to ensure that the thread runs independently.
A thread function can call a function, and the called function can be nested in layers, so the threadYou must have your own function stack so that function calls can execute normally, not by other threads To the sound.
4. Error return code
Because many threads in the same process are running concurrently, a thread may make system callsThe errno value is set after the thread has not handled the error, and another thread is at this point is put into operation by the scheduler so that the error value can be modified.
Therefore, different threads should have their own error return code variable.
5. Thread's Signal Shield code
Because each thread is interested in a different signal, the thread's signal mask should beManagement. But all the threads share the same signal processor.
6. Priority of Threads
Because the thread needs to be dispatched as if it were a process, there must be a parameter that can be used for scheduling
Number, this parameter is the priority of the thread .
When it comes to multi-threaded routines, there are often things that are difficult to Incredibles, and allocating a variable with heaps and stacks can produce unexpected results in subsequent executions, and the result is that the memory is being accessed illegally, causing the contents of the memory to be changed.
The two basic concepts for understanding this phenomenon are that the threads in a process share the heap, while threads in the process maintain their own stacks.
Another mechanism is to declare a member variable such as Char name[200], with the end of this code call, the address of name on the stack is freed, and if it is char * Name = new char[200]; The situation is completely different, unless a call to delete is displayed and the address pointed to by name is not freed.
In B, if you allocate the declaration V and the heap area using the mechanism of the temporary variable in the stack area, the result is different. If you use the stack area, if the variable address is am1-am2 so large, exit B calls when the address is released, the C function may overwrite the memory, so that when D executes, the content read from the memory am1-am2 is changed.
And if you allocate with new (heap), that is not the case, because there is no display of the pair with delete and the heap is shared with the thread, that is, 2 threads can see what the 1 threads are allocating in the heap, so there will be no false write.
This is the problem I found in the company internship, because at that time just involved in multithreaded programming, the operating system so simple topic plagued the author for a long time, I hope that the first C + + multi-threaded readers can help! 2) If two threads share the heap and all of them are likely to perform memory allocation and release operations, they must be protected synchronously, which is not related to Class C, class R, T. You see an example of two threads that should be using the respective heap.
On platforms such as Windows, different threads use the same heap by default, so when you allocate memory with malloc (or Windows GlobalAlloc) in C, you use synchronous protection. Without synchronous protection, a race condition occurs when two threads perform simultaneous memory operations, which can lead to confusion in memory management within the heap. For example, two threads are allocated a unified block memory address, an idle list pointer error, and so on.
Symbian threads generally use a separate heap space. This allows each thread to be allocated and freed directly in its own heap, reducing the overhead that synchronization introduces. When the thread exits, the system reclaims the thread's heap space directly, and the memory space within the thread does not cause a memory leak within the process.
However, when two threads use a common heap, they must be protected synchronously with the critical section or mutex. Otherwise, the program crashes sooner or later. If your thread needs to allocate and release any number and type of objects on the shared heap without rules, you can customize a allcator to use synchronous protection within allocator. The thread uses this allocator to allocate memory directly. This is equivalent to implementing your own malloc,free. However, it is recommended that you review your system again, because most of this is unnecessary. With good design, the local heap of threads should be able to meet the needs of most objects. If you have a class of objects that need to be created and shared on a shared heap, this is a reasonable requirement, and you can implement shared protection on new and delete on this class.
Long time no write things, recently experienced a lot of things, come on, the future is good
What is reproduced in the resources shared by threads in the same process