Example of communication between multiple threads

Source: Internet
Author: User

Example of communication between multiple threads
For threads, to say plainly, is a function, if everyone has an understanding of this chapter function, then I
For the operating system, there is a new understanding of the communication between threads and processes! Next I'll take each line of code
Comments, in the process, you can also have a new understanding of C language.

The first function creates two threads.

#include <stdio.h>
#include <pthread.h> This header function is to be included because our subsequent functions are system calls, so we need to apply the header function

This allows you to find the source file for this function at compile time.


void *mythread1 (void)//Declare a pointer function, for function pointers, and pointer functions, for beginners may be very easy to mix,
{The function pointer is equivalent to having a pointer to a function, a pointer function, and a return value is a pointer type.}
int i;
for (i=0; i<100; i++)
{
printf ("This was the 1st pthread,created by zieckey.\n");
Sleep (1);//let this thread to sleep 1 Second,and then continue to run
}
}

void *mythread2 (void)
{
int i;
for (i=0; i<100; i++)
{
printf ("This was the 2st pthread,created by zieckey.\n");
Sleep (1);
}
}

int main ()
{
int i=0, ret=0;
pthread_t Id1,id2;

/* Create thread 1*/
ret = pthread_create (&AMP;ID1, NULL, (void*) myThread1, null); Here we call the function of the system creation thread
if (ret) first argument, thread ID, second type, can default, third is thread function, note is pointer type!
{
printf ("Create pthread error!\n");
return 1;
}

/* Create thread 2*/
ret = pthread_create (&AMP;ID2, NULL, (void*) myThread2, NULL);
if (ret)
{
printf ("Create pthread error!\n");
return 1;
}

Pthread_join (ID1, NULL); The thread waits for the function, and the same nature as the wait () of the process
Pthread_join (Id2, NULL);

return 0;
}


/******* This function implements the Pthread_join () function ************/


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

void *thread (void *str)
{
int i;
for (i = 0; i <; ++i)
{
Sleep (2);
printf ("This in the thread:%d\n", i);
}
return NULL;
}

int main ()
{
pthread_t PTH;
int i;
int ret = Pthread_create (&pth, NULL, Thread, (void *) (i));

Pthread_join (PTH, NULL); First parameter, waiting for the thread ID
printf ("123\n");
for (i = 0; i <; ++i)
{
Sleep (1);
printf ("This in the main:%d\n", i);
}

return 0;
}


/*********** the execution order between threads **************/

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

void *create (void *arg)
{
printf ("New thread is created ... \ n");
return (void *) 8;
}

int main (int argc,char *argv[])
{
pthread_t Tid;
int error;
void *temp;

Error = pthread_create (&tid, NULL, create, NULL);
printf ("Main thread!\n");

if (Error)
{
printf ("Thread is not created ... \ n");
return-1;
}
Error = Pthread_join (tid, &temp);

if (Error)
{
printf ("Thread is not exit ... \ n");
Return-2;
}

printf ("Thread is exit code%d \ n", (int) temp);
return 0;
}

/*********** test the ID relationship of the parent process and the child thread ***************/

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>/*getpid () */

void *create (void *arg)
{
printf ("New thread ... \ n");
printf ("This thread ' s ID is%u \ n", (unsigned int) pthread_self ());
printf ("The process PID is%d \ n", Getpid ());
return (void *) 0;
}

int main (int argc,char *argv[])
{
pthread_t Tid;
int error;

printf ("Main thread is starting ... \ n");

Error = pthread_create (&tid, NULL, create, NULL);

if (Error)
{
printf ("Thread is not created ... \ n");
return-1;
}
printf ("The main process ' s PID is%d \ n", Getpid ());
Sleep (1);
return 0;
}

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

void *create (void *arg)
{
Char *name;
Name= (char *) arg;
printf ("The parameter passed from main function is%s \ n", name);
return (void *) 0;
}

int main (int argc, char *argv[])
{
Char *a= "Zieckey";
int error;
pthread_t TIDP;

Error=pthread_create (&TIDP, NULL, create, (void *) a);

if (error!=0)
{
printf ("Pthread is not created.\n");
return-1;
}
Sleep (1);
printf ("Pthread is created ... \ n");
return 0;
}

Example of communication between multiple threads

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.