Parse multi-thread programming in Linux and pass multiple parameters

Source: Internet
Author: User
Article Title: Resolve multi-threaded programming in Linux and pass multiple parameters. Linux is a technology channel of the IT lab in China. Some basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source are included. multi-threaded programming in Linux and multiple parameter instances are passed, let's first look at the content. This morning, I tested multi-threaded programming in Linux and passed multiple parameters to the function to be executed by the thread. After compiling in Linux, it is moved to the Android simulator.
The source code of the experiment program is as follows:
Pthread. c
# Include
# Include
# Include
# Include
# Include
Struct argument
{
Int num;
Char string [30];
};
Int main ()
{
Int I, ret;
Void * thread‑func (void *);
Void * thread2_func (void *);
Void * thread_return1, * thread_return2;/* is used to receive the return value after two threads exit */
Pthread_t thread1, thread2;
Struct argument arg1, arg2;
Arg1.num = 1949;
Strcpy (arg1.string, "founding of the People's Republic of China ");
Arg2.num = 2009;
Strcpy (arg2.string, "60 th anniversary of the founding of the People's Republic of China ");
Pthread_create (& thread1, NULL, thread1_func, (void *) & arg1 );
Pthread_create (& thread2, NULL, thread2_func, (void *) & arg2 );
For (I = 0; I <= 2; I ++)
{
Printf ("I am the initial process! \ N ");
Sleep (2 );
}
Ret = pthread_join (thread1, & thread_return1);/* wait for the first thread to exit and receive its return value */
If (ret! = 0)
Printf ("An error occurred while calling pthread_join to obtain the return value of thread 1! \ N ");
Else
Printf ("pthread_join call successful! The value brought back after thread 1 exits is % d \ n ", (int) thread_return1 );
Ret = pthread_join (thread2, & thread_return2);/* wait for the second thread to exit and receive its return value */
If (ret! = 0)
Printf ("An error occurred while calling pthread_join to obtain the return value of thread 2! \ N ");
Else
Printf ("pthread_join call successful! The value brought back after thread 2 exits is % d \ n ", (int) thread_return2 );
Return 0;} void * thread1_func (void * arg)
{
Int I;
Struct argument * arg_thread1;/* This defines a pointer to arg_thread1 of the argument type struct, which is used to receive the address of the passed parameter */
Arg_thread1 = (struct argument *) arg;
For (I = 0; I <= 3; I ++)
{
Printf ("I'm from thread 1, and the parameter passed to me is % d, % s. \ N ", (* arg_thread1 ). Num, (* arg_thread1 ). String );
Sleep (2 );
}
Return (void *) 123 ;}
Void * thread2_func (void * arg)
{
Int I;
Struct argument arg_thread2;/* It defines an argument type structure arg_thread2, which is used to receive the value pointed to by the passed pointer parameter. This method is both feasible in the threadjavasfunc function */
Arg_thread2 = * (struct argument *) arg;
For (I = 0; I <= 3; I ++)
{
Printf ("I'm from thread 2, and the parameter passed to me is % d, % s. \ N ", arg_thread2.num, arg_thread2.string );
Sleep (2 );
}
Return (void *) 456;
} Above is the content of pthread. c.

[1] [2] Next page

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.