Linux Processes and Threads six

Source: Internet
Author: User

//inter-thread communication#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<pthread.h>/*thread 1 To control thread communication method the global variable*/intg_num=Ten;void*thread_run (void*Arg) {    //thread 1 is detached, but it is best not to use the Pthread_join () function to accept, multi-threaded error memory overflowPthread_detach (Pthread_self ()); int*p= (int*) Arg; (*P) + +; G_num= A; /*thread 1 arguments to the control thread communication Method 2--pthread_exit () function*/    //Pthread_exit (P); //after executing the pthread_exit () function, the thread exits directlyprintf"thread 1 has exited! \ n"); /*Thread 1 communication method with control thread 3--in thread return*/    returnp;}intMainintArgChar*args[]) {    /** Each thread has its own stack memory, but the stack memory between each other can be accessed, * For example, in the control thread defined variables, can be accessed in thread 1 * threads 1 defined variables may not be able to control the thread access, because the variable cannot be determined because the thread is executed Freed Up **/    intindex=0;    pthread_t Thr1; if(Pthread_create (&thr1,null,thread_run,&index)! =0) {printf ("pthread_create () failed!\n"); return-1; }    //waiting for thread 1    int*p=NULL; Pthread_join (THR1, (void* *) &p); printf ("The return value of thread 1%d\n",*p); printf ("controlling the index=%d\n of a thread", index); printf ("g_num=%d\n", G_num); return 0;}

//parallel execution of Threads#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<pthread.h>void*thread_run (void*Arg) {    //Pthread_detach (Pthread_self ());    if(Arg! =NULL) {        int*p = (int*) Arg; inti =0;  for(i =0; i < *p; i++) {printf ("This is the%d circle of thread%d! \ n", (int) pthread_self (), i); Sleep (1); }    }    returnNULL;}intMainintArgChar*args[]) {    inti =0; intNUM1 =Ten; intnum2 =Ten; printf ("Please enter the number of creation threads: \ n"); scanf ("%d", &NUM1); printf ("Please enter the number of laps per thread: \ n"); scanf ("%d", &num2); pthread_t thrs[ $]; /** Multithreading is run in parallel **/     for(i =0; i < NUM1; i++)    {        if(Pthread_create (&thrs[i], NULL, Thread_run, &num2)! =0) {printf ("pthread_create () failed!\n"); return-1; }    }     for(i =0; i < NUM1; i++)    {        /*If there is no Pthread_join (), then the control thread will not wait for the child thread and will exit directly */Pthread_join (thrs[i],null); }    return 0;}

Linux processes and Threads six

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.