I. POSIX thread first-off function
POSIX line Libraries
thread-related functions form a complete series, and most functions have names that begin with "Pthread".
To use these libraries, introduce header files <pthread.h>
The "-lpthread" option to use the compiler command when connecting these thread function libraries
1.pthread_create function
Function:Create a new thread
Prototype:int Pthread_create (pthread_t *thread, const pthread_attr_t *attr,
void * (*start_routine) (void *), void *arg);
Parameters:
Thread: Returns the threading ID
attr: Setting the properties of a thread, attr null means using the default property
Start_routine: is a function address, the thread starts to execute this function
ARG: Arguments to a function after a thread is started
return value:successful return 0, failure return error code
Error checking
(1) Traditionally some functions have succeeded in returning 0, failing to return 1, and assigning a value to the global variable errno to indicate an error.
(2) The Pthreads function does not set the global variable errno (while most other POSIX functions do). Instead, the error code is returned by the return value.
(3) Pthreads also provides errno variables within the thread to support other code that uses errno. For errors in the Pthreads function, it is recommended that the return value be determined because reading the return value is less expensive than the errno variable within the read thread.
2.pthread_exit function
Function:Thread Termination
Prototype:
void Pthread_exit (void * value_ptr);
Parameters:
Value_ptr:value_ptr do not point to a local variable.
return value:no return value, like a process, cannot be returned to its caller (itself) at the end of the thread
3.pthread_join function
Function:wait for thread to end
Prototype:
int Pthread_join (pthread_t thread,void * * value_ptr);
Parameters:
Thread: Threads ID
Value_ptr: It points to a pointer, which points to the return value of the thread
return value:successful return 0, failure returns error code.
3.pthread_self function
Function:return thread ID
Prototype:pthread_t pthread_self (void);
return value:successful return 0
4.pthread_cancel function
Function:to cancel a thread in execution
Prototype:
int pthread_cancel (pthread_t thread);
Parameters:
Thread: Threads ID
return value:successful return 0, failure to return error code
5.Pthread_detach function
Function:Detach one thread from the other
Prototype:
int Pthread_detach (pthread_t thread);
Parameters:
Thread: Threads ID
Return value: successfully returned 0, failed to return error code
Case code:
Pthread.c
#include <unistd.h> #include <sys/types.h> #include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #define ERR_EXIT (m) do {perror (M); EXIT (exit_failure ); }while (0) void* thread_routine (void *arg) {int i;for (i=0;i<20;i++) {printf ("B"); Fflush (stdout); usleep; if (i==3 ) Pthread_exit ("When i==3, Pthread exit");} Sleep (3); Delay the end of the child thread return 0;} int main () {pthread_t tid;int ret;//error message returned by function if (ret = pthread_create (&tid,null,thread_routine,null))!=0) {Fprin TF (stderr, "pthread_create:%s\n", Strerror (ret)); Exit (exit_failure);} int i; For the main thread, print the letter A for (i=0;i<20;++i) {printf ("a"); Fflush (stdout);//Flush Output Buffer usleep (20);} Wait for the end of the child thread to void *value;if ((ret = Pthread_join (tid,&value))! = 0) {fprintf (stderr, "pthread_create:%s\n", Strerror ( ret)); exit (exit_failure);} printf ("\ n");p rintf ("return message:%s\n", (char*) value); return 0;}
two. Using thread to implement the client/server program
#include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h># Include <arpa/inet.h> #include <pthread.h> #include <stdlib.h> #include <stdio.h> #include < errno.h> #include <string.h> #define ERR_EXIT (m) do {perror (M); Exit (Exit_failure); } while (0) void echo_srv (int conn) {char recvbuf[1024]; while (1) {memset (recvbuf, 0, sizeof (RECVBUF)); int ret = READ (conn, recvbuf, sizeof (RECVBUF)); if (ret = = 0) {printf ("Client close\n"); Break } else if (ret = =-1) err_exit ("read"); Fputs (Recvbuf, stdout); Write (conn, recvbuf, ret); } close (conn);} void *thread_routine (void *arg) {////The main thread does not call Pthread_join wait for thread to exit//peel thread, avoid generating a zombie thread int conn = (int) arg; PTHREAD_SELF return thread ID//Pthread_detach detach thread Pthread_detach (Pthread_self ()); int conn = * ((int *), ARG); Casts an untyped pointer to int* pointer free (ARG); Take out the value, free ECHO_SRV (conn); Each thread handles one connection, and the same process does not have a listener socket printf ("Exiting thread ... \ n"); return NULL;} int main (void) {int listenfd; if ((LISTENFD = socket (pf_inet, Sock_stream, ipproto_tcp)) < 0) Err_exit ("socket"); struct sockaddr_in servaddr; memset (&servaddr, 0, sizeof (SERVADDR)); servaddr.sin_family = af_inet; Servaddr.sin_port = htons (5188); SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any); int on = 1; if (setsockopt (LISTENFD, Sol_socket, so_reuseaddr, &on, sizeof (ON)) < 0) Err_exit ("setsockopt"); if (Bind (LISTENFD, (struct sockaddr *) &servaddr, sizeof (SERVADDR)) < 0) Err_exit ("bind"); if (Listen (LISTENFD, Somaxconn) < 0) Err_exit ("Listen"); struct sockaddr_in peeraddr; socklen_t Peerlen = sizeof (PEERADDR); int conn; while (1) {if (conn = Accept (LISTENFD, (struct sockaddr *) &peeraddr, &peerlen)) < 0) Err_exit ("accept"); printf ("ip=%s port=%d\n", Inet_ntoa (PEERADDR.SIN_ADDR), Ntohs (Peeraddr.sin_port)); pthread_t Tid; int ret; /*pthread_create (&tid, NULL, Thread_routine, (void*) &conn); *///Race condition problem, state of the question int * p = malloc (sizeof (int)); *P = conn; int ret; if (ret = Pthread_create (&tid, NULL, thread_routine,p)) = 0)//64-bit system when pointer is not 4 bytes, non-portable, all using malloc, {fprintf (stderr, "pthread_create:%s\n", Strerror (ret)); Exit (Exit_failure); } }}
POSIX Thread (i)