POSIX Thread Libraries Review

Source: Internet
Author: User
Tags posix

Create a new thread

int Pthread_create (pthread_t *thread, const pthread_attr_t *attr,
void * (*start_routine) (void *), void *arg);

Thread: Returns the threading ID

attr: Sets the properties of the thread, and attr is null to use the default property.

Start_toutine: is a function address, the function to execute after the thread starts

ARG: Arguments passed to the thread start function.

Successful return 0, failure return error code;

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<pthread.h>#include<unistd.h>#include<sys/types.h>#defineErr_exit (M) Do{perror (M);    Exit (Exit_failure); } while(0)void* Therad_routine (void*Arg) {    inti;  for(i =0; I < -; ++i) {/*Code*/printf ("B");    Fflush (stdout); }    return 0;}intMainintargcConst Char*argv[])    {pthread_t tid; intret; RET= Pthread_create (&tid, NULL, therad_routine, NULL); if(Ret! =0) {fprintf (stderr,"pthread_create:%s\n", Strerror (ret));    Exit (Exit_failure); }    inti;  for(i =0; I < -; ++i) {/*Code*/printf ("A");        Fflush (stdout); Usleep ( -); } Sleep (1); return 0;}
When the thread creation fails, the error message is returned to errno.

Pthread also provides thread-and-errno variables, one that supports other code that uses errno,

For the error of the Pthreads function, it is easy to determine by the return value, because the anger return value is less expensive than the errno variable within the pique thread.

Print results

Abababababab

It can be seen that the primary thread and the thread are shared CPU time slices in the case of a single core.

They are performed alternately.

ret = pthread_join (tid, NULL);     if 0 )    {        "pthread_join:%s\n", Strerror (ret));        Exit (exit_failure);    }

The main thread uses the Pthread_join to collect threads resources.

Zombie threads also spawn in multiple threads, relative to the concept of a multi-process zombie process.

So we can use Pthread_detach to separate a thread so that it doesn't spawn a zombie thread.

The following is the use of thread functions to implement a simple back-firing server practice

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>#defineErr_exit (M) Do{perror (M);    Exit (Exit_failure); } while(0)void* Thread_routine (void*Arg)    {Pthread_detach (pthread_self ()); intconn = (int) Arg;    Echo_sev (conn); printf ("exiting thread ... \ n"); return 0;}intMainintargcConst Char*argv[]) {    intListen = socket (pf_inet, Sock_stream,0); if(Socket <0) Err_exit ("Socket"); structsockaddr_in servraddr; Servaddr.sin_family=af_inet; Servaddr.sin_port= Htons (5188); Servaddr.sin_addr.s_addr=htonl (Inaddr_any); intOn =1; //set up port multiplexing    if(Setsocketopt (LISTENFD, Sol_socket, SO_REUSEADDR, &on,sizeof(ON)) <0) Err_exit ("setsocketopt"); if(Bind (LISTENFD, (structsockaddr*) &servaddr,sizeof(SERVADDR)) <0) Err_exit ("Bind"); if(Listen (LISTENFD, Somaxconn) <0) Err_exit ("Listen"); structsockaddr_in peeraddr;    Socklen_t Peerlen; intConn;  while(1) {Conn= Accept (LISTENFD, (structsockaddr*) &peeraddr, &Peerlen); if(Conn <0) Err_exit ("Accept"); printf ("IP =%s, port =%d\n", Inet_ntoa (peeraddr.sin_addr, Ntohs (Peeraddr.sin_port)));        pthread_t Tid; intret; RET= Pthread_create (&tid, NULL, Thread_routine, (void*) conn); if(Ret! =0) {fprintf (stderr,"pthread_create:%s\n", Strerrno (ret));        Exit (Exit_failure); }    }        return 0;}

Note that it is important to detrach the created thread to avoid the creation of a zombie thread.

POSIX Thread Libraries Review

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.