I was using threads for timing out messages.
But apparently everyone knows you can just use select(2)
if (wait <= 0)
return;
- TimerThread timer(wait*1000000); //Wait in seconds
- timer.Start();
+
+
#ifdef BUILD_GRAPHICS
+
+
if (!graphicsEnabled)
{
- while (!timer.Finished());
- timer.Stop();
+ usleep(1000000*wait); //Wait in seconds
return;
}
- #endif //BUILD_GRAPHICS
+ TimerThread timer(wait*1000000); //Wait in seconds
+ timer.Start();
while (!timer.Finished())
{
- #ifdef BUILD_GRAPHICS
+
SDL_Event event;
while (SDL_PollEvent(&event))
{
break;
}
}
- #endif //BUILD_GRAPHICS
}
timer.Stop();
+
+ #else
+ usleep(wait*1000000); //Wait in seconds
+ #endif //BUILD_GRAPHICS
}
setbuf(file, NULL);
}
+ struct timeval tv;
+ fd_set readfds;
+
+ tv.tv_sec = (int)(timeout);
+ tv.tv_usec = (timeout - (double)((int)timeout)) * 1000000;
+
+ FD_ZERO(&readfds);
+ FD_SET(sfd, &readfds);
+
+ select(sfd+1, &readfds, NULL, NULL, &tv);
+
+ if (!FD_ISSET(sfd, &readfds))
+ return false; //Timed out
+ //fprintf(stderr, "Got message!\n");
+ for (char c = fgetc(file); c != '\n' && (int)(c) != EOF; c = fgetc(file))
+ {
+ //fprintf(stderr, "%c", c);
+ buffer += c;
+ }
+ //fprintf(stderr, "%s\n", buffer.c_str());
+ return true;
+
+
+ /* Old way, which is apparently terrible
+
assert(&buffer != NULL);
GetterThread getterThread(file, buffer);
assert(&(getterThread.buffer) != NULL);
if (buffer.size() == 1 && buffer[0] == EOF)
return false;
return true;
+ */
}
#include <string.h>
#include <stdio.h>
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+
using namespace std;
char ** arguments = NULL;
if (args.size() > 0)
{
- arguments = new char*[args.size()+2];
+ arguments = new char*[args.size()];
for (unsigned int i=0; i < args.size(); ++i)
arguments[i] = args[i];
}
if (!Running() || timeout == 0)
return false;
+ struct timeval tv;
+ fd_set readfds;
+
+ tv.tv_sec = (int)(timeout);
+ tv.tv_usec = (timeout - (double)((int)timeout)) * 1000000;
+
+ int fd = fileno(input);
+
+ FD_ZERO(&readfds);
+ FD_SET(fd, &readfds);
+
+ select(fd+1, &readfds, NULL, NULL, &tv);
+
+ if (!FD_ISSET(fd, &readfds))
+ return false; //Timed out
+ //fprintf(stderr, "Got message!\n");
+ for (char c = fgetc(input); c != '\n' && (int)(c) != EOF; c = fgetc(input))
+ {
+ //fprintf(stderr, "%c", c);
+ buffer += c;
+ }
+ //fprintf(stderr, "%s\n", buffer.c_str());
+ return true;
+
+ /* Old way, using threads, which apparently is terrible
assert(&buffer != NULL);
GetterThread getterThread(input, buffer);
assert(&(getterThread.buffer) != NULL);
if (buffer.size() == 1 && buffer[0] == EOF)
return false;
return true;
-
+ */
}