I'm new with C++ pthreads. What I'm trying to do is use one thread to catch UDP packets and put it into a queue, and another one to process them and send them after. My question is, how can I push/pop elements into/from a container in a separate thread?
Here's an example:
#include <queue>
#include <iostream>
#include <pthread.h>
#include <signal.h>
class A{
public:
    A(){
        pthread_create(&thread, NULL, &A::pushQueue, NULL);
        pthread_join(thread, NULL);
    }
    virtual ~A(){
        pthread_kill(thread, 0);
    }
private:
    static void* pushQueue(void* context){
        for(int i = 0; i < 10; i++){
            bufferInbound.push(i);
            std::cout << i << " pushed!" << std::endl;
        }
    }
    static std::queue<int> bufferInbound;
    pthread_t thread;
};
int main(){
    A* a = new A();
    return 0;
}
When I compile, it gives me the following result:
U53R@Foo:~/$ make
g++ -g -lpthread main.cpp -c
g++ -g -lpthread main.o -o this
main.o: In function `A::pushQueue(void*)':
/home/U53R/main.cpp:20: undefined reference to `A::bufferInbound'
collect2: ld returned 1 exit status
make: *** [make] Error 1
Thanks for helping.
you need to initialize the static member, add std::queue<int> A::bufferInbound; after the class or move it inside your function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With