Multithreading-you know the line stacks?

Source: Internet
Author: User
problem

1. Stack and stack process for local variables
void Func1 () {
int a = 0;
int b = 0;
}
There is a stack-top pointer in the system, and each time the local variable is allocated and reclaimed, it is actually moving the stack pointer.

2. Allocation risk of static local variables
void Func2 () {
static int a = 0;
}
This variable a may be allocated multiple times, because if Func2 may be called by multiple threads at the same time, it is possible for the function to switch threads when allocating memory.

Problem:
Such as
void Func3 () {
int A;
int b;
}

void Func4 () {
int C;
int D;
}
Suppose that func3 and Func4 are called by two threads respectively, and func3 is executed before FUNC4, and the order of 4 variables is a, B, C, D, respectively. According to the 1th note above, this time the top pointer of the stack points to D.
If, at this time, the FUNC3 executes first, then this time, the system will recycle B and a, but B is not at the top of the stack, so the top pointer cannot be moved, so B and a cannot be recycled. The most complex situation may be as follows, the order of the stack is a, C, D, B, this time B can be recovered normally. When you want to recycle A, D will not be mistaken for a to be recycled. How should we explain the problem?
Obviously, it is not actually mentioned above, because there is a very important attribute in the thread stacksize, it makes us vaguely feel that the thread is a private stack space, if so, the ABCD stack out of the stack is not a problem, because they are not saved together.


Pthread Line Stacks

#include <stdio.h>
#include <pthread.h>

void* thread1 (void* a)
{
	char m[8388608];
	printf ("thread1\n");
}

int main () {
	pthread_t pthread_id;
	pthread_attr_t thread_attr;
	int status;

	Status = Pthread_attr_init (&thread_attr);
	if (Status! = 0)
		printf ("Init error\n");

	size_t stacksize = +;
	Status = Pthread_attr_getstacksize (&thread_attr, &stacksize);
	printf ("stacksize (%d) \ n", stacksize);
	printf ("size (%d) \ n", sizeof (int));

	Status = Pthread_create (&pthread_id, NULL, THREAD1, NULL);
	while (1)
	{}
	return 0;
}

Operation Result:

StackSize (8388608)
Segment Error

Analysis

Pthread_attr_getstacksize can get the size of the private stack of the thread, I this machine is 8388608 bytes, 8M, that is, the private stack is 8M maximum, so, a thread function created with a local array length of 8M, Display segment Error (although the array size and the private stack as large, but the private stack in addition to allocating local variables, but also to save some management information, so it must be less than 8M), if the array length is reduced by a certain value, you can see the THREAD1 function printing information.


pthread thread Memory layout



As we can see from the diagram, the stack between the two threads is independent and the others are shared, so there is a possibility that synchronization is required when operating the shared area, and the stack does not need to be synchronized.

Finally we know that Pthread also provides a private heap mechanism, which is explained later in relation to the private heap mechanism.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.