【问题标题】:pthread_create and socket descriptor were not synchronizedpthread_create 和套接字描述符不同步
【发布时间】:2012-08-20 08:01:21
【问题描述】:
 main() {
    unsigned int newfd;
    ...
    .....
    while (1) {        
      printf("Waiting for connection\n");
      addrlen = sizeof (clientaddr);
      if ((newfd = accept(listener, (struct sockaddr *) &clientaddr, &addrlen)) < 0) {
          perror("Server-accept() error lol!");
          break;
      }
      printf("New connection from %s on socket %u\n", inet_ntoa(clientaddr.sin_addr), newfd);
      pthread_create(&threads[i++], NULL, (void*)fileTransfer_Worker, &newfd); 
      sleep(1);
   }

}

void* fileTransfer_Worker(void *desc) {
     unsigned int sock = *(unsigned int *) desc;
     printf("Waiting for data in sock %d %u\n", sock, pthread_self());
}

输出

Waiting for connection
New connection from 192.168.4.57 on socket 4
Waiting for connection
New connection from 192.168.4.57 on socket 5
Waiting for connection
Waiting for data in sock 4 3076578160
Waiting for data in sock 5 3068189552
New connection from 192.168.4.57 on socket 6
Waiting for connection
Waiting for data in sock 6 3059800944
New connection from 192.168.4.57 on socket 7
Waiting for connection
New connection from 192.168.4.57 on socket 8
Waiting for connection
New connection from 192.168.4.57 on socket 9
Waiting for connection
Waiting for data in sock 8 3051412336
New connection from 192.168.4.57 on socket 10
Waiting for data in sock 9 3034635120
Waiting for data in sock 10 3043023728
Waiting for connection
New connection from 192.168.4.57 on socket 11
Waiting for connection
New connection from 192.168.4.57 on socket 12
Waiting for connection
New connection from 192.168.4.57 on socket 13
Waiting for connection
Waiting for data in sock 13 3001080688
Waiting for data in sock 13 3026246512
Waiting for data in sock 13 3017857904
Waiting for data in sock 13 3009469296

如果查看输出,您会注意到 sock 13 显示了 4 次,实际上应该是套接字 7、11、12 和 13。

每次我同时调用更多客户端连接时,行为都会发生变化。

如果我在 pthread_create() 之后添加 sleep(1),那么我就能看到预期的行为。 pthread_create 之后的睡眠是必须的吗?或者如何在不使用睡眠的情况下解决此问题?

帮我解决这个问题。提前致谢

【问题讨论】:

    标签: c sockets pthreads


    【解决方案1】:
    pthread_create(&threads[i++], NULL, (void*)fileTransfer_Worker, &newfd);
                                                                    ^^^^^^
    

    问题是您将局部变量的地址传递给pthread_create。当线程开始使用它时,main 可能已经改变了它。所以如果当你调用pthread_create的时候描述符是10,那么当线程真正开始使用main的时候已经有机会改变它了。

    您至少有 2 个选项可以解决此问题:

    • 为每个线程传递一个单独的对象(可能是你malloc
    • 在参数中填充整数,就好像它是void *,然后在线程函数中转换回int

    虽然第二个选项被广泛使用,但它也是不可移植的。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 2011-03-31
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多