MultiThread program in VC++ - c++

I am trying to do a threaded application to infinitely print a set of numbers after enqueing them. I get this error:
Error 1 error C3867: 'Test::ThreadFunc': function call missing argument list; use '&Test::ThreadFunc' to create a pointer to member.
What am I doing wrong? What is the mistake ?
#include "stdafx.h"
#include <chrono>
#include <mutex>
#include <thread>
#include <list>
class Test {
std::list<int> queue;
std::mutex m;
public:
void ThreadFunc()
{
// Loop is required, otherwise thread will exit
for (;;)
{
bool read = false;
int value;
{
std::lock_guard<std::mutex> lock(m);
if (queue.size())
{
value = queue.front();
read = true;
queue.pop_back();
}
}
if (read)
{
// send(header.data(), header.dataSize());
// send(proto.data(), proto.dataSize());
printf("Hello %d\n", value);
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void TestFunc()
{
std::thread thread(ThreadFunc);
thread.detach();
int i = 0;
// Loops only as a test example
for (;;)
{
std::lock_guard<std::mutex> lock(m);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
queue.push_back(i++);
// Queue Message(header, payload);
}
}
};
int main()
{
Test test;
test.TestFunc();
}

You're attempting to pass a pointer to a member function of a class. When you do this, there's an argument added to the function, tacitly, that is a pointer to the instance of the class that you're invoking the function on. In your case, the pointer to the class will be the this pointer.
See this for syntax reference: Start thread with member function
To answer your comment, why isn't it passed implicitly? You're not calling the function as a member of a class, you're passing the member function by pointer. This is a different, unique, situation, see this reference: Passing a member function as an argument in C++
Also, to save a little future headache, the next problem that comes up is that std::thread's constructor takes its arguments by value, so if you need to pass any arguments by reference, take a look at std::ref.

Here's the fix. This works. Thank you #mock_blatt
#include "stdafx.h"
#include <chrono>
#include <mutex>
#include <thread>
#include <list>
class Test {
std::list<int> queue;
std::mutex m;
public:
void ThreadFunc()
{
// Loop is required, otherwise thread will exit
for (;;)
{
bool read = false;
int value;
{
std::lock_guard<std::mutex> lock(m);
if (queue.size())
{
value = queue.front();
read = true;
queue.pop_back();
}
}
if (read)
{
// send(header.data(), header.dataSize());
// send(proto.data(), proto.dataSize());
printf("Hello %d\n", value);
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void TestFunc()
{
std::thread thread(std::bind(&Test::ThreadFunc, this));
thread.detach();
int i = 0;
// Loops only as a test example
for (;;)
{
std::lock_guard<std::mutex> lock(m);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
queue.push_back(i++);
// Queue Message(header, payload);
}
}
};
int main()
{
Test test;
test.TestFunc();
}

Change std::thread thread(ThreadFunc); to std::thread thread(Test::ThreadFunc, this);

Related

(C++) How to use Payload Object to imiplement thread pool?

I saw this very well implemented thread pool: https://github.com/progschj/ThreadPool. I am wondering whether I can use a payload object instead. The idea is that instead of using a function pointer, use an object to describe the payload, which always contains a run function and a promise. The main thread then wait on the future of the promise.
Here is what I got:
#include <iostream>
#include <queue>
#include <thread>
#include <future>
#include <condition_variable>
#include <mutex>
class GenericPayload {
protected:
std::promise <int> m_returnCode;
public:
virtual void run() = 0;
std::future <int> getFuture() {
return m_returnCode.get_future();
}
};
class MyPayload:public GenericPayload {
private:
int m_input1;
int m_input2;
int m_result;
public:
MyPayload(int input1, int input2):m_input1(input1), m_input2(input2) {}
void run() {
m_result = m_input1 * m_input2;
m_returnCode.set_value(0);
}
int getResult() {
return m_result;
}
};
class ThreadPool {
private:
std::queue <GenericPayload *> payloads;
std::mutex queue_mutex;
std::condition_variable cv;
std::vector< std::thread > workers;
bool stop;
public:
ThreadPool(size_t threads)
: stop(false)
{
for(size_t i = 0;i<threads;++i)
workers.emplace_back(
[this]
{
for(;;)
{
GenericPayload *payload;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->cv.wait(lock,
[this]{ return this->stop || !this->payloads.empty(); });
if(this->stop)
return;
payload = this->payloads.front();
this->payloads.pop();
}
payload->run();
}
}
);
}
void addPayLoad (GenericPayload *payload) {
payloads.push(payload);
}
~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
cv.notify_all();
for(std::thread &worker: workers)
worker.join();
}
};
int main() {
MyPayload myPayload(3, 5);
ThreadPool threadPool(2);
std::future <int> returnCode = myPayload.getFuture();
threadPool.addPayLoad(&myPayload);
returnCode.get();
std::cout << myPayload.getResult() << std::endl;
}
Is this the right way to do it though? I had to pass a pointer to the payload because 1. I want GenericPayload to be abstract and 2. std::promise is not copyable. Thx for any feedback.

Misuse of conditional variable

Could you please review and suggest what is wrong with this code?
It either crashes on line 21 (cond_var_.wait(lock); in the gc_thread_proc()) or locks on line 56 (lock.lock(); in release()).
#include <condition_variable>
#include <deque>
#include <functional>
#include <mutex>
#include <thread>
#include <vector>
#include <iostream>
class stream {
std::deque<int> pending_cleanups_;
std::mutex mut_{};
bool continue_{true};
std::thread gc_worker_;
std::condition_variable cond_var_;
void gc_thread_proc() {
while (true) {
std::vector<int> events_to_clean;
std::unique_lock<std::mutex> lock(mut_);
while (pending_cleanups_.empty() && continue_) {
cond_var_.wait(lock);
}
if (!continue_) {
break;
}
std::move(std::begin(pending_cleanups_), std::end(pending_cleanups_), std::back_inserter(events_to_clean));
pending_cleanups_.clear();
}
}
public:
explicit stream() : gc_worker_(&stream::gc_thread_proc, this) {}
void register_pending_event(int val) {
{
std::lock_guard<std::mutex> lock_guard(mut_);
pending_cleanups_.push_back(val);
}
cond_var_.notify_one();
}
void release() {
std::unique_lock<std::mutex> lock(mut_);
if (!continue_) {
return;
}
continue_ = false;
lock.unlock();
cond_var_.notify_one();
gc_worker_.join();
lock.lock();
pending_cleanups_.clear();
}
~stream() { release(); }
};
int main() {
int N=100000;
while(N--) {
std::cout << ".";
stream s;
}
std::cout << "ok";
return 0;
}
Changing order of members makes this problem go away - when cond_var_ is put before the gc_worker_ problem doesn't reproduce. But I guess it doesn't fix it just hides it somehow...
non-static data members are initialized in order of declaration in the class definition: https://en.cppreference.com/w/cpp/language/initializer_list
3) Then, non-static data members are initialized in order of declaration in the class definition.
In your case, since your std::thread member is initialized to start executing in its constructor, cv may not be initialized when it's used in gc_thread_proc. A command way to have a std::thread member is to move assign it in the class contructor, i.e.
class stream {
std::thread gc_worker_;
std::condition_variable cond_var_;
public:
stream(): {
gc_work = std::move(std::thread(&stream::gc_thread_proc, this));
}
};

C++11 thread pool example with bug

Below is some code showing a simple and short implementation of a thread pool.
The code is inspired by this post.
I compile it with clang++ -std=c++11 threadpool.cpp -o threadpool -lpthread
When executed I got following:
./threadpool
terminate called without an active exception
As I see, the problem is getting out of function pool_t::pop() and its infinite loop.
My question is, how to get elegantly out of the loop?
the forgotten code - my apologies -
#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <functional>
#include <condition_variable>
struct tasks_t
{
std::queue<std::function<void()>> queue;
std::mutex mutex;
};
struct threads_t
{
std::vector<std::thread> vector;
std::condition_variable condition;
};
struct pool_t
{
tasks_t tasks;
threads_t threads;
void pop()
{
while(true)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(tasks.mutex);
threads.condition.wait(lock,[this]{return !tasks.queue.empty();});
task = tasks.queue.front();
tasks.queue.pop();
}
task();
}
}
void push(std::function<void()> function)
{
{
std::unique_lock<std::mutex> lock(tasks.mutex);
tasks.queue.push(function);
}
threads.condition.notify_one();
}
void start()
{
for (int i=0,j=std::thread::hardware_concurrency(); i!=j; ++i)
{
threads.vector.push_back(std::thread(&pool_t::pop,this));
}
}
};
#include <chrono>
#include <iostream>
std::function<void()> t0 = []
{
std::cout << "t0" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
return;
};
std::function<void()> t1 = []
{
std::cout << "t1" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
return;
};
int main()
{
pool_t pool;
pool.start();
pool.push(t0);
pool.push(t1);
}
In a case such as this the easiest way is often to enqueue a task that simply throws a specific type of exception than can be caught and acted upon...
struct pool_t {
class quit_exception {};
tasks_t tasks;
threads_t threads;
void pop ()
{
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(tasks.mutex);
threads.condition.wait(lock, [this]{ return !tasks.queue.empty(); });
task = tasks.queue.front();
tasks.queue.pop();
}
try {
task();
}
catch (quit_exception &ex) {
return;
}
}
}
When you need to break out of the loop just do...
pool.push([](){ throw pool::quit_exception(); });
Depending on the precise usage you may want to make quit_exception a private type so that only pool_t itself can exit the loop in this fashion -- in its destructor for example.

