我试图让 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)