1

When i'm compiling main.cpp i've got these kind of error:

prod_cons.hpp:26:8: note: ‘pile_params::pile_params(const pile_params&)’ is implicitly deleted because the default definition would be ill-formed: struct pile_params{

main.cpp

pile_params pile_analyse(url_racine);
pile_params pile_telechargement(url_racine);
vector_params vect_all_pages();
vector<thread> threads_analyse;
vector<thread> threads_telechargement;

for(int i=0; i<nb_th_get;i++){
    threads_telechargement[i] = thread(telecharger,pile_telechargement,pile_analyse,vect_all_pages);
}

for(int i=0; i<nb_th_analyse;i++){
    threads_telechargement[i] = thread(analyser,profondeur,pile_telechargement,pile_analyse,vect_all_pages);
}

prod_cons.hpp

struct pile_params{
    deque<string> deck;
    stack<string> pile;
    string url_racine;
    condition_variable_any plein;
    condition_variable_any vide;
    mutex mut;
    pile_params(string _url_racine) : 
    pile(deck), url_racine(_url_racine), plein(), vide(), mut(){}
};

struct vector_params{
    vector<page> vect;
    condition_variable_any plein;
    condition_variable_any vide;
    mutex mut;
    vector_params(void) : 
    vect(), plein(), vide(), mut(){}
};

I didn't manage to solve that even by looking to the others topics concerning the error.

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Aïssa-H
  • 97
  • 2
  • 8
  • 1
    Possible duplicate of [Copy Class with std::mutex](http://stackoverflow.com/questions/30340029/copy-class-with-stdmutex) – LogicStuff Apr 02 '16 at 10:18
  • I think that the problem doesn't come from the mutex – Aïssa-H Apr 02 '16 at 10:39
  • 1
    You're mistaken, Aissa. `std::mutex` is non-copyable. A consequence is that a `struct` or `class` that contains one is also not copyable, so a copy constructor would be ill-formed and cannot be generated for it. Try removing both the `mutex` and the `condition_variable` members (both are non-copyable) and you'll find the problem goes away. – Peter Apr 02 '16 at 13:14

3 Answers3

1

In response to the new answer/question, here is a complete, minimal example that demonstrates how to solve the problem.

You can copy/paste this code and compile/run it directly.

#include <iostream>
#include <string>
#include <memory>
#include <mutex>
#include <thread>
#include <functional>
#include <future>
#include <deque>
#include <stack>
#include <vector>
#include <condition_variable>

struct pile_params{
    std::deque<std::string> deck;
    std::stack<std::string> pile;
    std::string url_racine;
    std::condition_variable_any plein;
    std::condition_variable_any vide;
    std::mutex mut;

    pile_params(std::string _url_racine) :
    pile(deck), url_racine(std::move(_url_racine)), plein(), vide(), mut(){}
};

struct page {};

struct vector_params{
    std::vector<page> vect;
    std::condition_variable_any plein;
    std::condition_variable_any vide;
    std::mutex mut;

    vector_params() :
    vect(), plein(), vide(), mut(){}
};

void telecharger(pile_params &a_telecharger, pile_params &a_analyser, vector_params &tlp)
{

}

int main()
{
    auto url_racine = "";

    pile_params pile_analyse(url_racine);
    pile_params pile_telechargement(url_racine);
    vector_params vect_all_pages;
    std::vector<std::thread> threads_analyse;
    std::vector<std::thread> threads_telechargement;

    auto nb_th_get = 100;

    for(int i=0; i<nb_th_get;i++){
        threads_telechargement.emplace_back(telecharger,
                                            std::ref(pile_telechargement),
                                            std::ref(pile_analyse),
                                            std::ref(vect_all_pages));
    }

    for (auto&t : threads_telechargement)
    {
        if (t.joinable())
            t.join();
    }

    return 0;
}

One last thing, it's a mistake to use a using namespace in a header file. It pollutes the global namespace in every cpp file that includes that header. In a project with 3 files it doesn't matter. When the project grows it will matter a lot.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
0
  1. Construct a pile_params:

pile_params pile_analyse(url_racine);

  1. Invoke the copy constructor.

thread(..., pile_analyse, ...);

... But we can't invoke the copy constructor because a pile_params is non-copyable... because std::mutex is non-copyable (so is condition_variable, IIRC).

One fix would be to pass the params by reference:

threads_telechargement[i] = thread(telecharger,
                                   pile_telechargement, 
                                   std::ref(pile_analyse),
                                   vect_all_pages);

another would be to pass a shared_ptr<> to the params.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
0

If constructor and copy constructor and move constructor was not defined in the structure or classes then these functions implicitly created.

In main.cpp file, you call copy constructor and this function implicitly generated like:

struct pile_params
{
    ...
    pile_params(pile_params& pile_params) = default;
};

Then in Members have an one is also not copyable, so a copy constructor would be ill-formed and cannot be generated for it.

copy constructor of std::mutex member is non-copyable like:

mutex( const mutex& ) = delete;
AmirSalar
  • 325
  • 2
  • 14