PDA

View Full Version : multithreading simple question



qtlearner123
31st January 2012, 08:16
Hi all,

As the forum name suggests i am a newbie to qt programming and this the first time i am posting and hope to get some reply. i have one of these tasks to perform i have one PCI based card with some digital inputs and outputs i have to read the status of those inputs continuously (safely i can read at 100ms without loss of data the lesser it is still better). For this task the supplier has provided a function waitForIRQ() and explains that it would deadlock the thread and returns non zero if some irq happened and returns 0 if some error happened. Now i am trying to write the program in qt and created a thread like this


int main(int argc, char *argv[])
{
QApplication app(argc, argv);

my_thread thread;
thread.start();
// doing some functionality in the main
return app.exec();
}

void my_thread::run()
{
while( waitForIRQ())
return value;

}

i am sorry i have just started QT and C++. please let me know if i have written the code properly. sorry for the poor explanation.

please let me know if i did not explain clearly.

thanks and regards,
satya

wysota
31st January 2012, 10:41
It's more or less okay (provided you do assign some values to "value") however it would be better to do it without having to block the thread. Does the supplier provide any non-blocking API as well?

qtlearner123
31st January 2012, 11:10
Thank you for the reply. i will try to execute the program and see if i could run the program successfully. one thing is i did not understand this blocking and non blocking api you have mentioned i did not think of that. if you don't mind could you please explain me. i will also try google to understand.

thanks and regards,
satya

wysota
31st January 2012, 13:15
Blocking API is what you called "deadlock" (it's not actually a deadlock because a deadlock situation is that something is waiting for an event that will never happen) which means that when you call a function, it blocks execution of the program until there is data available. Non-blocking API on the other hand would return immediately telling you there is no data to be read so that you can attend other tasks before data is available. This is usually accompanied by some mechanism (like a unix signal, interrrupt, socket listener) that will notify you that data is available and you can read it.

qtlearner123
2nd February 2012, 06:29
Hi,

i still have some problems of basic understanding of the threading currently i am showing the pseudo code of what i have written

/* main.h */

class test:public QObject
{

Q_Object

public:
readinputstatuschangeLedColourtoGreen();
maketheflagTrue();

}


class mythread:public Thread
{
Q_Object

public:
mythread();

protected:
void run();

public:
int my_flag;

public slots:
void maketheflagTrue();
}



/* main.cpp*/

int main(int argc, char *argv[])
{
QApplication app(argc,argv);

Window window;


mythread threadA;

threadA.start();

window.show();

return app.exec();

}


/* file1.cpp */

test test1; // created object of test

window::Window
{

// created a gui with all the switches and Leds (switches are normal push button switches and leds are
// normal circles where you can fill with either green or red colour depending on the input data from
// pci based input card.

QPushButton *button1 = new QPushButton("BUTTON1");

QObject::connection(button1, SIGNAL(clicked()), &test1, SLOT(maketheflagTrue()));

}

/* main.cpp*/

mythread::mythread()
{
my_flag = FALSE;
}

mythread::run()
{
while(!myflag)
{
}
readinputstatuschangeLedColourtoGreen();
my_flag = FALSE;
}


/*file3.c*/

extern test test1;

void test:: readinputstatuschangeLedColourtoGreen()
{
// based on the input data i would make the leds green or red if input is 1 or 0 respectively.
}

void test::maketheflagTrue()
{
test1.my_flag = TRUE;
}

i will very quickly and briefly explain the code
1. i have to actually get the data from the pci card which currently i am trying to simulate with button1 and the flag i am using my_flag.
2. my main purpose is read some input data and color the led which i am trying to do it in thread.

Questions:

1. i have started the thread by making the my_flag variable FALSE. so once the thread is started will the program hang in the thread itself as per the following code

while(!myflag)
{
}
or it will be in the main thread and constantly looking for the my_flag variable change using scheduling mechanism. i really don't understand this. (i observed that the code does not hang).

2. By clicking the button i have made my_flag TRUE so in the thread it should go to the function readinputstatuschangeLedColourtoGreen execute it, after executing the my_flag is made FALSE. will the program automatically go to the thread or i need to start again. if i have to start the thread where and how to start the thread.

Sorry for the lengthy mail but please let me know if you did not understand. Your reply would help me to gain little confidence on multithreading.

thanks in advance,
regards,
satya

wysota
2nd February 2012, 09:25
I think you should really read a bit about threading (http://en.wikipedia.org/wiki/Thread_(computing)). It seems you lack basic understanding of what it does.

qtlearner123
3rd February 2012, 03:11
Hi,

Thank you for the link. it was very useful.

regards,
satya