Introduction to Multithreading---pthread

Source: Internet
Author: User

1. Introduction

Pthread is part of the POSIX multithreaded development framework. It is a cross-platform multithreaded solution provided by the C language. Because of its in iOS programming, the operation is more troublesome, generally not, here introduced only as understanding.

Use of 2.pthread

Create a pthread with the following function, which is usually _t/ref at the end of the type in C and does not require the use of *:

const pthread_attr_t * __restrict,        void *(*)(void *), void * __restrict);

Parameters:

 1. pthread_t * __restrict 线程代号的地址 2. onst pthread_attr_t * __restrict 线程的属性 3. 调用函数的指针    - void *(*)(void *)    - 返回值 (函数指针)(参数)    - void * 和 OC 中的 id 是等价的 4. void * __restrict :传递给该函数的参数

return value:

    • If it's 0, that's correct.
    • If it is non-0, it indicates an error code
3. Code Combat:
#import "ViewController.h" #import <pthread.h>  @interface viewcontroller ()@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];NSString*STR = @"MR";    pthread_t thread; Pthread_create (&thread,NULL, &demo, (__bridgevoid*) (str));void* Demo (void* params) {NSString*str = (__bridgeNSString*) (params); for(inti =0; I < -; i++) {NSLog(@"%@", str); }return NULL;}@end

__bridge (bridge Connection)

    1. In ARC development, if you are designing to convert to the same data type in C, you need to use the __bridge "bridge"
    2. In OC, if ARC is developed, the compiler will automatically add retain, release, and autorelease to the code structure at compile time.
    3. ARC is only responsible for the OC part of the code, not responsible for C code, if the C language frame appears retain/create/copy word function, all need to release

Run Results

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction to Multithreading---pthread

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.