我正在尝试输入小写字母char并将其添加到in.buffer中,另一个线程应从in.buffer读取并使其小写并发送到out.buffer,最后一个线程应从out.buffer和打印。
我遇到错误:数字常量信号量m_free(10)之前的预期标识符; 错误:成员函数“信号量RingBuffer :: m_free(int)”的无效使用
那是为什么?
谢谢
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
struct Semaphore
{
Semaphore() = default;
Semaphore(int x) : m_s(x) {}
void release() {
std::unique_lock<std::mutex> lock(m_mut);
m_s += 1;
m_cv.notify_one();
}
void acquire() {
std::unique_lock<std::mutex> lock(m_mut);
m_cv.wait(lock,[this](){ return m_s != 0; });
m_s -= 1;
}
private:
int m_s = 0;
std::mutex m_mut;
std::condition_variable m_cv;
};
struct RingBuffer {
void write(char x);
char read();
private:
std::array<char,10> m_buff;
int m_w = 0;
int m_r = 0;
Semaphore m_free(10);
Semaphore m_taken(0);
std::mutex m_mut;
};
void RingBuffer::write(char x) {
m_free.acquire();
{
std::lock_guard<std::mutex> l(m_mut);
m_buff[m_w] = x;
m_w = m_w % 10 == 0 ? 0 : m_w + 1;
}
m_taken.release();
}
char RingBuffer::read() {
int res=-1;
m_taken.acquire();
{
std::lock_guard<std::mutex> l(m_mut);
res = m_buff[m_r];
m_r = m_r % 10 == 0 ? 0 : m_r + 1;
}
m_free.release();
return res;
}
int main()
{
//RING_SIZE;
RingBuffer in,out;
std::thread threadin([&in](){
//while(true){
char ch;
std::cin>>ch;
in.write(ch);
//}
});
std::thread threaddo([&in,&out](){
char ch=in.read();
ch=ch-32;
out.write(ch);
//}
});
std::thread threadout([&out](){
//while(true){
char ch=out.read();
std::cout<<ch<<std::endl;
//}
});
threadin.join();
threaddo.join();
threadout.join();
return 0;
}