小编Var*_*eux的帖子

未定义的静态队列引用

我是C++ pthreads的新手.我正在尝试做的是使用一个线程来捕获UDP数据包并将其放入队列,另一个线程用于处理它们并在之后发送它们.我的问题是,如何在单独的线程中将元素推入/移出容器?

这是一个例子:

#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;
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,它给了我以下结果:

U53R@Foo:~/$ make
g++ -g -lpthread main.cpp -c
g++ -g -lpthread main.o -o …
Run Code Online (Sandbox Code Playgroud)

c++ static

5
推荐指数
1
解决办法
3404
查看次数

标签 统计

c++ ×1

static ×1