3

I've got the following code and I'm attempting to add threading so it executes faster but I'm really stumped. I basically want different threads to execute different iterations of the for loop to speed up the process since I'll be pinging a lot of urls. However, I wasn't sure how to do that or if it was possible so I did what is seen below but I get an error at "thread doStuff(exec" right at the exec part that says there is no instance of constructor "std::thread::thread" matches the argument list. How can I apply threading to this situation when the value of the parameter for the "exec" function is changing at every iteration of the for loop?

On a side note, if you couldn't tell, I need to put the result of the "exec" function into index i of the vector "work".

EDIT: The last function is supposed to look incomplete because I deleted code that was irrelevant to this problem.

vector<string> check(vector<string> url)
{

    vector<string> work;
    for (int i = 0; i < url.size(); i++)
    {
        string ping = "ping ";
        ping.append(url[i]);
        thread doStuff(exec,work.push_back(exec(ping.c_str())));

        cout << work[i] << endl;
    }

    return work;
}
std::string exec(const char* cmd) {
    string timeOut = "Request timed out";
    string reply = "Reply from";
    std::array<char, 128> buffer;
    std::string result;
    std::shared_ptr<FILE> pipe(_popen(cmd, "r"), _pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    while (!feof(pipe.get())) {
        if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
            result += buffer.data();
    }
return result;
2
  • Threading is a wasps nest of potential (and hard to find) bugs if you don't know what you are doing (in most cases; expert only territory). Step 1) learn C++ really well. Step 2) learn about synchronization, priority inversion, atomic operations, deadlocking and much more. Step 3) attempt threads. Commented Jun 19, 2018 at 15:51
  • 4
    @JesperJuhl, Re, "learn C++ really well." And just sweep out King Augeas' stables on your way. Learning all of C++ really well is a monumental task. One could be qualified to undertake the study of threads after learning a subset of C++ really well. E.g., One need not know how to design a template library in order to start learning about threads. Commented Jun 19, 2018 at 16:53

2 Answers 2

3

Since you're using Visual Studio, you might want to consider parallel_for. Also, C++17 introduced the Parallelism TS, which is a work in progress attempt at standardizing parallel algorithms. This blog post gives a nice introduction to what features are available, and how you can try them out today.

Sign up to request clarification or add additional context in comments.

9 Comments

I just tried using parallel_for but when it hits ping.append(domain[i]) it triggers a breakpoint in dbgrptt.cpp.
Hmm, I'm not sure what caused that breakpoint, but looking at your code, I suspect you will have another problem with using parallel_for with your current implementation. You modify the work vector inside the for loop. If you changed that to a parallel_for loop, then you would have multiple threads modifying the same vector. This would need to be synchronized (either with a mutex or a thread-safe vector).
Can you provide more details about the breakpoint? I imagine there's a backtrace?
Oh I see. I wish I could give you more details about the breakpoint but it refuses to tell me because it can't find dbgrptt.cpp. However, I tested to make sure it was a problem with accessing the vector by putting string hi = domain[i] and that caused the same breakpoint. How exactly would I go about adding in the mutex?
Weird...it says the size of the url vector is 0...I'm not sure why I didn't catch that before.
|
2

You can use std::async in for loop

auto handle = std::async(std::launch::async,exec,ping.c_str());
work.push_back(handle.get());

That should work.

3 Comments

I get no compilation errors with this but it seems to be running no faster than before.
Your program with std::thread (no return) is running faster than before?
Well I was never able to get std::thread to work but when I use std::async it runs at the same speed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.