wxFile Read doesn't block [solved]
Posted: Tue Mar 08, 2016 5:11 pm
On windows, I'm trying to simulate a Linux named pipe. But instead of blocking on the read from an empty file, the application is saturating one core of the CPU furiously reading NULL from the empty file (drive light lights up brightly too). I determined this with gdb. My research has not revealed how to set the file read mode to blocking. I'd rather not use sockets because of the need to determine a safe port number.
My code is here.
My code is here.
Code: Select all
#include <cstdlib>
#include <iostream>
#include <wx/stdpaths.h>
#include <wx/datetime.h>
#include <wx/string.h>
#include <wx/init.h>
#include <wx/utils.h>
#include <wx/file.h>
#include "Database.h"
#ifdef __WXMSW__
#define PIPENAME wxT("c:\\fakepipe.txt")
#else
#define PIPENAME "/opt/oregano/daemonpipe"
#endif
int main(int argc, char *argv[])
{
wxInitializer ini;
//create the pipe
wxFile file(PIPENAME);
#ifndef __WXMSW__
if (wxExecute("mkfifo "PIPENAME,wxEXEC_SYNC))
#else
if (file.Create(PIPENAME))
#endif
{
exit(wxSysErrorCode());
}
if (!file.Open(PIPENAME))
{
exit(wxSysErrorCode());
}
// initializedb
theDB = new Database();
#ifdef __WXMSW__
wxString DBPath=wxStandardPaths::Get().GetUserDataDir()+"\\tvguide.db";
#else
wxString DBPath=wxStandardPaths::Get().GetUserDataDir()+"/tvguide.db";
#endif
if (!(theDB->OpenDataBase(DBPath)))
{
wxMessageBox("FATAL ERROR! Cannot Open or Create "+DBPath);
exit(wxSysErrorCode());
}
theDB->checkdatabase();
for(;;) // Main loop
{
// check databse and adjust next recording accordingly
//TODO
//wait for a message from the pipe
for(;;)
{
char c;
file.Read(&c,1); // should block here but doesn't
if (c=='u') break; //
else if (c=='s')
{
//close db and exit
theDB->Close();
exit(0);
}
}
}
}