[英]Why isn´t the for loop accessed in release mode, but in debug it works fine
my problem is that I coded server/client application on windows.我的问题是我在 Windows 上编写了服务器/客户端应用程序。 Now in debug mode everything works fine and as it should , but in release mode the server doesn´t receive or send messages.现在在调试模式下一切正常,但在发布模式下服务器不会接收或发送消息。
I think it is due to that, that the for loop inside the infinite for loop doesn´t get accessed.I also tried to implement this solution with a while but it didn´t work.我认为这是因为无法访问无限 for 循环内的 for 循环。我也尝试用一段时间来实现此解决方案,但没有奏效。 I think it might be the problem that i call a function in the condition field, because when i compare the i to an integer it gets accessed.我认为这可能是我在条件字段中调用函数的问题,因为当我将 i 与整数进行比较时,它会被访问。 Also interesting is that when i std::cout something right before the inner for loop , the loop gets also accessed , despite the fact that I am calling the function in the condition field.同样有趣的是,当我在内部 for 循环之前使用 std::cout 时,循环也会被访问,尽管我在条件字段中调用函数。
#include <iostream>
#include <thread>
#include "server.cpp"
//gets defined in server.cpp
void server::acceptConn() {
u_long mode =1;
for(;;){
int len = sizeof(incAddr[connectedSocks]);
if((inc_sock[connectedSocks] = accept(serv,(struct sockaddr *)&incAddr[connectedSocks],&len))!= SOCKET_ERROR){
std::cout<<"client connected : "<<inet_ntoa(incAddr[connectedSocks].sin_addr)<<std::endl;
ioctlsocket(inc_sock[connectedSocks],FIONBIO,&mode);
connectedSocks++;
}
}
}
int main() {
server ser;
ser.init_Server();
std::thread t(&server::acceptConn,&ser);
char buf[1024];
for(;;){
for(int i=0 ; ser.getCounter()>i;i++){
if (recv(ser.getInc_Sock()[i], buf, sizeof(buf), 0) == SOCKET_ERROR) {
} else{
for (int x = 0; x < ser.getCounter(); x++) {
if(x==i){//just that the message doesnt get send back to the sender}
else{
if (send(ser.getInc_Sock()[x], buf, sizeof(buf), 0) == SOCKET_ERROR) {
std::cout<<"send failed : "<<WSAGetLastError()<<std::endl;
}
}
}
}
}
}
}
int getCounter(){return connectedSocks;};//in server.h
The result should be that Server is having a List of connected socks and is distributing the messages to everyone.结果应该是服务器有一个连接的袜子列表,并将消息分发给每个人。 Now when I am running it in debug mode everything works fine.现在,当我在调试模式下运行它时,一切正常。 What could be the problem?可能是什么问题呢?
Your code lacks any form of synchronization between the two threads.您的代码在两个线程之间缺少任何形式的同步。 The worker thread writes data, and the main thread reads data.工作线程写入数据,主线程读取数据。 But because there is no synchronization between them (mutex/atomics/etc), every read from the main thread is a data race with the writes from the worker thread.但是因为它们之间没有同步(互斥体/原子/等),所以从主线程读取的每次都是与工作线程写入的数据竞争。
And data races are undefined behavior.数据竞争是未定义的行为。 So the compiler is permitted to compile the main thread as if nobody ever writes data .所以编译器被允许编译主线程,就好像没有人写过数据一样。
When you put a std::cout
in your loops, this "works" because std::cout
requires some form of synchronization to keep multiple threads from writing to stdout at the same time.当您将std::cout
放入循环中时,这“有效”,因为std::cout
需要某种形式的同步以防止多个线程同时写入 stdout。 So you're effectively piggybacking off of that internal synchronization.因此,您实际上是在利用内部同步。 But you shouldn't rely on that;但你不应该依赖它; you should instead use proper synchronization primitives.您应该改用适当的同步原语。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.