Chce pozbyć się efektu wyścigu wątków, najbardziej podstawowe nażedzie to mutex i lock_guard
i teraz tak kiedy używam tego z funkcjemi wszystko działa poprawnie tekst wyświetla się na przemiennie
ale jak chce tego użyć z funktorami to mam taki błąd
error C2280: 'std::mutex::mutex(const std::mutex &)' : attempting to reference a deleted function
przecież żadna funkcja nie jest usunięta więc o co chodzi??
#include <iostream>
#include <thread>
#include <mutex>
class thread_guard
{
private:
std::thread m_thread;
public:
thread_guard(std::thread t)
{
m_thread = std::move(t);
if (!m_thread.joinable())
std::cout << "Brak watku!!!" << std::endl;
}
~thread_guard()
{
m_thread.join();
}
};
class func
{
private:
std::mutex mut;
public:
void operator()()
{
for (int i = 0; i < 11000; i++)
{
std::lock_guard<std::mutex> guard(mut);
std::cout << "watek dziala 1" << std::endl;
}
}
};
class func2
{
private:
std::mutex mut;
public:
void operator()()
{
for (int i = 0; i < 11000; i++)
{
std::lock_guard<std::mutex> guard(mut);
std::cout << "watek dziala 2" << std::endl;
}
}
};
std::mutex mut2;
void fun()
{
for (int i = 0; i < 11000; i++)
{
std::lock_guard<std::mutex> guard(mut2);
std::cout << "watek dziala 1" << std::endl;
}
}
void fun2()
{
for (int i = 0; i < 11000; i++)
{
std::lock_guard<std::mutex> guard(mut2);
std::cout << "watek dziala 2" << std::endl;
}
}
int main(void)
{
thread_guard t1( (std::thread( func() )) );
thread_guard t2( (std::thread(func2() )) );
//thread_guard t1((std::thread(fun)));
//thread_guard t2((std::thread(fun2)));
}