0

(I have seen other posts on SO with this same error but I checked the recommended fixes which did not work for me ..)

I am using Windows 10 and Microsoft Visual Studio.

I am trying to self-learn multi-threading and am working on a very simple multithreading queue.

I made a queue class which has an array of size 50, and a length and count both starting at one.

I have two member functions: one adds another number to the end of the array and the other prints the first element of the array then removes it by pulling all of the elements back one index (similar to a regular queue).

My trouble here is with the threads. I want both functions running simultaneously as if the integers are customers and the array is a queue in a supermarket. The customers line up at random times and any time the queue is not empty, the cashier calls for the next person to come over.

Here is my code: (You can ignore the include of chrono, I am planning on making it more advanced by having numbers added or removed every few seconds after I get the program to work)


#include <iostream>
#include <chrono>
#include <thread>

using namespace std;

class Queue {

public:
    Queue() {
        length = 0;
        count = 0;
    }

    void add() {
        int x = 0;
        while (x < 100) {
            line[length] = count;
            ++length;
            ++count;
            ++x;
        }
    }

    void read() {
        int y = 0;
        while (y < 100) {
            if (length == 0) {
                printf("Queue empty\n");
                continue;
            }
            printf("Processed %d\n", line[0]);
            for (unsigned int i = 0; i < length - 1; ++i) {
                line[i] = line[i + 1];
            }
            --length;
            ++y;
        }

    }

private:
    static const unsigned int MAX_SIZE = 50;
    unsigned int line[MAX_SIZE];
    unsigned int length;
    unsigned int count;
};

int main()
{
    Queue test;
    thread a(test.add);
    thread b(test.read);

    return 0;
}

Here are my errors:

'Queue::add': non-standard syntax; use '&' to create a point to member
'Queue::read': non-standard syntax; use '&' to create a point to member

these occur in the two thread lines in the main function.

I have tried adding () in the thread calls, as in : thread a(test.add()) instead of thread a(test.add) but that has resulted with the error:

no instance of constructor "std::thread::thread" matches the argument list

Does anyone know how I can solve this issue ?

Thank you in advance :D

0 Answers0