C++ 线程被ACE\U SOCK\U流的函数send()或recv()暂停?
我正在使用pthread+ACE编写一个假客户端 该客户端有3个线程,每个线程可以使用ACE无休止地发送和接收消息。但是,这些线程总是被函数send()或recv()停止。也就是说,如果发送或接收出现错误,线程将退出,不幸的是,我不知道错误是什么,我也无法捕获它。代码为:C++ 线程被ACE\U SOCK\U流的函数send()或recv()暂停?,c++,multithreading,ace,C++,Multithreading,Ace,我正在使用pthread+ACE编写一个假客户端 该客户端有3个线程,每个线程可以使用ACE无休止地发送和接收消息。但是,这些线程总是被函数send()或recv()停止。也就是说,如果发送或接收出现错误,线程将退出,不幸的是,我不知道错误是什么,我也无法捕获它。代码为: struct thread_data { int thread_id; string ip; uint32_t port; uint32_t timeout; }; std::vector&l
struct thread_data {
int thread_id;
string ip;
uint32_t port;
uint32_t timeout;
};
std::vector<struct thread_data> m_thread;
void * test_fun1(void * threadid)
{
struct thread_data * tmp_thread_data = (struct thread_data *)threadid;
long tmp_threadid = (long)tmp_thread_data->thread_id;
string tmp_ip = tmp_thread_data->ip;
uint32_t tmp_port = tmp_thread_data->port;
uint32_t tmp_timeout = tmp_thread_data->timeout;
ACE_INET_Addr addr(tmp_port, tmp_ip.c_str());
ACE_Time_Value timeout(0, tmp_timeout * 1000);
ACE_SOCK_Connector connector;
ACE_SOCK_Stream peer;
// connect
if(connector.connect(peer, addr, &timeout) != 0)
pthread_exit((void *) threadid);
// send msg
while (1)
{
ssize_t tmp_ret1 = peer.send("hello world", 12);
if (tmp_ret1 <= 0)
continue;
char tmp_buf[1024] = '\0';
ssize_t tmp_ret2 = peer.recv(tmp_buf, 1024, &timeout);
if (tmp_ret2 <= 0)
continue;
else
fprintf(stderr, "recv:%s\n", tmp_buf);
}
// close
peer.close();
pthread_exit((void *) threadid);
}
int main(int argc, char *argv[])
{
std::vector<pthread_t> threads;
pthread_attr_t attr;
int rc;
int i = 0;
void * status;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// thread create
int tmp_num = 3;
for(i = 0; i < tmp_num; i++)
{
pthread_t tmp_thread_handler;
struct thread_data tmp_thread_info;
tmp_thread_info.thread_id = i;
tmp_thread_info.ip = "127.0.0.1";
tmp_thread_info.port = 8001;
tmp_thread_info.timeout = 100;
rc = pthread_create(&tmp_thread_handler, NULL, test_fun1, (void *)&tmp_thread_info);
if (rc != 0)
return -1;
threads.push_back(tmp_thread_handler);
m_thread.push_back(tmp_thread_info);
}
// thread start
pthread_attr_destroy(&attr);
for(i = 0; i < tmp_num; i++)
{
rc = pthread_join(threads[i], &status);
if (rc != 0)
return -1;
}
pthread_exit(NULL);
return 0;
}
struct-thread\u数据{
int-thread_-id;
字符串ip;
uint32_t端口;
uint32_t超时;
};
std::向量m_线程;
void*test\u fun1(void*threadid)
{
结构线程数据*tmp线程数据=(结构线程数据*)线程ID;
长tmp_线程id=(长)tmp_线程数据->线程id;
字符串tmp_ip=tmp_线程数据->ip;
uint32\u t tmp\u端口=tmp\u线程\u数据->端口;
uint32\u t tmp\u超时=tmp\u线程\u数据->超时;
ACE_INET_Addr Addr(tmp_端口,tmp_ip.c_str());
ACE_时间_值超时(0,tmp_超时*1000);
ACE_插座连接器;
ACE_SOCK_流对等;
//连接
if(连接器连接(对等、地址和超时)!=0)
pthread_exit((void*)threadid);
//发送消息
而(1)
{
ssize_t tmp_ret1=peer.send(“hello world”,12);
如果(tmp_ret1您在这段代码中有一个重要的作用域生存期问题。您正在向线程发送一个数据块,该数据块是在每次循环迭代中创建/销毁的。省去边线:
// thread create
int tmp_num = 3;
for(i = 0; i < tmp_num; i++)
{
pthread_t tmp_thread_handler;
struct thread_data tmp_thread_info;
// --- snip ----
rc = pthread_create(&tmp_thread_handler, NULL, test_fun1, (void *)&tmp_thread_info);
// --- snip ----
}
关于其他问题,在线程进程中,这不会编译:
char tmp_buf[1024] = '\0';
但是,与您的未定义行为相比,这是非常小的问题。在这段代码中,您有一个重要的作用域生存期问题。您正在向线程发送一个数据块,该数据块在每次循环迭代中创建/销毁。省去了边线:
// thread create
int tmp_num = 3;
for(i = 0; i < tmp_num; i++)
{
pthread_t tmp_thread_handler;
struct thread_data tmp_thread_info;
// --- snip ----
rc = pthread_create(&tmp_thread_handler, NULL, test_fun1, (void *)&tmp_thread_info);
// --- snip ----
}
关于其他问题,在线程进程中,这不会编译:
char tmp_buf[1024] = '\0';
但与您的未定义行为相比,这是非常小的。您不知道错误是什么的唯一原因是因为您没有通过errno或strerror或perror()打印错误。这只是您代码中的一个错误,不需要就此提出问题。当然,如果出现错误,您应该退出循环:连接几乎肯定已断开。如果获得EOS(recv()返回零),它已损坏。谢谢,我可以打印错误信息,并且我已经修复了它。您不知道错误是什么的唯一原因是因为您没有通过errno或strerror或perror()打印错误信息。这只是您代码中的一个错误,不需要就此提出问题。当然,如果您遇到错误,您应该退出循环:连接几乎肯定已断开。如果您得到EOS(recv()返回零),它已断开。谢谢,我可以打印错误信息,并且我已经修复了它。