C++ Visual Studio 13 using threads: minimal example not working - c++

i got a minimal example from http://www.cplusplus.com/reference/thread/thread/
for the implementation of threads.
Unfortunately this example did not compile and ERROR C2664 is thrown:
// thread example
#include <iostream> // std::cout
#include <thread> // std::thread
void foo()
{
// do stuff...
}
void bar(int x)
{
// do stuff...
}
int main()
{
std::thread first (foo); // spawn new thread that calls foo()
std::thread second (bar,0); // spawn new thread that calls bar(0)
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
std::cout << "foo and bar completed.\n";
return 0;
}
error C2664: 'std::thread::thread(const std::thread &)' :
cannot convert argument 1 from 'void' to 'std::thread &&'
Can someone explain what ist wrong with the example // with Visual Studio?
Thanks

The above code works in VS 2013 (but also in VS 2012, VS 2015).
Try to create a new project and copy the code there. VS behaves strange sometimes. Also removing the .sdf file may help together with "build -> clean solution".

Related

Why does a lock_guard on a mutex reference produce C26110

The following code in a Visual Studio Professional 2019 project (version 16.3.6) produces a warning:
#include <thread>
#include <future>
class Foo {
public:
mutable std::recursive_mutex _writingMutex;
std::recursive_mutex& writingMutex() const { return _writingMutex; }
};
int main()
{
Foo a;
std::lock_guard<std::recursive_mutex> lock(a.writingMutex()); // produces C26110
std::lock_guard<std::recursive_mutex> lock2(a._writingMutex); // no warning
}
The first lock produces the warning C26110:
Warning C26110 Caller failing to hold lock 'lock' before calling function 'std::lock_guard::~lock_guard'
Why is this so? Does passing the mutex as reference not work?
Based on the compilation result of Alan and the comment of rustyx, I will answer my own question:
This is likely to be a code analysis bug in Visual Studio. Looks like C26110 can't recognize a mutex via a reference. The issue was reported here and I added my minimal example as comment there. The issue persists in the most recent version 16.3.7 as well

BUS error during thread invocation

The following code causes bus error in Rasbian distro on a rasberry pi mod2 system.
#include <thread>
#include <iostream>
class bar {
public:
void startFoo() {
std::thread t(&bar::foo, this); //bus error because of this line
t.join();
}
void foo() {
std::cout << "hello from member function" << std::endl;
}
};
int main()
{
bar b;
b.startFoo();
return 0;
}
This link states that bus error occurs when your processor cannot even attempt the memory access requested. But in my code i am accessing the class own member function in thread. I cannot relate how it is causing bus error. Could someone clarify me? P.S. The source code was cross compiled on a Ubuntu OS running on a x86 PC and the binary was tested on Rasberry pi (ARM).

Visual Studio 2013 std::thread

the following program give some strange compile/run-time behavior when compiled with Visual Studio 2013:
#include "stdafx.h"
#include <thread>
#include <chrono>
#include <iostream>
int main()
{
{//works
std::thread([](){
std::cout << " thread running1\n";
});
}
{//compile but synstax error exist
auto func = [](){
std::cout << " thread running2\n";
};
std::thread(fun); //fun is not defined
}
{//compile, see next block for mystery compile error
auto func = [](){
std::cout << " thread running2\n";
};
std::thread tmp(func);
}
{//does not compile and don't know why
auto func = [](){
std::cout << " thread running2\n";
};
std::thread(func); //error C2371: 'func' : redefinition; different basic types
}
return 0;
}
When this program work, there may crash as there is race condition between the thread. The main thread may end before the other threads.
Does anybody know why the second block and last block does not work?
{//compile but synstax error exist
auto func = [](){
std::cout << " thread running2\n";
};
std::thread(fun); //fun is not defined
}
There's no syntax error here, std::thread(fun) default constructs an std::thread object named fun.
The error in the last block is because of the same reason
std::thread(func); //error C2371: 'func' : redefinition; different basic types
You're trying to default construct an std::thread object named func above and that's an error because a lambda of the same name already exists in the same scope. To pass the lambda to the thread constructor use braces instead
std::thread{func};
Now, after you've made these changes your code will compile but will fail at runtime because the thread objects in blocks 1, 3 & 4 are all going to call std::terminate (of course, your program terminates when the first thread object calls std::terminate so the other two doing that is a moot point).
The reason that happens is that you have joinable threads in all 3 blocks and if the destructor of such a thread object runs, std::terminate will be called. To avoid that you must call thread::join (you could also call thread::detach but don't do that).
For instance
{//works
std::thread([](){
std::cout << " thread running1\n";
}).join();
}
Live demo

passing functor to boost::thread failed in visual studio 2010 [duplicate]

This question already has answers here:
Why is there no call to the constructor? [duplicate]
(3 answers)
Closed 8 years ago.
This simple code won't compile:
#include <cstdio>
#include <boost/thread.hpp>
struct ftor{
void operator()(){ printf("Hello"); }
};
int main()
{
boost::thread th( ftor() );
th.join(); //<--- error C2228: left of '.join' must have class/struct/union
}
But, following code well compiled:
#include <cstdio>
#include <boost/thread.hpp>
struct ftor{
void operator()(){ printf("Hello"); }
};
int main()
{
ftor f;
boost::thread th( f );
th.join();
}
Q: What's problem with #1 code ?
I use visual studio 2010 .
Update:
codepade http://codepad.org/r5Aok406 shows more informative error:
Line 19: error: request for member 'join' in 'th', which is of non-class type 'mythread ()(ftor (*)())'
boost::thread th( ftor() );
th is declared as a function that returns boost::thread, and takes a function pointer ftor(*)() as input parameter.
To avoid this, either use C++11's new initialization syntax,
boost::thread th{ ftor() };
Or add a parathesis around the ftor().
boost::thread th( (ftor()) );
This is actually one of the well-known c++ glitches. The cause of this problem is for C compatibility.
struct TEST;
TEST a(); //well defined in c, a is a function that returns TEST
C++ has to be compatible with C, so Test a() must be a function, but not declare a as a TEST instance and call its default constructor!

Movable parameters in std::thread constructor (Visual C++ 2012)

I run into problem with rvalue references in MSVC 2012.
#include <thread>
#include <string>
#include <future>
void foo(std::promise<std::string> &&prms) { /* some code */ }
int main() {
std::promise<std::string> prms;
// std::future<std::string> ftr = prms.get_future();
std::thread th(&foo, std::move(prms));
// some other code
}
Compiler says: error C2664: 'void (std::promise<_Ty> &&)' : cannot convert parameter 1 from 'std::promise<_Ty>' to 'std::promise<_Ty> &&'
Is there my mistake (then how to fix it) or compiler issue (then I'd like to know origin of such behaviour)?
This is a known issue in the Visual C++ 2012 implementation of std::thread. See the following bug on Microsoft Connect:
std::thread constructor doesn't handle movable object
The response to that bug states:
We attempted to fix this during VC11's development, but it exploded horribly and we had to revert the change. As it turns out, std::thread can't be powered by bind(), because thread needs to move its arguments, and bind() is forbidden from doing so (since bound functors should be repeatedly invokable, without their bound arguments being moved-from). So we'll need to reimplement std::thread's ctor to avoid using bind().