circular buffer for opencv cv::Mat - multithreading

I use this example for circular buffering.
To test test I created next three functions:
void cnt(ScreenStreamer &SS, cv::Mat & img)
{
CVUtils::Image m_img;
SS >> m_img;
img = m_img.matRef();
cv::imshow("",img);
cv::waitKey();
}
void snd1(circ_buffer<cv::Mat > & CM, cv::Mat & M)
{
CM.send(M);
}
void rcv1(circ_buffer<cv::Mat > & CV, cv::Mat & M)
{
M = CV.receive();
}
And then run it in main function:
int main()
{
ScreenStreamer stream;
CVUtils::Image m_img;
cv::Mat bufImg;
cv::Mat inpImg;
int key = 0;
circ_buffer<cv::Mat > F1;
F1.set_capacity(50);
std::thread m1( snd1, F1, &inpImg );
std::thread m2( rcv1, F1, &bufImg );
std::thread m3( cnt, stream, &inpImg);
m1.join();
m2.join();
m3.join();
return 0;
}
Unfortunatelly I get this error
Error C2664 'std::tuple<void (__cdecl *)(circ_buffer<cv::Mat> &,cv::Mat &),circ_buffer<cv::Mat>,cv::Mat *>::tuple(std::tuple<void (__cdecl *)(circ_buffer<cv::Mat> &,cv::Mat &),circ_buffer<cv::Mat>,cv::Mat *> &&)': cannot convert argument 1 from 'void (__cdecl &)(circ_buffer<cv::Mat> &,cv::Mat &)' to 'std::allocator_arg_t' CVUtilsExperimental C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\memory 1630
Does it come from template implementation or from function implementations?
What to google to solve it?

All your functions take parameters by reference, you need to use std::ref wrapper class to pass them to thread constructor:
The arguments to the thread function are moved or copied by value. If a reference argument needs to be passed to the thread function, it has to be wrapped (e.g. with std::ref or std::cref). thread reference
so rewrite as follows
std::thread m1( snd1, ref(F1), ref(inpImg) );
std::thread m2( rcv1, ref(F1), ref(bufImg) );
std::thread m3( cnt, ref(stream), ref(inpImg) );

Related

An error occur when using a thread variadic template constractor

void f(vector<int>& v){
for(const auto& x:v) cout << x;
}
class F{
private:
vector<int> v;
public:
F(vector<int>& vc):v{vc}{}
void operator()(){
for(const auto& x:v) cout << x;
}
};
int main()
{
vector<int> some_vec{3,5,77,32,1};
vector<int> vec{66,8,90,45,777};
thread t1{f,some_vec};
thread t2{F(vec)};
t1.join();
t2.join();
cout << '\n';
}
An error "no type named 'type' in 'class std::result_of< void (*(std::vector))(std::vector&)>' occur
If the argument vector in f is declared as const, void f(const vector<int>& v), the error disappears.
On the other hand, the code with function object F works just fine.
Code from Bjarne Stroustrup -- the C++ programming language 5.3.2 Passing Arguments
std::thread stores copies of the arguments passed to its constructor, and then uses rvalues of those copies as the arguments for a handler. That is, function f cannot be called with an rvalue of std::vector, as it expects a non-const lvalue reference. Even if you change it to a const lvalue reference, then it's a copy of what is actually passed to the t1's constructor.
On the contrary, class F has an implicitly defined copy-constructor, and its function call operator expects no arguments, hence you get no errors. (And F itself is constructed before it's passed to a thread's constructor).
If you want function f to operate on the some_vec instance, you'd have to wrap it with a reference wrapper:
#include <functional>
std::thread t1{f, std::ref(some_vec)};
// ~~~~~~~^

C++11 std::thread accepting function with rvalue parameter

