class EventData
{
// add members for your data and functions to access the data
// if possible use fixed sized pod members (c types, no pointers).
};
struct ThreadData
{
std::vector<EventData*> event_queue;
pthread_mutex_t mux;
bool stop;
static void threadfunc(void *);
};
int main(....)
{
...
ThreadData data;
// init data
...
pthread_create( &thread, NULL, ThreadData::threadfunc, &data);
while (true)
{
// check for user messages
...
// do idle processing
...
EventData * pEvent = NULL;
pthread_mutex_lock(&data.mux);
if (!data.event_queue.empty())
{
pEvent = data.event_queue[0];
data.event_queue.erase(data.event_queue.begin());
}
pthread_mutex_unlock(&data.mux);
if (pEvent)
HandleEvent(pEvent);
...
void ThreadData::threadfunc(void * parg)
{
ThreadData * pdata = (ThreadData *)parg;
while (!pdata->stop)
{
EventData pEvent = ReceiveEvent(...);
if (pEvent != NULL)
{
// lock the queue
pthread_mutex_lock(&pdata->mux);
pdata->event_queue.push_back(pEvent);
pthread_mutex_unlock(&pdata->mux);
}
// sleep a little amount of time
}
}