XCode Boost Thread Example Compile Error - multithreading

Here's my basic boost code
#include <iostream>
#include <boost/thread.hpp>
using namespace boost;
using namespace boost::this_thread;
using namespace std;
// Global function called by thread
void GlobalFunction()
{
for (int i=0;i<10;++i)
{
cout << i << "Do something in parallel with main method." << endl;
boost::this_thread::yield();
}
}
void GlobalThreadTest()
{
boost::thread t(&GlobalFunction);
for (int i = 0; i<10; ++i) {
cout << i << "Do something in main method. " << endl;
}
}
int main(int argc, const char * argv[])
{
GlobalThreadTest();
return 0;
}
I'm getting lots of errors like this from xcode
(null): "boost::this_thread::yield()", referenced from:
(null): "boost::detail::thread_data_base::~thread_data_base()",
referenced from: (null): "boost::system::system_category()",
referenced from: (null): "boost::system::generic_category()",
referenced from: (null): "boost::thread::start_thread()", referenced
from:
I have installed boost using macports and the header search paths in xcode is set to
/opt/local/include/
This contains all the .hpp files.
2 Questions
Does boost create *.o files if so where are they stored?
How do I get xcode 4.2 to work with this boost thread example? If there is a flag I have to set, which parameter in xcode 4.2 do I set it in?
Thanks.

Related

How to make a simple structure visible in different cpp file

I have a structure defined in main.cpp file and i want that same structure with value to be visible in other cpp file.
I tried this and this is way out of understanding for me as a beginner. So i re-ask the initial question again but in a simple way.
main.cpp
#include<iostream>
extern struct abc
{
int a;
std::string bla_bla;
};
void display(abc (&)[2]);
int main(void)
{
abc put[2];
put[0] = {10,"Apple"};
put[1] = {20,"Ball"};
display(put);
}
other.cpp
#include <iostream>
extern abc;
void display(abc (&put)[2])
{
std::cout << put[0].a << '\t' << put[0].bla_bla << std::endl;
std::cout << put[1].a << '\t' << put[1].bla_bla << std::endl;
}
it shows error a storage class can only be specified for objects and functions
and i am using c++17
is there any way to make that one structure visible to every cpp?
thanks in advance
EDIT: I got it need to keep the struct in .h file

creating a static library using g++ -std=c++11 using templates

While creating a static library using c++11, it fails during linking I think.
I can create a static library and link to it using the information in How to create a static library with g++? with ordinary c++, but if I try to follow the steps using c++11 features, it fails during the linking.
test.cpp:
#include <iostream>
#include <vector>
#include "libtestlib.h"
using namespace std;
int main() {
itest n1={{1,2,3,4},
{5,6,7,8},
{9,0,1,2}};
cout << "testing...\n";
test_print(n1);
return 0;
}
libtestlib.h:
#ifndef testlib
#define testlib
#include <vector>
using namespace std;
typedef vector<vector<double>> dtest;
typedef vector<vector<int>> itest;
template <typename testtype>
void test_print(testtype);
#endif
libtestlib.cpp:
#include <iostream>
#include "libtestlib.h"
using namespace std;
template <typename testtype>
void test_print(testtype &t)
{
int m=t.size();
int n=t[0].size();
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++)
cout << t[i][j] << " ";
cout << endl;
}
cout << endl;
}
this is the output I'm getting:
$ g++ -std=c++11 -c libtestlib.cpp
$ ar rvs libtestlib.a libtestlib.o
r - libtestlib.o
$ g++ -std=c++11 test.cpp libtestlib.a
/tmp/cccJ7SXZ.o:test.cpp:(.text+0x1af): undefined reference to `void test_print<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)'
/tmp/cccJ7SXZ.o:test.cpp:(.text+0x1af): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `void test_print<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)'
collect2: error: ld returned 1 exit status
Since it seems you are supporting operations on a limited number of types, the classic "Just do it in a header" answer to this problem is not necessarily the best one.
You can get the best of both worlds by explicitely exporting a symbol for each implementation, but delegate the implementation to a template within the library:
libtestlib.h:
#ifndef testlib
#define testlib
#include <vector>
using namespace std;
typedef vector<vector<double>> dtest;
typedef vector<vector<int>> itest;
void test_print(dtest&);
void test_print(itest&);
libtestlib.cpp:
#include <iostream>
#include "libtestlib.h"
using namespace std;
namespace {
template <typename testtype>
void test_print_impl(testtype &t)
{
int m=t.size();
int n=t[0].size();
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++)
cout << t[i][j] << " ";
cout << endl;
}
cout << endl;
}
}
void test_print(dtest& val) {
test_print_impl(val);
}
void test_print(itest& val) {
test_print_impl(val);
}
Mind you, for a small function like this, it's probably not worth the effort, and just inlining the template code in the header is just fine. At what point does the complexity of a function and the scope of its dependencies warrant this is a bit of a judgement call.
Putting example requested from comment on the question here as code looked really ugly in a comment. Combined libtestlib.h and libtestlib.cpp
#ifndef testlib
#define testlib
#include <vector>
#include <iostream>
using namespace std;
typedef vector<vector<double>> dtest;
typedef vector<vector<int>> itest;
template <typename testtype>
void test_print(testtype &t)
{
int m=t.size();
int n=t[0].size();
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++)
cout << t[i][j] << " ";
cout << endl;
}
cout << endl;
}
#endif

