获取线程号和杀不死的NSThread线程

简介: 获取线程号和杀不死的NSThread线程

最近我遇到长连接线程,在执行connect闪断导致永远阻塞线程,结果我想杀死这个阻塞的线程,用了九牛二之力也杀不死。希望高手指点一二。我用了伟大的kill -9 pid仍旧干不死它,不甘心啊!

估计是当线程在执行sleep时,不能被杀死。当线程处于wait状态才能被杀死。

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[NSThread currentThread] setName:@"socket thread"];
        _socketThread = [NSThread currentThread];
//        _socketThread.description;
//        [[NSThread currentThread] start];
//        while(!_socketThread.is())
//        {
//            
//        }
//        _socketThread.stop();
        NSString * socketThreadDescription = [NSThread currentThread].description;
        NSRange range = [socketThreadDescription rangeOfString:@"number = "];
        NSRange range1 = [socketThreadDescription rangeOfString:@","];
        if((range.length <= 0) || ((range1.length <= 0)))
        {
            _socketThreadSno = -1;
        }
        else
        {
            range = NSMakeRange (range.location + range.length, range1.location - range.location - range.length);
            NSString *number = [socketThreadDescription substringWithRange : range];
            _socketThreadSno = [number longLongValue];
        }
    }

下面这个普通的NSThread线程获取线程号和上面那个略有不同
-(void)socketNSThread
{
    [[NSThread currentThread] setName:@"socket thread"];
    _socketThread = [NSThread currentThread];
    //        _socketThread.description;
    //        [[NSThread currentThread] start];
    //        while(!_socketThread.is())
    //        {
    //
    //        }
    //        _socketThread.stop();
    NSString * socketThreadDescription = [NSThread currentThread].description;
//    NSRange range = [socketThreadDescription rangeOfString:@"number = "];
//    NSRange range1 = [socketThreadDescription rangeOfString:@","];
//    if((range.length <= 0) || ((range1.length <= 0)))
//    {
//        _socketThreadSno = -1;
//    }
//    else
//    {
//        range = NSMakeRange (range.location + range.length, range1.location - range.location - range.length);
//        NSString *number = [socketThreadDescription substringWithRange : range];
//        _socketThreadSno = [number longLongValue];
//    }
    NSRange range = [socketThreadDescription rangeOfString:@"num = "];
    NSRange range1 = [socketThreadDescription rangeOfString:@"}"];
    if((range.length <= 0) || ((range1.length <= 0)))
    {
        _socketThreadSno = -1;
    }
    else
    {
        range = NSMakeRange (range.location + range.length, range1.location - range.location - range.length);
        NSString *number = [socketThreadDescription substringWithRange : range];
        _socketThreadSno = [number longLongValue];
    }
    //        kill(6, SIGABRT);
    FLDDLogDebug(@"socket thread");
}


试过杀线程的方法如下,但都以失败告终,就是我关闭那个连接的socket都引起不了connect的响应。让人无语了!


//   下线
- (void)OffLine
{
    FLDDLogDebug(@"函数");
    FLDDLogDebug(@"offLine");
    long long sno = [SingleAsyncSocket sharedInstance].socketThreadSno;
    if(-1 != sno)
    {
//        [[SingleAsyncSocket sharedInstance] endSocket];
         kill((pid_t)sno, SIGKILL);
//        pthread_kill((__bridge pthread_t)([SingleAsyncSocket sharedInstance].socketThread), SIGQUIT);
        [[SingleAsyncSocket sharedInstance].socketThread cancel];
    }

    return;
}
目录
相关文章
|
安全 测试技术 调度
iOS开发多线程篇-NSThread
上篇我们学习了iOS多线程解决方式中的NSOperation,这篇我主要概况总结iOS多线程中NSThread的解决方式和基本用例
|
iOS开发
iOS多线程编程之一——NSThread线程管理
iOS多线程编程之一——NSThread线程管理
142 0
|
程序员 iOS开发 C语言
iOS多线程开发系列之(一)NSThread
什么是多线程? 一个进程的开始阶段系统会分配一个主线程进行正常的工作,工作过程中是依次执行的,只有这个任务执行完毕的时候才能执行下一个任务,两个或多个任务不能同时执行。这样的工作效率是低效的,因此就引入了多线程,能够利用iPhone设备多核的优势同时并发的处理多个事件。
934 0