Threading inside class with atomics and mutex c++

I wrote this sample program to mimic what I'm trying to do in a larger program.
I have some data that will come from the user and be passed into a thread for some processing. I am using mutexes around the data the flags to signal when there is data.
Using the lambda expression, is a pointer to *this send to the thread? I seem to be getting the behavior I expect in the cout statement.
Are the mutexes used properly around the data?
Is putting the atomics and mutexes as a private member of the class a good move?
foo.h
#pragma once
#include <atomic>
#include <thread>
#include <vector>
#include <mutex>
class Foo
{
public:
Foo();
~Foo();
void StartThread();
void StopThread();
void SendData();
private:
std::atomic<bool> dataFlag;
std::atomic<bool> runBar;
void bar();
std::thread t1;
std::vector<int> data;
std::mutex mx;
};
foo.c
#include "FooClass.h"
#include <thread>
#include <string>
#include <iostream>
Foo::Foo()
{
dataFlag = false;
}
Foo::~Foo()
{
StopThread();
}
void Foo::StartThread()
{
runBar = true;
t1 = std::thread([=] {bar(); });
return;
}
void Foo::StopThread()
{
runBar = false;
if(t1.joinable())
t1.join();
return;
}
void Foo::SendData()
{
mx.lock();
for (int i = 0; i < 5; ++i) {
data.push_back(i);
}
mx.unlock();
dataFlag = true;
}
void Foo::bar()
{
while (runBar)
{
if(dataFlag)
{
mx.lock();
for(auto it = data.begin(); it < data.end(); ++it)
{
std::cout << *it << '\n';
}
mx.unlock();
dataFlag = false;
}
}
}
main.cpp
#include "FooClass.h"
#include <iostream>
#include <string>
int main()
{
Foo foo1;
std::cout << "Type anything to end thread" << std::endl;
foo1.StartThread();
foo1.SendData();
// type something to end threads
char a;
std::cin >> a;
foo1.StopThread();
return 0;
}
You ensure that the thread is joined using RAII techniques? Check.
All data access/modification is either protected through atomics or mutexs? Check.
Mutex locking uses std::lock_guard? Nope. Using std::lock_guard wraps your lock() and unlock() calls with RAII. This ensures that even if an exception occurs while within the lock, that the lock is released.
Is putting the atomics and mutexes as a private member of the class a good move?
Its neither good nor bad, but in this scenario, where Foo is a wrapper for a std::thread that does work and controls the synchronization, it makes sense.
Using the lambda expression, is a pointer to *this send to the thread?
Yes, you can also do t1 = std::thread([this]{bar();}); to make it more explicit.
As it stands, with your dataFlag assignments after the locks, you may encounter problems. If you call SendData twice such that bar processes the first one but is halted before setting dataFlag = false so that the second call adds the data, sets the flag to true only to have bar set it back to false. Then, you'll have data that has been "sent" but bar doesn't think there's anything to process.
There may be other tricky situations, but this was just one example; moving it into the lock clears up that problem.
for example, your SendData should look like:
void Foo::SendData()
{
std::lock_guard<std::mutex> guard(mx);
for (int i = 0; i < 5; ++i) {
data.push_back(i);
}
dataFlag = true;
}

