call_once to initialize a function - c++

I am trying to initialize a function using call_once(...). My program is giving me the compiling error 'std::once_flag::once_flag(const std::once_flag&)': attempting to reference a deleted function. I don't understand why the function is deleted.
#include "stdafx.h"
#include <string>
#include <thread>
#include <future>
#include <cstdio>
#include <iostream>
#include <queue>
#include <condition_variable>
using namespace std;
once_flag flagZero;
string printerFunc(queue<char>& input, once_flag& flag){
string output = "";
function<void(string&)> f;
call_once(flag, [&](){
f = [&](string& output){}; });
f(output);
return output;
}
int _tmain(int argc, _TCHAR* argv[])
{
string input = "0102030";
queue<char> inputQueue;
for(char c : input){
inputQueue.push(c);
}
auto zeros = async(printerFunc, ref(inputQueue), flagZero);
this_thread::sleep_for(chrono::seconds(10));
return 0;
}

My program is giving me the compiling error
std::once_flag::once_flag(const std::once_flag&): attempting to
reference a deleted function.
This is a copy constructor:
std::once_flag::once_flag(const std::once_flag&)
As per the documentation: std::once_flag is neither copyable nor movable. This is enforced by ensuring that the relevant constructors and assignment operator functions are deleted.
Just as with std::thread the arguments are passed by value.
To pass them by reference and to ensure that flagZero is not copied wrap it in std::ref and pass it as follows: std::async(printerFunc, ref(inputQueue),std::ref(flagZero));

Related

How do I fix this c++ expected expression error?

I am making something in c++, it doesn't have any errors visible in Visual Studio code, but when I use g++ to be able to execute it, I get this error:
In file included from Main.cpp:6: In file included from ./Filechange/Filechange.hpp:1: ./Filechange/Filechange.cpp:14:24: error: expected expression
std::thread first ([&wtime,&f,&fn]() mutable {
^ Main.cpp:16:33: error: expected expression
OnFilechange("FileEvent", 0.5, [](char* txt){
^ 2 errors generated.
These are the files:
Main.cpp:
#include <lua.hpp>
#include <iostream>
#include <chrono>
#include <thread>
#include <stdio.h>
#include "Filechange/Filechange.hpp"
void wait(int seconds)
{
std::this_thread::sleep_for(std::chrono::seconds(seconds));
}
int main(int argc, char const *argv[])
{
lua_State *State = luaL_newstate();
OnFilechange("FileEvent", 0.5, [](char* txt){
std::cout << txt << std::endl;
});
lua_close(State);
return 0;
}
Filechange.cpp:
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
char* StringToChar(std::string str){
char* Array = new char[str.length() + 1];
strcpy(Array,str.c_str());
return Array;
}
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt)){
std::thread first ([&wtime,&f,&fn]() mutable {
std::ifstream file(f);
std::string str;
std::string filecontents;
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
char* LastContents = StringToChar(filecontents);
char* CurrentContents = StringToChar(filecontents);
while (true){
if (wtime != 0){
std::this_thread::sleep_for(std::chrono::milliseconds(int(wtime*1000)));
}
filecontents = "";
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
CurrentContents = StringToChar(filecontents);
if (strcmp(LastContents, CurrentContents) != 0){
LastContents = StringToChar(filecontents);
fn(StringToChar(filecontents));
}
}
});
}
Filechange.hpp:
#include "Filechange.cpp"
#ifndef FILECHANGE_HPP
#define FILECHANGE_HPP
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
void OnFilechange(const char *f,float wtime,void (*fn)(char txt));
#endif
There's also a extension less file named FileEvent which will change in the runtime using other code files.
The Filechange.cpp and Filechange.hpp are in a folder named "Filechange"
This function:
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt))
expects a function pointer, and a lambda in g++ is not implemented as a function pointer. Instead, you should declare the function to take a std::function, as in:
void OnFilechange(const char *f, float wtime, std::function<void(char *)> fn)
You may also need #include <functional> to get the declaration of std::function.
use -std=c++17 in g++ if possible as g++ defaulted to c++98

Using boost::asio::post for a function that takes in parameters

