UNIX Network Programming Volume 1 server Programming Paradigm 7 pre-create thread to protect accept with mutex lock mode

Source: Internet
Author: User
Tags terminates

This article is Senlie original, reprint please retain this address:Http://blog.csdn.net/zhengsenlie


1. Pre-Create a thread pool and have each thread call the Accept

2. Use a mutex instead of having each thread block in the Accept call


The thread structure that is used to maintain information about each thread the typedef struct {pthread_tthread_tid;/* Threads ID */longthread_count;/* processing the number of connections */} Thread; thread*tptr;/* the thread structure pointer to a calloc-generated thread structure array *///global variables, including a listener socket and a mutex variable to be shared by all threads INTLISTENFD, Nthreads;socklen _taddrlen;pthread_mutex_tmlock;/* include serv07 */#include "unpthread.h" #include "pthread07.h" Pthread_mutex_tmlock = Pthread_mutex_initializer;intmain (int argc, char **argv) {inti;voidsig_int (int), thread_make (int),//1. Create a listen socket if ( ARGC = = 3) LISTENFD = Tcp_listen (NULL, argv[1], &addrlen); else if (argc = = 4) LISTENFD = Tcp_listen (argv[1], argv[2], &A Mp;addrlen) Elseerr_quit ("usage:serv07 [

UNIX Network Programming Volume 1 server Programming Paradigm 7 pre-create thread to protect accept with mutex lock mode

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.