Hi i'm learning to use queue and i'm trying to make a simple read and write program. i have 2 thread 1 that sends the message got from std::cin and the other thread that waits for the queue to not be empty and prints the content when there's something in the queue. But when i execute my program the receiver receives nothing i think it's because they don't have the same object so i tried to give the reference to the object but it doesn't work i got this error message :
In file included from main.cpp:2:
/usr/include/c++/10/thread: In instantiation of βstd::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(IPC&); _Args = {IPC*}; <template-parameter-1-3> = void]β:
main.cpp:35:44: required from here
/usr/include/c++/10/thread:136:44: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
136 | typename decay<_Args>::type...>::value,
// main.cpp
#include "IPC.hpp"
#include <thread>
std::mutex mtx;
void thread_receiver(IPC ipc)
{
std::string order;
while (true) {
if (ipc.isEmpty()) {
continue;
}
else {
order = ipc.getMsg(mtx);
std::cout << "Order: " << order << std::endl;
}
}
}
void thread_sender(IPC ipc)
{
std::string order;
while (true) {
std::cout << "Enter order : ";
std::cin >> order;
ipc.sendMsg(mtx, order);
}
}
int main()
{
IPC ipc;
std::thread thread1(thread_sender, ipc);
std::thread thread2(thread_receiver, ipc);
thread1.join();
thread2.join();
return 0;
}
//IPC.hpp
#pragma once
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <queue>
#include <string>
#include <mutex>
class IPC {
public:
IPC();
~IPC();
std::string getMsg(std::mutex &m);
void sendMsg(std::mutex &m, std::string msg);
int getQueueSize();
bool isEmpty();
private:
std::queue<std::string> _order_q;
};