Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c performSelectorInBackground显示在>;1线_Objective C_Multithreading_Sockets - Fatal编程技术网

Objective c performSelectorInBackground显示在>;1线

Objective c performSelectorInBackground显示在>;1线,objective-c,multithreading,sockets,Objective C,Multithreading,Sockets,当我尝试分派单个线程轮询unix套接字时,我注意到一些奇怪的行为。 当我在osx 10.6.8上测试代码时,代码崩溃,我看到不止一个线程在运行代码。 奇怪的是,在osx 10.7.5上,当它崩溃时,我只看到它在一个线程上运行 在AppDelegate中,我按如下方式分派线程(一次): pp是我的IPC类的一个实例 在我的IPC类中,runloop是一些C代码的包装器 @implementation IPC -(void) runloop { ipc_busy = 0; unixsocke

当我尝试分派单个线程轮询unix套接字时,我注意到一些奇怪的行为。 当我在osx 10.6.8上测试代码时,代码崩溃,我看到不止一个线程在运行代码。 奇怪的是,在osx 10.7.5上,当它崩溃时,我只看到它在一个线程上运行

在AppDelegate中,我按如下方式分派线程(一次):

pp是我的IPC类的一个实例

在我的IPC类中,runloop是一些C代码的包装器

@implementation IPC
-(void) runloop
{
  ipc_busy = 0;
  unixsocket_runloop();
}
runloop用于等待单个连接(每个运行时) 然后处理程序等待数据包,直到程序终止。 C代码如下所示-

/**
 * Waits for a connection and dispatches the socket handler
 * No need to fork since we will only ever have a single connection per program launch
 */
int unixsocket_runloop()
{
  int connection_fd;
  socklen_t address_length;
  while((connection_fd = accept(socket_fd,
                                (struct sockaddr *) &address,
                                &address_length)) > -1)
  {
    return unixsocket_handler(connection_fd);
  }
  return 0; // for posterity
}
这是连接处理程序-

/**
 * Handles the connection from the client
 * loops infinitely and reads from the socket when there is a message
 * Stores message into a ring buffer of buffers
 */
int unixsocket_handler(int connection_fd)
{
  size_t nbytes;
  char readbuffer[USBUFSIZE];
  bzero(readbuffer,USBUFSIZE);
  while((nbytes = read(connection_fd, readbuffer, USBUFSIZE)))
  {
    // store in buffer
    bufPut(readbuffer,nbytes);
    bzero(readbuffer,USBUFSIZE);
  }
  close(connection_fd);
  return 0;
}
在我的堆栈跟踪中,我看到这个unixsocket_运行循环发生在两个线程中。 虽然我不认为这是撞车的根本原因, 这是我想要解决的意外行为


我使用自己的socket类的唯一原因是b/c,我在开源社区看到的许多选项都是硬连接到TCP的。特别是吸引人的cocoaAsyncSocket类

第一个问题是:
unixsocket\u runloop
的两个实例是否都将
-[AppDelegate runloop]
作为堆栈中的下一个对象?如果跟踪堆栈,看看是什么导致了这种情况。如果不是(假设堆栈上的任何东西都不可能调用
unixsocket\u runloop
),则很可能是堆栈损坏和/或错误符号,所以你找错了方向。运行循环的两个线程中的每一个堆栈似乎都是由thread\u start->pthread\u start->\u NSThread\u main->unixsocket\u runloop调用的。堆栈损坏是一个很好的开始寻找的方向,因为10.7相关的崩溃是在一个stk\u chk\u失败后才开始的。谢谢你的建议,阿巴内特。
/**
 * Handles the connection from the client
 * loops infinitely and reads from the socket when there is a message
 * Stores message into a ring buffer of buffers
 */
int unixsocket_handler(int connection_fd)
{
  size_t nbytes;
  char readbuffer[USBUFSIZE];
  bzero(readbuffer,USBUFSIZE);
  while((nbytes = read(connection_fd, readbuffer, USBUFSIZE)))
  {
    // store in buffer
    bufPut(readbuffer,nbytes);
    bzero(readbuffer,USBUFSIZE);
  }
  close(connection_fd);
  return 0;
}