vcl methods + exceptions corrupt memory with bcc64

My colleagues and I had a strange bug in a C++ Builder program and boiled it down to the following snippet:
#include <vcl.h>
#include <iostream>
void SIDE_EFFECTS() {
if (StrToFloat("1337")) {
throw "abc";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
double innocent = StrToFloat("42");
std::cout << innocent << std::endl;
try {
SIDE_EFFECTS();
} catch (...) {
}
std::cout << innocent << std::endl;
return 0;
}
Expected Output:
42
42
Actual Output when compiled for 64bit/ReleaseBuild/OptimizationsON:
42
1337
Compiler (latest 10.1 Berlin version of C++ Builder):
Embarcadero C++ 7.20 for Win64 Copyright (c) 2012-2016 Embarcadero Technologies, Inc.
Embarcadero Technologies Inc. bcc64 version 3.3.1 (35759.1709ea1.58602a0) (based on LLVM 3.3.1)
The internet says [citation needed] that the bug is always in the user program but never in the compiler or standard library, so please enlighten us if/where we do things that are not to be done in C++ / C++ Builder.
Strictly speaking, there is nothing wrong with this code, so it has to be a compiler bug. File a bug report at Quality Portal.
That being said, you should generally stay away from using catch (...). If you are going to catch an exception at all, at least catch what you are expecting and willing to handle:
catch (const char *)
Let anything unexpected pass through and be handled higher up the caller chain.
I would not recommend throwing a string literal directly. It is better to wrap it in a std::runtime_error or System::Sysutils::Exception based object instead.
#include <vcl.h>
#include <iostream>
#include <stdexcept>
void SIDE_EFFECTS() {
if (StrToFloat("1337")) {
throw std::runtime_error("abc");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
double innocent = StrToFloat("42");
std::cout << innocent << std::endl;
try {
SIDE_EFFECTS();
} catch (const std::runtime_error &) {
}
std::cout << innocent << std::endl;
return 0;
}
#include <vcl.h>
#include <iostream>
void SIDE_EFFECTS() {
if (StrToFloat("1337")) {
throw Exception("abc");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
double innocent = StrToFloat("42");
std::cout << innocent << std::endl;
try {
SIDE_EFFECTS();
} catch (const Exception &) {
}
std::cout << innocent << std::endl;
return 0;
}

std::async, libc++ with clang33 does not work under Linux

I have a really simple program that works with clang33 under OSX. However if I try to run the same program under Linux it fails. Has anyone got std::asynch to work with clang33 under Linux (CentoOS)?
#include <iostream>
#include <future>
#include <thread>
int main() {
// future from a packaged_task
std::packaged_task<int()> task([]() {
return 7;
}); // wrap the function
std::future<int> f1 = task.get_future(); // get a future
std::thread(std::move(task)).detach(); // launch on a thread
// future from an async()
std::future<int> f2 = std::async(std::launch::async, []() {
return 8;
});
// future from a promise
std::promise<int> p;
std::future<int> f3 = p.get_future();
std::thread([](std::promise<int> & p) {
p.set_value(9);
},
std::ref(p)).detach();
std::cout << "Waiting..." << std::flush;
f1.wait();
f2.wait();
f3.wait();
std::cout << "Done!\nResults are: " << f1.get() << ' ' << f2.get() << ' '
<< f3.get() << '\n';
}
The above example works with trunk/198686 when I compile libc++ with cxxabi. However now I have encountered another problem:
#include <iostream>
#include <vector>
#include <exception>
int main () {
std::vector<int> foo;
try {
foo.at(1);
}
catch (std::exception& e) {
std::cerr << "exception caught: " << e.what() << '\n';
}
std::cout << "Works" << '\n';
return 0;
}
The example code above generates the following expected output under OS X:
exception caught: vector
Works
Under Linux I get the following output:
exception caught: Segmentation fault
I have debugged the code and segmentation fault occurs inside the destructor of logic_error (stdexcept.cpp, line 137). Does anyone have any suggestions?
BTW: Its no longer possible to compile libc++ using the libsupc++ method.
I have actually got everything to work. The above problems occurs with r198686. I checked out the same revision as #BenPope and then everything works as expected.
Thanks,
Patrik

Trouble with garbage chars in visual c++ file reading

I am trying to read a text file using the following code:
void function readfile(char *inputfile) {
istream is;
int filesize = 0;
is.open(inputfile);
if (!is.is_open()) {
return;
}
is.seekg(0, ios::end);
filesize = (int)is.tellg();
is.seekg(0, ios::beg);
char *buf = new char[filesize];
is.read(buf, filesize);
is.close();
cout << buf << endl;
delete[] buf;
return;
}
While in g++ (mac / macports) it works correctly (getting all contents into a dynamic allocated char* array), in Visual Studio C++ 2010, I get constant errors of this type: Debug assertion failed: (unsigned)(c+1) <= 256, file isctype.c.
The problem is that it opens the file but can't find a termination delimeter so when it reaches the eof it starts reading somewhere else (garbage characters). Using the cout << buf; I can see that the file is being read correctly in mac but in visual c++ it types more garbage chars. What is the problem here?
Make your buffer one larger and add the terminating nul yourself.
Let C++ standard library do the work for you:
void readfile(const char *inputfile) {
std::ifstream is(inputfile);
std::string buf(std::istreambuf_iterator<char>(is), {});
std::cout << buf << std::endl;
}
See, it's now also
exception safe
handles embedded NUL characters correctly
Note, of course you can use vector instead of string if you prefer (just change that one word)
Full demo: see it live on Coliru
#include <fstream>
#include <iostream>
#include <iterator>
void readfile(const char *inputfile) {
std::ifstream is(inputfile);
std::string buf(std::istreambuf_iterator<char>(is), {});
std::cout << buf << std::endl;
}
int main()
{
readfile("main.cpp");
}
Update For C++11 challenged compilers (and showing how to use a vector):
Also Live on Coliru
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
void readfile(const char *inputfile) {
std::ifstream is(inputfile);
std::istreambuf_iterator<char> f(is), l;
std::vector<char> buf(f, l);
std::cout.write(buf.data(), buf.size());
}
int main()
{
readfile("main.cpp");
}

Resources