Analyze multithreaded programming in Linux and pass multiple parameters __arduino

Source: Internet
Author: User
Multi-threaded programming in Linux and passing multiple parameter instances is the content of this article, not to say, first look at the content. This morning I experimented with multithreaded programming under Linux and passed multiple parameters to a function that the thread was going to execute. After compiling in the Linux environment, it also moves it to the Android emulator.
The following is the source code of the experimental program:
Pthread.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
struct argument
{
int num;
Char string[30];
};
int main ()
{
int I,ret;
void *thread1_func (void *);
void *thread2_func (void *);
void *thread_return1,*thread_return2;/* is used to receive the return value after two threads have exited.
pthread_t thread1,thread2;
struct argument arg1,arg2;
arg1.num=1949;
strcpy (arg1.string, "Establishment of the People's Republic of China");
arg2.num=2009;
strcpy (arg2.string, "60 anniversary of the founding of the Nation");
Pthread_create (&thread1,null,thread1_func, (void *) &AMP;ARG1);
Pthread_create (&thread2,null,thread2_func, (void *) &AMP;ARG2);
for (i=0;i<=2;i++)
{
printf ("I was the original process.") \ n ");
Sleep (2);
}
Ret=pthread_join (THREAD1,&AMP;THREAD_RETURN1); * Wait for the first thread to exit and receive its return value * *
if (ret!=0)
printf ("Call Pthread_join get thread 1 return value error.") \ n ");
Else
printf ("Pthread_join call succeeded.") The value returned after thread 1 exits is%d\n ", (int) thread_return1);
Ret=pthread_join (THREAD2,&AMP;THREAD_RETURN2); * Wait for the second thread to exit and receive its return value * *
if (ret!=0)
printf ("Call Pthread_join get thread 2 return value error.") \ n ");
Else
printf ("Pthread_join call succeeded.") The value returned after thread 2 exits is%d\n ", (int) thread_return2);
return 0; } void *thread1_func (void *arg)
{
int i;
struct argument *arg_thread1;/* here defines a pointer arg_thread1 to the argument type structure, which is used to receive the address of the passed parameter.
arg_thread1= (struct argument *) arg;
for (i=0;i<=3;i++)
{
printf ("I came from thread 1, passed to my parameter is%d,%s.") \ n ", (*ARG_THREAD1). Num, (*ARG_THREAD1). string);
Sleep (2);
}
return (void *) 123; }
void *thread2_func (void *arg)
{
int i;
struct argumentarg_thread2;/* here defines a argument type of struct arg_thread2, which is used to receive the values that the passed pointer parameter points to. This method and the method in the Thread1_func function are all feasible.
arg_thread2=* (struct argument *) arg;
for (i=0;i<=3;i++)
{
printf ("I came from thread 2, passed to my parameter is%d,%s.") \ n ", arg_thread2.num,arg_thread2.string);
Sleep (2);
}
return (void *) 456;
Above is the content of PTHREAD.C
In the Linux terminal with the Gcc-o pthread-lpthread pthread.c command to compile, note should be added-lpthread
./pthread Run Results
I came from thread 2, passed to my parameter is 2009, the founding of the 60 anniversary.
I come from thread 1, pass to me the parameter is 1949, the People's Republic of China was founded.
I was the first process.
I came from thread 2, passed to my parameter is 2009, the founding of the 60 anniversary.
I was the first process.
I come from thread 1, pass to me the parameter is 1949, the People's Republic of China was founded.
I was the first process.
I come from thread 1, pass to me the parameter is 1949, the People's Republic of China was founded.
I came from thread 2, passed to my parameter is 2009, the founding of the 60 anniversary.
I came from thread 2, passed to my parameter is 2009, the founding of the 60 anniversary.
I come from thread 1, pass to me the parameter is 1949, the People's Republic of China was founded.
The Pthread_join call succeeded. The value returned after thread 1 exits is 123
The Pthread_join call succeeded. The value returned after thread 2 exits is 456
You can see from the order of execution that there is competition between threads, not in a fixed order.
After that, move it to the Android emulator and write the Android.mk file as follows
Android.mk
Local_path:= $ (call My-dir)
Include $ (clear_vars)
local_src_files:= \
Pthread.c
local_cflags= #
Note that there must be a local_ldlibs way to load the Pthread library, you cannot use "Local_cflags=-lpthread" or "Local_shared_libraries:=libpthread" or "Local_" Static_libraries: =libpthread "The way to load, otherwise it will fail.
Local_ldlibs + +-lpthread
local_module:= pthread include $ (build_executable)
The above is the content of ANDROID.MK
Compile execution and run the results similar to those in Linux.
Summary: Analyze the multithreaded programming in Linux and pass the contents of multiple parameter instances finished, I hope this article is helpful to you.
Related Article

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.