So I have got some code to compile, but I'm having some problems. Here is what I'm doing to give you an idea of what's going on with my code:
- I have C code with which I create a thread to run the GUI in
- In the GUI code I create a QThread which provides me entry points to write data to and from.
I can compile it all and run it, but it seems as though the GUI Thread is blocking my Main thread. The graphics show up, but everything else in my main C code is not executing.
My GUI code:
//globals
idThread *idthread;
MainWindow *window;
int hpgeUI(char *argv[])
//int main(int argc, char *argv[])
{
idthread = new idThread;
window = new MainWindow;
window->showFullScreen(); //Run in full screen mode
window
->setPalette
(QPalette(QColor(233,
233,
235)));
//Set palette color to off white window->setAutoFillBackground(true); //Fill background w/ palette color
window->show();//Show window
rdac->connect(idthread, SIGNAL(newIdValue(int)), window, SLOT(newIdValue(int)));
//return 0;
return rdac->exec();
}
void updateData(int id)
{
idthread->updateID(id);
}
//globals
QApplication *rdac;
idThread *idthread;
MainWindow *window;
int hpgeUI(char *argv[])
//int main(int argc, char *argv[])
{
rdac = new QApplication(argc, argv, QApplication::GuiServer);
idthread = new idThread;
window = new MainWindow;
window->showFullScreen(); //Run in full screen mode
window->setPalette(QPalette(QColor(233, 233, 235))); //Set palette color to off white
window->setAutoFillBackground(true); //Fill background w/ palette color
window->show();//Show window
rdac->connect(idthread, SIGNAL(newIdValue(int)), window, SLOT(newIdValue(int)));
//return 0;
return rdac->exec();
}
void updateData(int id)
{
idthread->updateID(id);
}
To copy to clipboard, switch view to plain text mode
The QThread to pass data to and from the C code:
void idThread::updateID(int id)
{
/* Lock the mutex so we can grab the data */
/* copy the id to our thread's copy */
this->localID = id;
/* if the thread is not running, start it, else restart the thread. */
if(!isRunning()){
start(LowPriority);
} else {
restart = true;
/* wake up the thread if necessary */
condition.wakeOne();
}
}
void idThread::run()
{
cout << "Running idThread\n";
int id = 0;
forever //Qt psuedo-keyword, like foreach
{
/* Lock the mutex and copy ID to a local copy so that if new ID's come in, we can get them immediately */
mutex.lock();
id = this->localID;
mutex.unlock();
/* if we get an abort for some reason, break from this loop and kill the thread */
if(abort)
return;
/* Emit the new ID unless restart is true */
if(!restart)
emit newIdValue(id);
/* Once we're done with all the iterations, we call QWaitCondition::wait() to put the thread */
/* to sleep by calling, unless restart is true. There's no use in keeping a worker thread looping */
/* indefinitely while there's nothing to do. */
mutex.lock();
if(!restart)
condition.wait(&mutex);
restart = false;
mutex.unlock();
}
}
void idThread::updateID(int id)
{
/* Lock the mutex so we can grab the data */
QMutexLocker locker(&mutex);
/* copy the id to our thread's copy */
this->localID = id;
/* if the thread is not running, start it, else restart the thread. */
if(!isRunning()){
start(LowPriority);
} else {
restart = true;
/* wake up the thread if necessary */
condition.wakeOne();
}
}
void idThread::run()
{
cout << "Running idThread\n";
int id = 0;
forever //Qt psuedo-keyword, like foreach
{
/* Lock the mutex and copy ID to a local copy so that if new ID's come in, we can get them immediately */
mutex.lock();
id = this->localID;
mutex.unlock();
/* if we get an abort for some reason, break from this loop and kill the thread */
if(abort)
return;
/* Emit the new ID unless restart is true */
if(!restart)
emit newIdValue(id);
/* Once we're done with all the iterations, we call QWaitCondition::wait() to put the thread */
/* to sleep by calling, unless restart is true. There's no use in keeping a worker thread looping */
/* indefinitely while there's nothing to do. */
mutex.lock();
if(!restart)
condition.wait(&mutex);
restart = false;
mutex.unlock();
}
}
To copy to clipboard, switch view to plain text mode
My C Code that puts everything together:
void *initUI(char *argv[]) {
printf("This is the initUI thread\n");
hpgeUI(argv);
return 0;
}
int main(int argc, char *argv[])
{
pthread_t ui;
pthread_create(&ui, NULL, initUI(argv), 0);
printf("Done initializing...\n");
int x = 0, test = 0x1;
while(1)
{
x++;
if(x%50 == 0) {
printf("Testing with value: 0x%04x\n", test);
if(test < 0x8)
test++;
else
test = 0x1;
updateData(test);
}
}
return 0;
}
void *initUI(char *argv[]) {
printf("This is the initUI thread\n");
hpgeUI(argv);
return 0;
}
int main(int argc, char *argv[])
{
pthread_t ui;
pthread_create(&ui, NULL, initUI(argv), 0);
printf("Done initializing...\n");
int x = 0, test = 0x1;
while(1)
{
x++;
if(x%50 == 0) {
printf("Testing with value: 0x%04x\n", test);
if(test < 0x8)
test++;
else
test = 0x1;
updateData(test);
}
}
return 0;
}
To copy to clipboard, switch view to plain text mode
Am I going about it all wrong? Again, I appreciate all the help.
Regards,
BB
Bookmarks