I am started writing my first thread tutorial, I am creating a single thread producer and consumer. It is not completed yet, synchronization is remaining. But it is not compiling, I am not understanding what that error means what argument is missing or wrong. Below is my code.
#include<iostream>
#include<thread>
#include<sstream>
#include<list>
#include<mutex>
#include<Windows.h>
using namespace std;
#define BUCKET_LEN 5
HANDLE gMutex = NULL;
HANDLE gSemFull = NULL;
HANDLE gSemEmpty = NULL;
class producerConsumer {
long i;
list<wstring> que;
public:
producerConsumer() {
i = 0;
que.clear();
}
void produce();
void consumer();
void startProducerConsumer();
};
void producerConsumer::produce() {
std::wstringstream str;
str << "Producer[" <<"]\n";
que.push_back(str.str());
}
void producerConsumer::consumer() {
wstring s = que.front();
cout << "Consumer[" << "]";
wcout << " " << s;
que.pop_front();
}
void producerConsumer::startProducerConsumer() {
std::thread t1(&producerConsumer::produce);
std::thread t2(&producerConsumer::consumer);
t1.joinable() ? t1.join() : 1;
t2.joinable() ? t2.join() : 1;
}
int main()
{
gMutex = CreateMutex(NULL, FALSE, NULL);
if (NULL == gMutex) {
cout << "Failed to create mutex\n";
}
else {
cout << "Created Mutex\n";
}
producerConsumer p;
p.startProducerConsumer();
if (ReleaseMutex(gMutex)) {
cout << "Failed to release mutex\n";
}
else {
cout << "Relested Mutex\n";
}
gMutex = NULL;
system("pause");
return 0;
}