GithubHelp home page GithubHelp logo

Comments (3)

Daemonson avatar Daemonson commented on June 3, 2024

The same as #648

from socketrocket.

jihengcong avatar jihengcong commented on June 3, 2024

Have you solved this problem

from socketrocket.

Wxm510846302 avatar Wxm510846302 commented on June 3, 2024

//
// Copyright 2012 Square Inc.
// Portions Copyright (c) 2016-present, Facebook, Inc.
//
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//

#import "SRRunLoopThread.h"
#import <pthread.h>
@interface SRRunLoopThread ()
//{
// dispatch_group_t _waitGroup;
//}

@Property (nonatomic, strong, readwrite) NSRunLoop *runLoop;
@Property (assign, nonatomic) pthread_mutex_t lock;
@Property (assign, nonatomic) pthread_cond_t cond;
@EnD

@implementation SRRunLoopThread

  • (void)dealloc {
    // 不用需要销毁
    pthread_mutex_destroy(&_lock);
    pthread_cond_destroy(&_cond);
    }

  • (void)__initMutex:(pthread_mutex_t *)mutex {
    // #define PTHREAD_MUTEX_INITIALIZER {_PTHREAD_MUTEX_SIG_init, {0}}
    // self.mutex = PTHREAD_MUTEX_INITIALIZER;

    // 静态初始化方法,所以这里不能用这种方式初始化,用下面👇🏻方法初始化
    // 1.初始化属性
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    /*

    • Mutex type attributes
      */
      // #define PTHREAD_MUTEX_NORMAL 0
      // #define PTHREAD_MUTEX_ERRORCHECK 1
      // #define PTHREAD_MUTEX_RECURSIVE 2
      // #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL

    // pthread_mutexattr_settype(&attr, NULL); 传空,默认 PTHREAD_MUTEX_DEFAULT
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);

    // 2.初始化锁
    pthread_mutex_init(mutex, &attr);

    // 3.销毁属性
    pthread_mutexattr_destroy(&attr);

    // 初始化条件
    pthread_cond_init(&_cond, NULL);

    pthread_mutex_lock(mutex);
    }

  • (instancetype)sharedThread
    {
    static SRRunLoopThread *thread;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    thread = [[SRRunLoopThread alloc] init];
    thread.name = @"com.facebook.SocketRocket.NetworkThread";
    [thread start];
    });
    return thread;
    }
  • (instancetype)init
    {
    self = [super init];
    if (self) {
    // _waitGroup = dispatch_group_create();
    // dispatch_group_enter(_waitGroup);
    [self __initMutex:&_lock];
    }
    return self;
    }

  • (void)main
    {

    @autoreleasepool {
    _runLoop = [NSRunLoop currentRunLoop];
    // dispatch_group_leave(_waitGroup);
    // 激活一个等待该条件的线程
    pthread_cond_signal(&_cond);
    NSLog(@"2222");
    // Add an empty run loop source to prevent runloop from spinning.
    CFRunLoopSourceContext sourceCtx = {
    .version = 0,
    .info = NULL,
    .retain = NULL,
    .release = NULL,
    .copyDescription = NULL,
    .equal = NULL,
    .hash = NULL,
    .schedule = NULL,
    .cancel = NULL,
    .perform = NULL
    };
    CFRunLoopSourceRef source = CFRunLoopSourceCreate(NULL, 0, &sourceCtx);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
    CFRelease(source);

      while ([_runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {
          
      }
      assert(NO);
    

    }
    }

  • (NSRunLoop *)runLoop;
    {
    // 等待条件(进入休眠,放开mutex锁;被唤醒后,会再次对mutex加锁)
    pthread_cond_wait(&_cond, &_lock);
    NSLog(@"1111");
    return _runLoop;
    }

@EnD

from socketrocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.