Multi-threaded programming in C language

Source: Internet
Author: User

a long time ago, my knowledge of C language is not a lot, I first heard that multithreaded programming is Java, in fact, C language also has multi-threading programming, and more simple, convenient, powerful. Let's take a quick look at the multi-threaded programming in UNIX C language environment! Let's look at a simple single-threaded thread:/*06.3.6 sghello.c Hello,world--Single Thread*/#include#defineNUM 6intMain () {voidPrint_msg (Char*); Print_msg ("Hello,"); Print_msg ("world!");}voidPrint_msg (Char*m) {    inti;  for(i=0; I {printf ("%s", M);        Fflush (stdout); Sleep (1); }} reflects the execution process of the program: execution result: $./Sghello.exehello,hello,hello,hello,hello,hello,world!world!world!world!world!world!So what if you want to execute two calls to the PRINT_MSG function at the same time and want to use fork to build two new processes? This idea is clearly reflected in: So how to achieve this effect? We can use the function pthread_create to create a new thread. Function Prototypes:intPthread_create (pthread_t *thread,pthread_attr_t*attr,void* (*func) (void*),void*arg); parameter: thread pointer to pthread_t type variable attr point to pthread_attr_t type                         Pointer to a variable, or null Func pointer to the function that the new thread is running. Arg passed to func parameter return value 0Successful return Errcode error we can use the function Pthread_join to wait for the end of a process. Function Prototypes:intPthread_join (pthread_t thread,void**retval); parameters: The process the thread waits for retval a variable return value that points to the return value of a stored thread:0successful return errorcode error The above two functions are included in the header file pthread.h. See the multi-threaded version of Hello,world!/*06.3.6 mhello1.c Hello,world--Multile Thread*/#include # include#defineNUM 6intMain () {voidPrint_msg (void*);    pthread_t t1,t2; Pthread_create (&t1,null,print_msg, (void*)"Hello,"); Pthread_create (&t2,null,print_msg, (void*)"world!\n");    Pthread_join (T1,null);   Pthread_join (T2,null); }voidPrint_msg (void*m) {    Char*cp= (Char*) m; inti;  for(i=0; I {printf ("%s", M);        Fflush (stdout); Sleep (1); }} Run Result:
$ gcc mhello1.c-o mhello1.exe$./Mhello1.exehello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!C language Once expanded my horizons, multi-threaded problems There are many, such as the division of labor between threads, the use of mutual exclusion mechanism to ensure the safe sharing of data between threads, using conditional variables to synchronize the transmission between threads, passing multiple parameters to the thread, if the reader is interested, can go deep. Recommended "Understanding Unix/linux Programming "

Multi-threaded programming in C language

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.