The delivery of C + + multithreading parameters

Source: Internet
Author: User

#include <iostream>#include<pthread.h>//multi-Threaded related operation header file, portable many platformsusing namespacestd;structmypara{intPARA1;//parameter 1    Char*para2;//Parameter 2pthread_t wait;};void* THREAD1 (void* args)//This function demonstrates that the data is outgoing{Mypara*my = (Mypara *) args; Srand (Unsigned (0))); My-&GT;PARA1 = rand ()% -; My->para2 =". "; cout<<"end of assignment to struct"<<Endl; return 0;} void* THREAD2 (void* args)//This function demonstrates that the data is passed in{Mypara*str_in = (Mypara *) args; cout<<"Thread 2 starts to run ........ ........."<<Endl; Pthread_join (str_in->wait,null);//You need to wait for the thread1 thread to finish assigning the struct struct to runcout <<"the random number generated by thread 1 is:"<< str_in->para1<<" "<< Str_in->para2 <<Endl; return 0;} intMain () {pthread_t tid1,tid2;    Mypara My_para; intret = Pthread_create (&AMP;TID1, NULL, Thread1, (void*) &My_para); //Pthread_join (tid1,null);My_para.wait =Tid1; Pthread_create (&tid2, NULL, Thread2, (void*) &My_para); System ("Pause"); Pthread_exit (NULL); }

C + + Multithreading parameter Transfer-pass the parameters through the struct. (Pthread multi-threaded class library, cannot compile on x64 bit)

The delivery of C + + multithreading parameters

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.