小编hei*_*ine的帖子

当 pollfd 在另一个线程上更改时,Linux 和 OS X 之间 poll() 的差异

我试图让 libwebsockets 在 OS X 上的多线程环境中运行。我无法触发从与主服务线程不同的线程发送数据。在 libwebsocket 文档上暗示这应该是可能的(演示代码邮件列表)。于是我深入研究代码,发现问题出在 poll() 函数中。

struct pollfd对于作为参数给出的参数,poll() 的行为似乎有所不同。libwebsockets 依赖于在 poll() 处于活动状态时更改 fds.event 字段的可能性。这在 Linux 上运行良好,但在 OS X 上不起作用。

我编写了一个小测试程序来演示该行为:

#include <unistd.h>
#include <netdb.h>
#include <poll.h>
#include <iostream>
#include <thread>

#define PORT "3490"

struct pollfd    fds[1];
bool connected = false;

void main_loop() {
    int sockfd, new_fd; 
    struct addrinfo hints, *servinfo, *p;
    socklen_t sin_size;
    int yes=1;
    char s[INET6_ADDRSTRLEN];
    int rv;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = …
Run Code Online (Sandbox Code Playgroud)

sockets linux macos multithreading libwebsockets

4
推荐指数
1
解决办法
1377
查看次数

标签 统计

libwebsockets ×1

linux ×1

macos ×1

multithreading ×1

sockets ×1