UDP, thread, Mutex lock (DAY15)

Source: Internet
Author: User
Tags joins mutex

a UDP-based network programming model server-side1, create a socket.2, bind the IP address and port number of the FD and server3, Recvfrom blocking waits to receive client data4, Business processing5, responding to client clients:1, create a socket2, sending data to the server SendTo3, blocking the response information waiting for the server4, processing response information5, break communication # include<sys/types.h>#include<sys/socket.h>ssize_t Recvfrom (intSOCKFD,void*buf,size_t Len,intflags,structSOCKADDR *src_addr, socklen_t*Addrlen); function: Receive message parameters from a socket: SOCKFD: Specify socket. Socket (2The return value of the BUF: the buffer address that holds the message len: Specifies the maximum size of the BUF flags:0src_addr: Store The opposite address Addrlen: is a value-The result parameter. Src_addr length return value: The number of bytes received successfully returned-1error errno is set0opposite down machine # include<sys/types.h>#include<sys/socket.h>ssize_t sendto (intSOCKFD,Const void*buf, size_t Len,intflags,Const structSOCKADDR *dest_addr, socklen_t addrlen); function: Send message on socket parameter: SOCKFD: Specify SOCKETBUF: Buffer for the first address len:buf the number of bytes valid in the data flags:0dest_addr: Destination address Addrlen: The length of the destination address return value:-1error errno was set to return the number of bytes sent out successfully. Write code to implement UDP-based network communication. Code See: Userv.c uclie.c172.30.3.93network communication Two, the basic unit of thread execution, the resource process process of thread sharing process is the basic unit of resource allocation each thread has its own tid. THREAD_ID each thread has its own private stack frame. Third, the thread creation system provides the function pthread_create (3) to create a thread # include<pthread.h>intPthread_create (pthread_t *Thread,Constpthread_attr_t *attr,void* (*start_routine) (void*),         void*arg); function: Creates a new thread parameter: thread: The buffer that holds the thread ID. Attr:null default Property Start_routine: The only parameter of the thread's execution function Arg:start_routine function. return value:0Success Error error code compile and link with-pthread.void* (*start_routine) (void*For example, create a new thread code see PTHREAD_C.CGETPID (2) Gets the PID of the process. Pthread_self (3) to get the thread's own tid. #include<pthread.h>pthread_t pthread_self (void); function: Gets the ID parameter of the current thread:voidreturn value: Returns the ID of the thread. Iv. thread exit, convergence, detach thread exit1, return, and exit (3the difference of return is just the return of the function, in the thread handler function, which represents the end of the threads only. and exit (3) represents the end of the process. All threads in the process are terminated. 2, using the function pthread_exit (3) to terminate a thread # include<pthread.h>voidPthread_exit (void*retval); function: Terminates the current thread parameter: retval: Specifies the value passed to another thread that calls Pthread_join (3) to receive this value. Return value: Not returned to the caller. 3, Pthread_cancel (3) #include<pthread.h>intpthread_cancel (pthread_t thread); function: sends a cancel request parameter to the thread: thread: Specifies the thread ID that receives the request. return value:0successful non-0 error code Note: Use Pthread_cancel to terminate the process when using Pthread_join (3Gets the thread exit information when it gets to the pthread_canceled. The convergence of Threads Pthread_join (3) waiting for a thread to converge # include<pthread.h>intPthread_join (pthread_t thread,void**retval); function: joins a terminated thread parameter: Thread: Specifies the idretval of the thread waiting to converge: return value: Success0Failure return error code for example, the exit and convergence codes of the thread are described in the detach Pthread_detach of the pthread_e.c threads (3) #include<pthread.h>intPthread_detach (pthread_t thread); function: Detach a thread parameter: thread: Specifies the return value of the thread to detach:0successful non-0 error code example of a disconnected code for thread threads see PTHREAD_D.C new threads and threads that already exist in the process are asynchronous. These threads compete against public resources. How to solve the competition? 1, reentrant functions2, which allows asynchronous threads to synchronize access to shared resources. V. Thread synchronization conditional variable mutex lock semaphore examples of multiple threads accessing shared resources asynchronously (critical resources) code see count.c Using mutex locks to resolve critical resource issues what is a mutex lock? Pthread_mutex_t is a type mutex lock type mutex is a mutex device. A variable of a mutex lock type has two state unlocked: not owned by any thread locked: a mutex lock owned by a thread can never be owned by two threads at the same time. If a thread wants to own a mutex lock, it is occupied by another thread. Then this thread suspends execution until another thread abandons it to get it. Access to critical resources is subject to three-foot:1, get the mutex lock first2, Access critical resources3, release the mutex lock Phtread_mutex_init (3) #include<pthread.h>static initialization of a mutex lock pthread_mutex_t Fastmutex=Pthread_mutex_initializer;intPthread_mutex_init (pthread_mutex_t *mutex,Constpthread_mutexattr_t *mutexattr); function: Initialize a mutex lock parameter: Mutex: Specifies the mutex lock mutexattr:null default return value to initialize:0intPthread_mutex_lock (pthread_mutex_t *mutex); function: Gets the mutex lock, if the lock is not occupied by another thread, returns immediately, owning the lock. Change the state of the lock to locked. This lock is occupied by other threads. Suspends the thread until the other line threads unlocked. Parameters: Mutex: Specifies the return value of the mutex lock to get: not 0 error0SuccessintPthread_mutex_trylock (pthread_mutex_t *mutex); function: Gets the mutex lock, which is non-blocking when other threads occupy the mutex lock. return immediately, error. Ebusy parameter: Mutex: Specifies the return value of the mutex lock to get: not 0 error0SuccessintPthread_mutex_unlock (pthread_mutex_t *mutex); function: Unlock mutex parameter: Mutex: Release mutex lock return value: not 0 error0SuccessintPthread_mutex_destroy (pthread_mutex_t *mutex); function: Destroy mutex lock parameter: Mutex: Specifies the mutex lock return value to destroy: not 0 error0successfully improved count.c. Use a mutex lock to allow the process to synchronize access to critical resources. Summary: First, UDP-based programming model two, the thread Foundation three, the thread creation four, the thread exits, joins, separates five, the thread synchronizes the mutex lock

UDP, thread, Mutex lock (DAY15)

Related Article

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.