"Linux" A simple example of thread creation and synchronization

Source: Internet
Author: User

Recently a lot of energy on Linux, today a simple look at Linux threads and synchronization, in fact, regardless of windows or linux,os layer of many principles and concepts are the same, many Windows experience and concepts can be completely ported to Linux.

Today, the creation thread and a blocking thread synchronization function are used.

A few of the functions used

#include <pthread.h>//Creating Threadsintpthread_create (pthread_t* Thread,/*line marker ID, pthread_t pthread_self (void) to get the current thread ID*/pthread_attr_t* attr,/*thread Properties, if no need can be 0*/void* (*start_routine) (void*),/*Thread Functions*/void* ARG/*Thread function Parameters*/); The return value succeeds:0Failure: Error code//terminating a threadvoidPthread_exit (void* retval/*the value returned when the thread returns, attention to local variables and other issues*/)//blocking-Thread synchronizationintpthread_join (pthread_t th,/*the value returned by the first parameter of the Pthread_create function*/void* * Thread/*thread function return value, memory allocated inside this function*/) //gets the current thread IDpthread_t pthread_self (void)

Paste the code

   /*a demo for Linux multithread*/  3#include <iostream>4#include <pthread.h>5#include <stdlib.h>6#include <unistd.h>7 using namespacestd; 8   9 //thread function Ten void* Start_routine (void*p) One {  A     if(0==p) -         return 0;  -   thesize_t nloops = * (size_t*) (p);  -   -      for(size_t i =0; i < Nloops; ++i) -     {  +cout << i <<Endl; -Usleep +* -);//Ms  +     }  A   atcout << Endl <<"This thread ID is"<< pthread_self () <<Endl; -   -     return 0;  - }  -   -   in intMain () - {  topthread_t PtThread1; +size_t* Ploops =Newsize_t (Ten);  -     intNret = Pthread_create (&ptthread1,0, Start_routine, (void*) ploops);  the     if(0!=nret) *Cerr << Endl <<"Create thread error!"<<Endl; $     Else Panax NotoginsengCerr << Endl <<"Create thread successfully, return value code is"<<Nret -<< Endl <<"Thread ID is"<< PtThread1 <<Endl; the   +     if(0==nret) A     {  thecout << Endl <<"Wait for thread"<< PtThread1 <<Endl; +         void* pRetVal =0;  -         intNjoinret = Pthread_join (PtThread1, (void* *) &pretval);  $cout << Endl <<"Thread"<< PtThread1 <<"finished!"<<Endl; $cout <<"thread return value is"<< (Char*) pRetVal <<Endl; -     }  -cout <<Endl; the   -Delete ploops;WuyiPloops =0;  the   -System"ls");  Wu   -     return 0;  About}

Execution results

PS: Note that you may need to add-lpthread to the g++ compilation parameters

"Linux" A simple example of thread creation and synchronization

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.