最近我遇到长连接线程,在执行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; }