I have some homework, and I have troubles understanding, (probably) how passing parameters to std::thread constructor works.
Assume following code (I deleted unneeded parts)
template<typename T, typename Task>
class Scheduler
{
private:
typedef std::unordered_map<std::size_t, T> Results;
class Solver
{
public:
Solver(Task&& task) : m_thread(&Solver::thread_function, std::move(task))
{
m_thread.detach();
}
Solver(Solver&& solver) = default; // required for vector::emplace_back
~Solver() = default;
private:
void thread_function(Task&& task)
{
task();
}
std::thread m_thread;
};
public:
Scheduler() = default;
~Scheduler() = default;
void add_task(Task&& task)
{
m_solvers.emplace_back(std::move(task));
}
private:
std::vector<Solver> m_solvers;
};
template<typename T>
struct Ftor
{
explicit Ftor(const T& t) : data(t) { }
T operator()() { std::cout << "Computed" << std::endl; return data; }
T data;
};
int main()
{
Scheduler<int, Ftor<int>> scheduler_ftor;
Scheduler<int, std::function<int(void)>> scheduler_lambda;
Ftor<int> s(5);
scheduler_ftor.add_task(std::move(s));
scheduler_lambda.add_task([](){ std::cout << "Computed" << std::endl; return 1; });
}
Why it doesn't compile?
MVS2015 is complaining about
functional(1195): error C2064: term does not evaluate to a function taking 1 arguments
functional(1195): note: class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
note: while compiling class template member function 'Scheduler<int,Ftor<int> >::Solver::Solver(Task &&)'
While G++ 4.9.2
functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (Scheduler<int, Ftor<int> >::Solver::*)(Ftor<int>&&)>(Ftor<int>)>’:
required from ‘void Scheduler<T, Task>::add_task(Task&&) [with T = int; Task = Ftor<int>]’
functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Scheduler<int, Ftor<int> >::Solver::*)(Ftor<int>&&)>(Ftor<int>)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
I suppose there are some problems with std::moving to std::thread.
If you use member function as first thread argument, second argument supposed to be this pointer, pointing to the object to which member function could be called to
UPDATE
Good discussion here
Start thread with member function
I don't follow your code, but addressing the question, a extrapolated answer will be( most of the code is psuedocode)
lets assume that there is a function int test(int name).
thread t0;
t0 = thread(test,32);
thread t1(test,43);
Passing a argument to function.
int temp = 0;
int testfunc(int& q)
{
cout<<q;
}
thread t1;
t1 = thread(testfunc,ref(temp));
In short, you just pass the name of the function that must be run in the thread as the first argument, and the functions parameters follow it in same order as they are in the function definition, for passing by reference you can use the ref() wrapper.See the below example.
#include <iostream>
#include <thread>
#include <string>
using namespace std;
void test(int a,int &a,string test)
{
\\do something
}
int main()
{
int test1 = 0;
string tt = "hello";
thread t1;
t1 = thread(23,&test1,tt);
t1.detach();
return 0;
}
if you are wondering about the use of join() and detach(), refer to this thread: When should I use std::thread::detach?, refer to my answer post in that thread.

Templated arguments causes inability to "Find function"

I'm trying to write some code that can execute a class member function in a separate thread, but with some added code for checking odds and ends before and after the thread has executed.
For this I'm using the std::bind and std::thread functionality of c++11 in VS2012.
All this works well if I fix the arguments the class member function can have to e.g. void*, but if I try to template that, I get an error.
Overview of Code
I have a simple class containing two functions, which will be executed in a thread. They differ in arguments and name.
The class I'm creating has a templated constructor and a function, which executes the functions of the previous class, but with the ability to perform it's own checks/notifications if the thread does/does not finish.
Finally the main function is simply to test the code.
The Code
Include Part:
#include <iostream>
#include <memory>
#include <thread>
#include <functional>
using namespace std;
Class to be executed:
class k1
{
public:
k1( int nVal = 0 ) : _val(nVal){};
~k1(){};
void doFunc( void * pParam ){
cout << "Val in class = " << _val << ", param = " << pParam << "\n";
}
void doFunc2( float pParam ){
cout << "Val in class = " << _val << ", param = " << pParam << "\n";
}
int _val;
};
typedef shared_ptr<k1> PK;
Thread Handle Class
class H
{
public:
void controlFunction( std::function<void(void)> callRef ){
cout << "Before calling\n";
callRef();
cout << "After calling\n";
}
template<class T, typename ParamType> // Constructor for any type of class function - void * parameter as only input
H( void(T::* pFunction)(ParamType *), T * pClass, ParamType pParam ){
std::function<void(void)> _call = std::bind( pFunction, pClass, pParam );
_thread = shared_ptr<std::thread>( new thread( &H::controlFunction, this, _call ));
}
~H( void ){
_thread->join();
}
shared_ptr<thread> _thread;
};
typedef shared_ptr<H> PH;
Main Function:
int main(int argc, char* argv[])
{
PK k = make_shared<k1>( 12 );
int i1 = 2;
float f1 = 1.0f;
PH p = PH( new H(&k1::doFunc, k.get(), &i1 ));
PH p2 = PH( new H(&k1::doFunc2, k.get(), f1 ));
return 0;
}
The error that comes out is:
error C2660: 'H::H' : function does not take 3 arguments
Thanks in advance!
/Henrik
I'm guessing that you get the error on the second line where you create a H object taking k1::doFunc2 as argument. The probable reason for the error is because k1::doFunc2 doesn't take a pointer for argument, while the member function pointer argument in the H constructor expects it to.
There is also some problems with the first line, when you declare the p variable. This is because then the ParamType template type can be deduced to be either int or int*. The reason for this is because the member function pointer have ParamType *, while the third argument to the constructor uses non-pointer ParamType but you pass a pointer here (&i1).

Thread Program in visual C++/CLI giving errors

