i need make new thread in class and use it.
Somethink like:
class Somethink
{
public:
func_to_be_thread();
init_func();
}
Somethink::init_func()
{
std::thread newThread(func_to_be_thread);
}
int main()
{
Somethink ss;
ss.init_func();
}
EDIT:
How to make it correctly? Everythink i wrote returns error becouse idk how to make new thread in class with parameter (function to run) class method. My question is how to do it correctly?
If you need to create a thread with member function you can do the following:
class Something
{
public:
void func_to_be_thread();
void func_to_be_thread_advanced(const char* arg1);
std::thread init_func();
std::thread init_func_with_param(const char *arg1);
}
std::thread Something::init_func()
{
return std::thread(&Something::func_to_be_thread, this);
}
Also you can do it with lambda and parameters:
std::thread init_func_with_param(const char *arg1)
{
return std::thread([=] { func_to_be_thread_advanced(arg1); });
}
which C++ version are you using ? you can use std::thread only starting C++11 .. if you need more help with the syntax, you can check std::thread calling method of class
Start thread with member function
Related
I've read various answer on SO and still didn't understood how I should make an object method to be callable in this case:
Considering:
Class A
{
void generator(void)
{
int i = 1;
while(1)
{
if(i == 1)
{
one(/***/);//Should be a flag
i = 2;
}
else
{
two(/**/);//Should be a flag
i = 1;
}
}
}
template <typename CallbackFunction>
void one(CallbackFunction&& func)
{
}
template <typename CallbackFunction>
void two(CallbackFunction&& func)
{
}
A()
{
std::thread t(&A::generator, this);
t.detach();
}
};
and a simple main file:
void pOne(/**/)
{
std::cout<<"1"<<std::endl;
}
void pTwo(/**/)
{
std::cout<<"2"<<std::endl;
}
A myA;
A.One(pOne);
A.Two(pTwo);
int main(int argc, char** argv)
{
while(1){}
}
Here are where I'm at:
generator() should update a flag, and both one() & two() should poll on that flag & loop forever.
One() (two() also) should have a function pointer as parameters and if necessary other parameters, pOne() should have the same parameters except the function pointer.
So my questions are:
1) Is my understanding correct?
2) Is there a clean way to make generator() to start one() or two() ? (flags, semaphore, mutex, or anything that is a standard way to do it)
3) Assuming that the code was working, is it behaving as I expect ? i.e. printing 1 and 2?
if it matters, I'm on ubuntu
Disclaimer 1: Like everyone else, I'm interpreting the question as:
-> You need an event handler
-> You want callback methods on those events
And the only reason I think that is because I helped you on a i2c handler sequence before.
Also, there are better logic than this, its provided following your stubs "rules".
You mentioned that you are on Ubuntu, so you will be lacking windows event system.
Disclaimer 2:
1- To avoid going to deep I'm going to use a simple way to handle events.
2- Code is untested & provided for logic only
class Handler
{
private:
std::mutex event_one;
event_one.lock();
void stubEventGenerator(void)
{
for(;;)
{
if(!event_one.try_lock())
{
event_one.unlock();
}
sleep(15); //you had a sleep so I added one
}
}
template <typename CallbackFunction>
void One__(CallbackFunction && func)
{
while(1)
{
event_one.lock();
func();
}
}
public:
Handler()
{
std::thread H(&Handler::stubEventGenerator, this);
}
~Handler()
{
//clean threads, etc
//this should really have a quit handler
}
template <typename CallbackFunction>
void One(CallbackFunction && func) //I think you have it right, still I'm not 100% sure
{
std::thread One_thread(&Handler::One__, this, func); //same here
}
};
Some points:
One() as to be a wrapper for the thread calling One__() if you want it to be non-blocking.
mutex can be a simple way to handle events as long as the same event doesn't occur during its previous occurence (you are free to use a better/more suitable tool for your use case, or use boost:: only if necessary)
Prototype of One() & One__() are probably wrong, that's some research for you.
Finally: How it works:
std::mutex.lock() is blocking as long as it can't lock the mutex, thus One__ will wait as long as your event generator won't unlock it.
Once unlock One__ will execute your std::function & wait for the event (mutex) to be raised (unlock) again.
far from a perfect answer, but lack of time, and not being able to put that in a comment made me post it, will edit later
With whatever limited information you provided this code can be made compilable in following manner:
#include <iostream>
#include <thread>
typedef void(*fptr)();
void pOne(/**/)
{
std::cout<<"1"<<std::endl;
}
void pTwo(/**/)
{
std::cout<<"2"<<std::endl;
}
class A
{
public:
void generator(void)
{
int i = 1;
while(1)
{
if(i == 1)
{
fptr func = pOne;
one(func);//Should be a flag
i = 2;
}
else
{
fptr func = pTwo;
two(func);//Should be a flag
i = 1;
}
}
}
template <typename CallbackFunction>
void one(CallbackFunction&& func)
{
func();
}
template <typename CallbackFunction>
void two(CallbackFunction&& func)
{
func();
}
A()
{
std::thread t(&A::generator, this);
t.detach();
}
};
int main()
{
A myA;
while(1)
{
}
return 0;
}
If you want that one and two should accept any type/number of arguments then pass second argument as variadic template.Also I could not understand why you want one and two to be called from main as your generator function is for this purpose only and this generator function is called from thread which is detached in class constructor
I am trying to create a Thread Pool in C++. The concept is that main will create a new Task and the ThreadPool class will get the Task objects and implement the other work. This is the Task class:
template<typename ... Arguments>
class Task{
//
public:
Task(std::function<void (Arguments...)> _func, Arguments... _args){
auto f1 = std::bind(_func, _args...);
f1();
};
void run(){
};
};
and this how i am trying to use it:
#include <iostream>
#include <algorithm>
#include "Task.hpp"
void prtinhi(int a)
{
std::cout << a << std::endl;
return;
}
int main(){
Task<int> task(prtinhi, 5);
task.run();
return 0;
}
Obviously the Task object will be passed in the ThreadPool and the ThreadPool will call run() according to some logic.
This codes run successfully printing 5. But what i want is to call f1() from the run() function. If i change Task class to have the auto f1 defintion as a class member i get an error:
non-static data member declared ‘auto’
if i declare it static i can't assign a value to it.
So how do i solve this one? Is there a better way to do it?
Why does the task need to know about the function arguments? The task should be a void() type-erased callable object.
using Task = std::function<void()>;
Task task([]{ prtinhi(5); });
ThreadPool pool;
pool.schedule(task);
// ...
// `prtinhi(5)` will be eventually called.
If you need to get the return value, you want std::future.
ThreadPool could be implemented as follows:
struct ThreadPool
{
some_lockfree_queue<Task> _queue;
std::vector<std::thread> _workers;
void initialize()
{
for(int i = 0; i < num_threads; ++i)
{
_workers.emplace_back([this]
{
Task t = _queue.try_dequeue();
t();
});
}
}
};
std::bind is bad (watch this talk by STL for more info). You should use lambdas to bind the arguments you require and get a nullary function back.
I used to code on C++ long ago, but now decided to recall old skills and achieve some new ones :D
For now I am trying to rewrite my C# program in C++ and one problem occured - I don't know how to manage threads, or even how to create them, using class methods and calling methods from the class.
class MyObj {
private:
void thread() {
while (true) {
std::string a;
cin >> a;
}
}
static DWORD static_entry(LPVOID* param) {
MyObj *myObj = (MyObj*)param;
myObj->thread();
return 0;
}
public:
void start() {
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)static_entry, this, 0, NULL);
}
};
That is sample, I've found here, on StackOverflow but 'void thread()' was empty function, I've added code, given above, but the thread seems to start and close immediately.
I've added code, given above, but the thread seems to start and close immediately.
That's because you don't wait for threads to finish in your main thread.
As from their documentation, you'll need to add something like
// Wait until all threads have terminated.
WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);
For std::thread this should be a call to std::thread::join().
I'd rather recommend using std::thread as a member of the MyObj class:
class MyObj {
private:
void thread_fn() {
while (true) {
std::string a;
cin >> a;
}
}
std::thread t;
public:
void start() {
t = std::thread(&MyObj::thread_fn,*this);
}
~MyObj() {
if(t.joinable())
t.join();
}
};
Thank you for your answers.
Using std::thread turned out to be easier than using CLI Tread class.
static void input() {
while (true) {
std::string a;
cin >> a;
secureProg::execute_command(a);
}
}
auto start() {
std::thread thread(secureProg::input);
thread.join();
return thread.get_id();
}
Thread start from main
secureProg a;
auto thread_ptr = a.start();
Final version (I hope) of two methods within class
I am trying to re-use a simple thread pool mentioned in SO -
class thread_pool
{
thread_safe_queue<std::function<void()> work_queue; // need to submit fun(v) of class A where v is vector<string> here.
void worker_thread() {
while(!done)
{
std::function<void()> task;
if(work_queue.try_pop(task))
{
task(); // how should my function MyClass::Func(a,b) be called here?
}
else
{
std::this_thread::yield();
}
}
}
// -- Submit a task to the thread pool
template <typename FunctionType>
void submit(FunctionType f) {
work_queue.push(std::function<void()>(f)); //how do i submit something like A.fun(v) ?
}
}
Now i need to submit a task which is a member function of a templatized class in the queue
template<class T>
class A
{
private:
int x ;
public:
void fun(std::vector<std::string> & items)
{
//do somehting with items.
x = 5; // modify the members.
}// please note that i need to modify members in this function in submitted thread.
};
so finally i need something like-
thread_pool tp;
// a member function of class obj A (a) submitted with vector<string> v.
tp.submit(&A<int>::fun, std::ref(a), v);
the queries i have is how will the task queue signature look like to execute above mentioned task?
How would I need to change the thread_pool class in order to run this templatized member function?How can I call the submit Function in my code?
I saw a similar question here but still wondering about it.
An example of the same would really be helpful.
Thank you very much for your help.
You may use lambda:
thread_pool tp;
A<int> a;
td::vector<std::string> v;
tp.submit([&]() { a.fun(v); });
Note: you have to make sure that a, v live long enough.
I'm trying to make a thread to callback a function of the object that created the thread. But it seems it is not posible to pass "this" as a parameter. Is there a way to implement this? Thanks in advance.
Helper.cpp
void Helper::ProcessSomething(void (*callback)(void))
{
boost::this_thread::sleep(boost::posix_time::seconds(1));
callback();
}
SomeClass.cpp
void SomeClass::Start(void)
{
Helper *helper = Helper();
boost::thread t(&Helper::ProcessSomething, helper, &this->SomeCallback);
t.join();
}
void SomeClass::SomeCallback(void)
{
std::cout << "Callback called" << std::endl;
}
The problem is that SomeCallback is not static (at least not that I can see), so there is another this unaccounted for in thread's constructor. Further, because it's not static, you can't convert SomeCallback to the void(*)(void) that ProcessSomething requires.
The simplest fix would just be to make SomeCallback static (and change &this->SomeCallback to &SomeClass::SomeCallback). But if you can't, this is a possible workaround (I'm assuming you don't have C++11):
void Helper::ProcessSomething(boost::function<void()> callback)
{
boost::this_thread::sleep(boost::posix_time::seconds(1));
callback();
}
// ...
void SomeClass::Start(void)
{
Helper *helper = Helper();
boost::function<void()> callback = boost::bind(&SomeClass::SomeCallback, this);
boost::thread t(&Helper::ProcessSomething, helper, callback);
t.join();
}
If you do have C++11 (but want to use boost::thread anyway), you could use a lambda instead of a binding:
void SomeClass::Start(void)
{
Helper *helper = Helper();
boost::thread t(&Helper::ProcessSomething, helper, [this]() { SomeCallback(); });
t.join();
}