ofThread waiting

Hi!
I have a class that inherits from ofThread, to put it simply:

class threadClass: public ofThread{
public:
    int pos;
    string link;
    string path;
    
    int init(int _pos, string l_ink, string _path, int _config);
    void threadedFunction();
    int method1(string _path, string _link,int _id);
    int hmethod2(string _path, int _id);

};

Methods method1 and method2 are used inside threadedFunction(); and those methods use system calls to command line commands that can run in parallel - I have tested that-
Then, I use a vector to store several of them and start them like this. Note that there are arguments passed from the same vector:

vector<videoDownloader*>::iterator it;
    for (int i = 0; i<a;i++){
        threadVector.push_back(new threadClass);
        it = (threadVector.end()-1);
        int sizeV = threadVector.size();
        string link = links[i];
        string p = path;
        (*it)->init(sizeV, link, p, 0);
        // I could have used (*it)->init(threadVector.size(), links[i], path)
       // but since the vectors are the same, I thought the threads may consider it a shared resource and block it...
        (*it)->startThread();
    }

My problem is that the threads, evoenthough they are started as I can see in the XCode debbuger, do not run at the same time. Only one thread -in addition to the main thread- is executed.

I am not having any other problem, but attending to this storing threads in a vector may be a bad idea.
If you have any suggestions, I will be glad to hear them!
Best,
D!

I do store pointers to threaded classes in vectors without problems… I can iterate all the vector’s element and start each one…

Why not :

    for (int i = 0; i<a; i++){
        threadClass* newThread = new ThreadClass();
        threadVector.push_back(newThread);
        newThread->init(threadVector.size(),  links[i], path, 0);
        newThread->startThread();
    }

Thanks tactif!
I do, and everything seems to be ok, but not all the threads are working. I am trying just with 2 and they are sequential.
Do you know what may be happening? Is it posible that they were sharing some resource? How can I find it out? Verbose mode looks like is not working any more…

If you share resources between threads you should use a lock mechanism… But in your case if you put a simple ofLog() << getThreadId(); in the threadedFunction() you should see several logged IDs. You could check that your threads are starting…

Thanks again tactif!
They are sharing resources but just for reading, so that should not be a problem.
As I said, the threads starts, but halt at some point after the first thread finishes…strange…
The main thread does continue perfectly.

It looks like using a system() call inside the thread is not a good idea (link), so that may be the problem.
If I bypass that call, threads behavie like they should
So the question now would be how to make several system calls in different thread :slight_smile:

Hum! Looks like using popen is a good solution :slight_smile: !! link

Interesting to know !!

Here ? http://stackoverflow.com/questions/6962156/is-there-a-way-to-not-wait-for-a-system-command-to-finish-in-c

Thanks tectif, but that may be a problem for some uses, like in my case.
Adding that “&” makes the call to system() -or fork() or whichever- to run on the background.
That means that the execution of the C++/openframeworks program will continue.
If the result of the command executed by system() is needed by the rest of the C++/openframeworks program, then you can not make it run on the background, since the original C++/openframeworks program would not have the infor requiered to continue.

In my case, threads were waiting for another threads to finished, which I could not accept.
But each thread needs for some of its internal processes to end before continuing.

In the case that you are suggesting, each thread will not wait even to their on process.

I am afraid it is a bit of a mess…