I am trying to follow the tutorial at http://www.drdobbs.com/cpp/ccli-threading-part-i/184402018 to do thread programming in winform in visual c++. I opened a win32 console project and added an empty cpp file to it inside which i placed the code as follows:
using namespace System;
using namespace System::Threading;
public class ThreadX{
int loopStart;
int loopEnd;
int dispFrequency;
public:
ThreadX(int startValue, int endValue, int frequency)
{
loopStart = startValue;
loopEnd = endValue;
dispFrequency = frequency;
}
void ThreadEntryPoint()
{
String^ threadName = Thread::CurrentThread->Name;
for (int i = loopStart; i <= loopEnd; ++i)
{
if ( i % dispFrequency == 0)
{
Console::WriteLine("{0} : i = {1,10}", threadName, i);
}
}
Console::WriteLine("{0} thread terminating", threadName);
}
};
int main()
{
ThreadX o1 = gcnew ThreadX(0, 1000000,200000);
Thread^ t1 = gcnew Thread(gcnew ThreadStart(o1, &ThreadX::ThreadEntryPoint));
t1->Name = "t1";
ThreadX o2 = gcnew ThreadX(-1000000, 0, 200000);
Thread^ t2 = gcnew Thread(gcnew ThreadStart(o2, &ThreadX::ThreadEntryPoint));
t1->Name = "t2";
t1->Start();
t2->Start();
Console::WriteLine("Primary Thread Terminating");
}
However this gives me errors such as :
error C2726: 'gcnew' may only be used to create an object with
managed type
error C2440: 'initializing' : cannot
convert from 'ThreadX *' to 'ThreadX' No constructor could take the
source type, or constructor overload resolution was ambiguous
error C3364: 'System::Threading::ThreadStart' : invalid argument for delegate constructor; delegate target needs to be a
pointer to a member function
You are mixing C++ and C++/CLI which is a different thing. Replace
public class ThreadX
with
public ref class ThreadX

Can't invoke or assign a std::function that has an rvalue reference as an argument (Visual C++)

Seems like Visual C++'s std::function<> doesn't handle functions with rvalue refs as arguments. Can anyone suggest a workaround?
#include <functional>
using namespace std;
class Object { };
void f(Object&&) { }
auto g = [](Object&&){ };
function<void(Object&&)> h;
int main()
{
Object o;
f(move(o));
g(move(o));
// Uncomment any one of the following lines, and we get an error from the instantiation
// of std::function: "error C2664: You cannot bind an lvalue to an rvalue reference"
//h(move(o));
//h = g;
//h = f;
return 0;
}
This is Visual Studio 2010. I am not using /Za (so it is not this problem).
Update after some research: The code compiles in Clang, so I am pretty sure it is a Microsoft bug. It might be this one, fixed in VC11: 649274
Correction to the update: The MS bug is not fixed in VC11. From the link:
our first opportunity will be the "out of band" release between VC11 and VC12 that Herb Sutter
announced at the GoingNative 2012 conference.
I'm not sure what workaround you'd like here. Assuming you cannot change the call expression of the function object and the target signature, you can wrap the rvalue reference and pass the wrapped object (a temporary) via const ref.
Essentially, the call expands to: f( wrap(move(o)) );
I suspect there's a problem with perfect forwarding, because binding i = bind(&f); does not work; therefore I've introduced an intermediate step performing perfect forwarding, such that the call is resolved to: f( move( (Object&)wrap( move(o) ) ) );
#include <iostream>
#include <functional>
using namespace std;
struct Object { int m; };
// target function with fixed signature (assuming we cannot change that)
void f(Object&& p) { p.m = 42; std::cout << p.m; };
// was surprised I didn't find any method to chain functions in the StdLib
// so here's my own:
template < typename F1, typename F2, typename P1 >
auto chain2(F1 f1, F2 f2, P1&& p1)
-> decltype( f1(f2( std::forward<P1>(p1) )) )
{
return f1( f2( std::forward<P1>(p1) ) );
}
// a special bind version; mostly syntactic sugar
// note you can also deduce the first template parameter; would be more work
// and not necessary here
template < typename P1, typename F1, typename F2 >
auto bind_chain(F1 f1, F2 f2)
-> decltype( std::bind( &chain2<F1,F2,P1>, f1, f2, std::placeholders::_1 ) )
{
return std::bind( &chain2<F1,F2,P1>, f1, f2, std::placeholders::_1 );
}
// as `std::move` is overloaded, we make things a little bit simpler;
// we later will need to get a function pointer on this, that's why
// I'd like to avoid too much overloading
template < typename T >
// for a certain reason, cannot use && here --------v, clang++3.2 accepts it
typename std::remove_reference<T>::type && my_move(T& p)
{
return std::move(p);
}
struct wrapper
{
Object&& m;
wrapper(Object&& p) : m(std::move(p)) {}
operator Object&() const { return m; }
// alternatively:
// operator Object&&() const { return std::move(m); }
};
int main()
{
Object o;
// we'll need to call the functor with an const ref
function<void(wrapper const&)> i;
// chaining the conversion to rvalue ref with the target function
i = bind_chain<wrapper const&>( &f, &my_move<Object> );
i( move(o) );
return 0;
}

Resources