PDA

View Full Version : Giving reference to QThread



codeman
24th March 2010, 19:50
Hello Friends,

I try today to give a thread a reference from a std::map. The thread have to fill the map but the run finction say that the reference variable are not declared in that scope ..

so the constructor looks like this


using namespace std;
....

MyThread::MyThread(map <int> &p_Map,
QObject* parent): QThread(parent)
{
}

MyThread::run()
{
//using p_Map;
}


and I try to use it in run()

is giving a reference to thread not allowed???


Or what is the best method to fill a map or a vector within a thread????

JohannesMunk
24th March 2010, 23:50
?? You can't access the parameters of one function in another.. You need to store it as a member..



class MyThread : ..
{
public:
MyThread(map <int> &p_Map,QObject* parent): QThread(parent)
{
m_data = p_Map;
}
void run()
{
//use m_data ..;
}
private:
map<int>& m_data;
}

I never tried references in that way.. if it doesn't work just use a pointer to the map.

HIH

Johannes

codeman
25th March 2010, 10:03
The point is after the thread is finished I have to use the filled map. With this approach ( I think so ) I loose my map after thread::finished() or??

The Idea behind this way ( maybe I am unexperienced) I have to fill some data and I don´t want to lock my gui...

JohannesMunk
25th March 2010, 11:33
No you don't loose it. References and Pointers are not deleted when they go out of scope.