Comments (3)
The same as #648
from socketrocket.
Have you solved this problem
from socketrocket.
//
// 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);
} - Mutex type attributes
- (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;
}
from socketrocket.
Related Issues (20)
- Seeing the app crash due to crash at com.facebook.SocketRocket.NetworkThread
- SRWebSocket.m give Hang risk warning in Xcode 14 HOT 5
- Memory Leak HOT 1
- Deployment Targets for Cocoapod
- EXC_BREAKPOINT pod '0.6.0' HOT 1
- is 0.6.1 available in pods? HOT 2
- Libraries collision in iOS, React Native and AWS Iot
- Open Error HOT 3
- Cannot connect with subprotocol `graphql-transport-ws`
- How to set or enforce TLS 1.3 version?
- Q: Support L4S udp socket?
- Add SocketRocket.podspec of 0.7.0 version to https://github.com/CocoaPods/Specs.git HOT 3
- Privacy Manifest HOT 3
- 'OSSpinLock' is deprecated: first deprecated in iOS 10.0 - Use os_unfair_lock() from <os/lock.h> instead
- -[AVCaptureSession startRunning] startRunning may not be called between calls to beginConfiguration and commitConfiguration
- Thread Performance Checker Warning HOT 1
- Security issues
- https://www.facebook.com/ghkfyfnvhoh.gmail.com.2024?mibextid=ZbWKwL
- Tiu
- Hash Generated Using Broken Cryptography API (SHA1) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from socketrocket.