我的要求:
背景:
我正在为每条消息创建一个新线程,因为如果我不这样做,消息生成部分将超过发送线程并且消息丢失,因此每条消息的线程似乎是正确的方法
问题:
问题是,以某种方式开始发送zMQ消息的线程没有被终止(不退出/完成)。以下行似乎存在问题:
s_send(*client, request.str());
因为如果我删除它然后线程终止正常,所以可能它的这一行导致问题,我的第一个猜测是线程正在等待响应,但是zmq_PUB是否等待响应?
这是我的代码:
void *SendHello(void *threadid) {
long tid;
tid = (long) threadid;
//cout << "Hello World! Thread ID, " << tid << endl;
std::stringstream request;
//writing the hex as request to be sent to the server
request << tid;
s_send(*client, request.str());
pthread_exit(NULL);
}
int main() {
int sequence = 0;
int NUM_THREADS = 1000;
while (1) {
pthread_t threads[NUM_THREADS];
int rc;
int i;
for (i = 0; i < NUM_THREADS; i++) {
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, SendHello, (void *) i);
pthread_detach(threads[i]);
sched_yield();
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
//usleep(1000);
sleep(1);
}
pthread_exit(NULL);
//delete client;
return 0;
}
我的问题:
我是否需要调整zMQ套接字以便PUB不会等待回复我做错了什么?
编辑:
添加客户端定义:
static zmq::socket_t * s_client_socket(zmq::context_t & context) {
std::cout << "I: connecting to server." << std::endl;
zmq::socket_t * client = new zmq::socket_t(context, ZMQ_SUB);
client->connect("tcp://localhost:5555");
// Configure socket to not wait at close time
int linger = 0;
client->setsockopt(ZMQ_LINGER, &linger, sizeof (linger));
return client;
}
zmq::context_t context(1);
zmq::socket_t * client = s_client_socket(context);
答案 0 :(得分:0)
但是zmq_PUB是否在等待响应?
不,如果您的插座不是PUB插座并且您达到了高水位标记,则可能出现这种情况,但事实并非如此。消息是否被发送?