How to create multiple threads using QtConcurrent::run()
-
@void Scanner::scan() // using libsmbclient library
{
for(int i=0;i<ipList.length();i++)
{
QtConcurrent::run(this,&Scanner::scanThread,i);
}
}
void Scanner::scanThread(int i)
{
int dh;
QString ip;
ip="smb://"+ipList[i]+"/";
dh= smbc_opendir(ip.toAscii()); // debugger points to this location
if(dh<0)
return;
emit
updateTree(i,dh); // on commenting this line, it still crashes
}
@ -
The library is not thread safe. I tried this:
@void ShareScanner::scanThread(int i)
{
int dh;
QString ip;ip="smb://"+ipList[i]+"/"; mutex.lock(); dh= smbc_opendir(ip.toAscii()); mutex.unlock(); if(dh<0) return; updateTree(i,dh);
}@
It works fine, but only one thread executes at a time. Can you plz explain what you mean by multiple processes. I suppose QProcess is for IPC only!
-
If the library isn't thread safe you can't use it in multiple threads in a single process (at least not concurrently), but you can use it concurrently in different processes, as they all have their instance of the library.
You will have to spawn multiple processes using QProcess, each process scanning a given share and communicating the information gathered back to the application (using some kind of IPC, like stdout, shared memory or sockets).
Be aware that spawing processes doesn't come for free as well, so it might be useful to spawn a bunch of process which then are reused to scan different shares consecutively. But this is up to a performance analysis.
And keep in mind that models aren't thread safe out of the box as well.