std::thread access to a function loaded from a shared library

On Ubuntu, I have a shared library mylibrary.so, with a function AlphaFunction. I want to load this function in C++ using dlopen, and then call it in two different threads. However, this is giving me run-time errors, presumably because the two threads are both trying to access the same memory where the function is stored.
The library itself controls a robot arm via USB, and the actual run-time error I get is: LIBUSB_ERROR_NO_DEVICE returned by the Write operation.
I know how to use std::atomic for dealing with shared variables, but what about a shared function?
For example:
void Foo(int (*FooFunction)())
{
while(true)
{
FooFunction();
}
}
void Bar(int (*BarFunction)())
{
while(true)
{
BarFunction();
}
}
int main()
{
void* api_handle = dlopen("mylibrary.so", RTLD_NOW|RTLD_GLOBAL);
int (*MoveRobot)() = (int (*)()) dlsym(api_handle, "Move");
std::thread t1(Foo, MoveRobot);
std::thread t2(Bar, MoveRobot);
t1.join();
t2.join();
return 0;
}
I've had a look at the comments. Here's a solution that covers all concerns:
the robot library is not thread safe, and
all calls to the robot library must be on the same thread
This answer proposes a solution in which a third thread is started up which acts as the robot request marshaller. The other threads post tasks to this thread's queue, which are executed one at a time, with the result of the call being returned via a future on which the caller can wait.
#include <thread>
#include <mutex>
#include <queue>
#include <future>
#include <functional>
// these definitions here just to make the example compile
#define RTLD_NOW 1
#define RTLD_GLOBAL 2
extern "C" void* dlopen(const char*, int);
extern "C" void* dlsym(void*, const char*);
struct RobotCaller final
{
RobotCaller()
{
_library_handle = dlopen("mylibrary.so", RTLD_NOW|RTLD_GLOBAL);
_Move = (int (*)()) dlsym(_library_handle, "Move");
// caution - thread starts. do not derive from this class
start();
}
void start()
{
_robot_thread = std::thread([this]{
consume_queue();
});
}
~RobotCaller() {
if (_robot_thread.joinable()) {
std::unique_lock<std::mutex> lock(_queue_mutex);
_should_quit = true;
lock.unlock();
_queue_condition.notify_all();
_robot_thread.join();
}
// close library code goes here
}
std::future<int> Move()
{
return queue_task(_Move);
}
private:
void consume_queue() {
;
for(std::unique_lock<std::mutex> lock(_queue_mutex) ; !_should_quit ; lock.lock()) {
_queue_condition.wait(lock, [this]{
return _should_quit || (!_task_queue.empty());
});
if (!_task_queue.empty()) {
auto task = std::move(_task_queue.front());
_task_queue.pop();
lock.unlock();
task();
}
}
}
std::future<int> queue_task(int (*f)())
{
std::packaged_task<int()> task(f);
auto fut = task.get_future();
std::unique_lock<std::mutex> lock(_queue_mutex);
_task_queue.push(std::move(task));
return fut;
}
private:
// library management
void* _library_handle = nullptr;
int (*_Move)() = nullptr;
// queue management
std::thread _robot_thread;
std::queue<std::packaged_task<int()>> _task_queue;
bool _should_quit = false;
std::mutex _queue_mutex;
std::condition_variable _queue_condition;
};
void Foo(std::function<std::future<int>()> FooFunction)
{
while(true)
{
// marshal the call onto the robot queue and wait for a result
auto result = FooFunction().get();
}
}
void Bar(std::function<std::future<int>()> BarFunction)
{
while(true)
{
// marshal the call onto the robot queue and wait for a result
auto result = BarFunction().get();
}
}
int main()
{
RobotCaller robot_caller;
std::thread t1(Foo, std::bind(&RobotCaller::Move, &robot_caller));
std::thread t2(Bar, std::bind(&RobotCaller::Move, &robot_caller));
t1.join();
t2.join();
return 0;
}