I'm new to using boost, and threadpools. I want to pass a function to a threadpool that takes a variable as a parameter. In this simple example I'm just passing in an integer. My understanding is post is going to call an available thread in the thread pool to do something. The code compiles if I set up printProduct to not take in any variables, but that's not what I'm eventually trying to do.
References to relevant documentation would be very helpful (I haven't successfully found much discussion of boost::asio::post), as well as advice on how to solve the problem. Thanks!
#include <cstdlib>
#include <iostream>
#include <pthread.h>
#include <boost/version.hpp> // version is 1.72
#include <boost/asio.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/thread_pool.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/asio/post.hpp>
#include <boost/lockfree/queue.hpp>
using namespace std;
int printProduct(int endval){
// int endval = 1000000;
int prod = 1;
for (int i=0;i<endval;i++){
prod = prod * i;
}
return prod;
}
int main() {
boost::asio::thread_pool tp(8);
for (int i =0; i<200; i++){
// issue is how to pass the parameter into post
boost::asio::post(tp, printProduct,i);
// boost::asio::post(tp, printProduct(i));
}
}
boost::asio::post takes any callable object. Requirements for such object you can find here.
There are many ways to achive what you want:
[1] lambda expressions
boost::asio::post(tp, [i]{ printProduct(i); });
[2] bind
boost::asio::post(tp, std::bind(printProduct,i));
[3] custom functor class
struct Wrapper {
int i;
Wrapper(int i) : i(i) {}
void operator()() {
printProduct(i);
}
};
boost::asio::post(Wrapper{i});

C++ Compilation error about running io_context in different thread

I am trying to use the asio::io_context in multiple threads.
#include <iostream>
#include <vector>
#include <asio.hpp>
#include <thread>
#include <future>
#include <functional>
int main()
{
asio::io_context ctx;
std::vector<std::future<asio::io_context::count_type>> tasks;
for(int i = 0; i < 3; ++i)
{
tasks.push_back(std::async(std::launch::async, std::bind(&asio::io_context::run, &ctx));
}
for(auto &task: tasks){tasks.get();}
return 0;
}
However, I am getting a compilation error
asio_blah.cpp: In function ‘int main()’:
asio_blah.cpp:101:94: error: no matching function for call to ‘bind(<unresolved overloaded function type>, asio::io_context*)’
tasks.push_back(std::async(std::launch::async, std::bind(&asio::io_context::run, &ctx));
Not sure whey the compiler is not able to figure the member function pointer ( I believe the member func type is asio::io_context::count_type (asio::io_context*)() and the function signature should visible to the compiler since asio.hpp is included) and report the error unresolved overloaded function type.
Any suggestions to fix the error?
You may go for lambda something like this:
#include <iostream>
#include <vector>
#include <boost/asio.hpp>
#include <thread>
#include <future>
#include <functional>
using namespace boost;
int main()
{
asio::io_context ctx;
std::vector<std::future<asio::io_context::count_type>> tasks;
for(int i = 0; i < 3; ++i)
{
tasks.push_back(std::async(std::launch::async, [&ctx]() {
return ctx.run();
}));
}
for(auto &task: tasks){task.get();}
return 0;
}
Edit:
As rightly said by Miles Budnek, io_context::run has multiple overloads. You cannot take a pointer to it without forcing overload resolution with a cast.
If you really want to use std::bind, go for casting.
My opinion is same as that of others. GO FOR LAMBDA!!!

C++ unique_ptr and arrays

I'm trying to use arrays with unique_ptr with no success.
What is the correct way to declare a unique_ptr of some size?
(size is some paramter).
unique_ptr<A[]> ptr = make_unique<A[]>(size);
Here's an example:
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <memory>
using namespace std;
class A {
string str;
public:
A(string _str): str(_str) {}
string getStr() {
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
}
This is not working, however, if I delete the constructor of A, it works.
I want the 3 to represent the size of the array, and not an argument to A's constructor, how do I make that happen?
This is not working, however, if I delete the constructor of A, it
works.
When you removed the user defined constructor, the compiler implicitly generates a default one. When you provide a user defined constructor, the compiler doesn't implicitly generate a default constructor.
std::make_unique<T[]> requires the use of default constructors...
So, provide one, and all should work well
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <memory>
using namespace std;
class A {
string str;
public:
A() = default;
A(string _str): str(_str) {}
string getStr() {
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
}

implementing map<mpz_t, double> in c++

For some reason, I need to have a map from arbitrary huge number to double and I tried to implement it with c++98 (and I have to) and Xcode but it doesn't work:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include "gurobi_c++.h"
#include <sstream>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
#include <cmath>
#include <gmp.h>
using namespace std;
using namespace ::boost::tuples;
using namespace ::boost;
int main()
{
map<mpz_t, double>J;
mpz_t a,b,c,n;
string tempstring;
int xrange=5,yrange=5,component=5;
mpz_set_str(n,"11", 10);
J[n]=-1;
return 0;
}
The error shown is: Array initializer must be an initializer list. Could someone help me with it? Thank you:)
Here's the detail error page:
I don't know the details of mpz_t. However, it appears to be an array.
You can get around the problem by defining a class to be used as the key in your map.
I am able to create an executable using the following code with g++ 4.8.2.
#include <map>
using namespace std;
typedef int (mpz_t)[2];
struct MyKey
{
// Add a proper implementation of a constructor
// with mpz_t.
MyKey(mpz_t in) {}
// Add a proper implementation of copy constructor.
MyKey(MyKey const& copy) {}
// Add a proper implementation of assignment operator.
MyKey& operator=(MyKey const& rhs)
{
return *this;
}
bool operator<(MyKey const& rhs) const
{
// Add a proper implementation.
return false;
}
mpz_t n;
};
int main()
{
map<MyKey, double> J;
mpz_t n;
J[n] = 1.0;
return 0;
}