high-performance iOS programming path-pthread-based thread pool

Source: Internet
Author: User
<span id="Label3"></p>Original link--http://blog.sina.com.cn/s/blog_7011f21c0101dkjj.html<p><p>In the OC framework, from Nsoperation to gcd, the dispatch queue is filled with the concept of queues, and the OC Framework helps us to write down the underlying Thread's schedule, and the benefit is that we can focus on the business logic at the top, The downside, of course, is that our control of the underlying dispatch becomes Weaker. The reason for writing this thread pool is also to practice practiced hand, as to the efficiency, after sending to several versions of the line, feedback can Also. Of course, there is room for continuous optimization.</p></p>I. Thread pool process 1. Create a fixed thread at the start of the program as a thread pool, waiting for dispatch 2. Thread scheduling based on task type when thread pool gets to Task 3. Task execution The entire process is simple, so we need to create some necessary structures to support our NEEDS.<p><p></p></p><p><p>typedef void (*TFMETHOD) (void *,short *);</p></p><p><p></p></p><p><p></p></p><p><p>typedef struct s_tftask_content{</p></p><p><p>Tfmethod method;</p></p><p><p>void *arg;</p></p><p><p>Short *cancel_point;</p></p><p><p>struct S_tftask_content *next;</p></p><p><p>}s_tftask_content;</p></p><p><p></p></p><p><p></p></p><p><p>typedef struct s_tfthread_config{</p></p><p><p>pthread_mutex_t mtx;</p></p><p><p>pthread_cond_t cond;</p></p><p><p>}s_tfthread_config;</p></p><p><p></p></p><p><p></p></p><p><p>typedef struct s_tftask{</p></p><p><p>S_tfthread_config config;</p></p><p><p>Short thread_status;</p></p><p><p>S_tftask_content *task_list;</p></p><p><p>}s_tftask;</p></p><p><p></p></p><p><p></p></p><p><p>Enum tfthread_status{</p></p><p><p>tfthread_init=0x0,</p></p><p><p>tfthread_idle,</p></p><p><p>tfthread_inuse,</p></p><p><p>Tfthread_dead</p></p><p><p>};</p></p><p><p></p></p><p><p></p></p><p><p>Enum tftask_type{</p></p><p><p>tftask_net_req=0x0,</p></p><p><p>tftask_first_respond,</p></p><p><p>tftask_assign,</p></p><p><p>Tftask_io_write</p></p><p><p>};</p></p><p><p></p></p><p><p></p></p><p><p>typedef struct s_tfthread{</p></p><p><p>S_tftask *tftask;</p></p><p><p>Char *name;</p></p><p><p>}s_tfthread;</p></p>Two. Implementation 1. Loop creates a thread through pthread_create, creates a S_tfthread object as a thread handle, joins a thread array, S_tftask_content->methord initializes to an empty function, and creates a task execution Function. After the task initialization function is executed, the currently created thread is suspended by the pthread_cond_wait signal in the execution Function.<p><p></p></p><p><p>S_tftask_content *content = (s_tftask_content *) malloc (sizeof (s_tftask_content));</p></p><p><p>content->next= NULL;</p></p><p><p>Content->method = empty_run;</p></p><p><p>content->cancel_point=null;</p></p><p><p></p></p><p><p>S_tftask *task = (s_tftask *) malloc (sizeof (s_tftask));</p></p><p><p>Task->task_list = content;</p></p><p><p>Task->thread_status = tfthread_idle;</p></p><p><p></p></p><p><p>S_tfthread_config config = {pthread_mutex_initializer,pthread_cond_initializer};</p></p><p><p>Task->config = config;</p></p><p><p></p></p><p><p>S_tfthread *thread = (s_tfthread *) malloc (sizeof (s_tfthread));</p></p><p><p></p></p><p><p>Thread->tftask = task;</p></p><p><p></p></p><p><p>Thread->name = k_tfthread_names[i];</p></p><p><p></p></p><p><p>k_threads[i]=thread;</p></p><p><p></p></p><p><p>ret = pthread_create (&posix_t_id, &attr, tfthreadpool_task_run,task);</p></p><p><p></p></p>2. After the loop is created, there will be n suspended threads in the program, and when a new task needs to be executed, the lookup will query out the idle thread in k_threads based on the different task flags and create a new s_tftask_content join S_ Tfthread the task list to resume the task by waking the thread through Pthread_cond_signal.<p><p>high-performance iOS programming path-pthread-based thread pool</p></p></span>

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.