Sharing linux multithreading Parameters

Source: Internet
Author: User
Whether linux multithreading parameters are shared-Linux general technology-Linux technology and application information. The following is a detailed description. Is multiple-threaded linux parameters shared?
This is my multi-threaded program. I know that in multithreading, the global variables of the program are shared, but I cannot determine the local variables, that is, the variables in the thread function change, does it affect other local variables of the thread? Here is the source code of my program:
# Include
# Include
# Include
# Include

Void * create1 (void * arg)
{
Int I = 0;
Int * num;
Num = (int *) arg;

For (I = 0; I <6; I ++)
{
Printf ("This is a pthread1. I = % d, attr = % d \ n", I, * num );
Sleep (1 );
}
}

Int main (int argc, char * argv [])
{
Pthread_t id1, id2;
Int error;

Int test1 = 1;
Int * attr1 = & test1;
Int test2 = 2;
Int * attr2 = & test2;

Error = pthread_create (& id1, NULL, create1, (void *) attr1 );
Error = pthread_create (& id2, NULL, create1, (void *) attr2 );

Printf ("pthread_create is created... \ r \ n ");
Pthread_join (id1, NULL );
Pthread_join (id2, NULL );

Exit (0 );
}


The test result is:
This is a pthread1 I = 0, attr = 1
This is a pthread1 I = 0, attr = 2
// Interval: one second
This is a pthread1 I = 1, attr = 1
This is a pthread1 I = 1, attr = 2
// Interval: one second

This is a pthread1 I = 2, attr = 1
This is a pthread1 I = 2, attr = 2
// Interval: one second
This is a pthread1 I = 3, attr = 1
This is a pthread1 I = 3, attr = 2
// Interval: one second
This is a pthread1 I = 4, attr = 1
This is a pthread1 I = 4, attr = 2
// Interval: one second
This is a pthread1 I = 5, attr = 1
This is a pthread1 I = 5, attr = 2
// Interval: one second

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.