ctseng
ctseng

Reputation: 13

How to get around unsupported C++11 code

The compiler at my school does not seem to support C++11 but I'm not sure how to get around this problem. I'm also not sure what's causing the last compiler error. I am attempting to compile on Linux with:

gcc -std=c++0x project2.cpp

The system does not recognize -std=c++11. Any ideas on how to get it to compile? Thank you!

#include <thread>
#include <cinttypes>
#include <mutex>
#include <iostream>
#include <fstream>
#include <string>
#include "time_functions.h"
using namespace std;

#define RING_BUFFER_SIZE 10
class lockled_ring_buffer_spsc
{
private:
    int write = 0;
    int read = 0;
    int size = RING_BUFFER_SIZE;
    string buffer[RING_BUFFER_SIZE];
    std::mutex lock;
public:

    void push(string val)
    {
        lock.lock();
        buffer[write%size] = val;
        write++;
        lock.unlock();
    }

    string pop()
    {
        lock.lock();
        string ret = buffer[read%size];
        read++;
        lock.unlock();
        return ret;
    }
};

int main(int argc, char** argv)
{
    lockled_ring_buffer_spsc queue;

    std::thread write_thread([&]() {
        start_timing();
        string line;
        ifstream myfile("p2-in.txt");
        if (myfile.is_open())
        {   
            while (getline(myfile, line))
            {
                line += "\n";
                queue.push(line);
                cout << line << " in \n";
            }
            queue.push("EOF");


            myfile.close();
            stop_timing();


        }

    }  
    );
    std::thread read_thread([&]() {
        ofstream myfile;
        myfile.open("p2-out.txt", std::ofstream::out | std::ofstream::trunc);
        myfile.clear();
        string tmp;
        while (1)
        {

            if (myfile.is_open())
            {
                tmp=queue.pop();

                cout << tmp << "out \n";
                if (tmp._Equal("EOF"))
                    break;

                myfile << tmp;
            }
            else cout << "Unable to open file";
        }
        stop_timing();
        myfile.close();
    }  
    );

    write_thread.join();
    read_thread.join();
    cout << "Wall clock diffs:" << get_wall_clock_diff() << "\n";
    cout << "CPU time diffs:" << get_CPU_time_diff() << "\n";

    system("pause");

    return 0;
}

Compiler Errors:

project2.cpp:14:14: sorry, unimplemented: non-static data member initializers
project2.cpp:14:14: error: ISO C++ forbids in-class initialization of non-const static member ‘write’
project2.cpp:15:13: sorry, unimplemented: non-static data member initializers
project2.cpp:15:13: error: ISO C++ forbids in-class initialization of non-const static member ‘read’
project2.cpp:16:13: sorry, unimplemented: non-static data member initializers
project2.cpp:16:13: error: ISO C++ forbids in-class initialization of non-const static member ‘size’
project2.cpp: In lambda function:
project2.cpp:79:13: error: ‘std::string’ has no member named ‘_Equal’

Upvotes: 1

Views: 292

Answers (2)

R Sahu
R Sahu

Reputation: 206607

It's strange that your compiler does not complain about std::mutex, which is a C++11 feature, but complains about the other C++11 features.

The lines

int write = 0;
int read = 0;
int size = RING_BUFFER_SIZE;

are valid in C++11 but not prior versions. You can initialize them in a constructor.

class lockled_ring_buffer_spsc
{
private:
    int write;
    int read;
    int size;
    string buffer[RING_BUFFER_SIZE];
    std::mutex lock;
public:
    lockled_ring_buffer_spsc() : write(0), read(0), size(RING_BUFFER_SIZE) {}

...

};

That will work as long as the compiler supports std::mutex in a pre-C++11 version. If that's not the case, you'll have to find a solution to that problem separately.

Upvotes: 0

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140188

you could replace

class lockled_ring_buffer_spsc
{
private:
    int write = 0;
    int read = 0;
    int size = RING_BUFFER_SIZE;
    string buffer[RING_BUFFER_SIZE];
    std::mutex lock;
public:

by uninitialized members (except for the static array) + a default constructor

class lockled_ring_buffer_spsc
{
private:
    int write;
    int read;
    int size;
    string buffer[RING_BUFFER_SIZE];
    //std::mutex lock;
public:
  lockled_ring_buffer_spsc() : write(0),read(0),size(RING_BUFFER_SIZE)
  {}

and

if (tmp._Equal("EOF"))

is simply

if (tmp == "EOF")

As someone noted, std::mutex only was introduced in C++ 11. You'll have to use plain C pthread_mutex_lock / pthread_mutex_unlock functions for instance.

Upvotes: 1

Related Questions