pthread with the class

I want to use pthread with the class.

#pragma once
#include "jobtype.h"
#include <stdio.h>
#include <pthread.h>
#include <queue>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#define _DEBUG_
#define MAXJOB 100

using namespace std;

typedef struct _vdata_t_
{
pthread_mutex_t mutex;
queue<job_t> jq;
} vdata_t;

class VM
{
private:
pthread_t tid;
float fUtil;
public:
int iVID;
struct timeval start;
vdata_t vdata;
public:
VM(void);
VM(int vid,int sid);
~VM(void);
int EnqJob(job_t job);
int Run(void);
static void* WorkingVM(void* data);
float GetUtil(void);
};

I put the working thread function (WorkingVM) in the class.
This VM class has a job queue. Outside of this class calls EnqJob() to push jobs, and WorkingVM will pop the queue, and handle the jobs.
So, I need a mutex on the job queue.

The problem is that it is impossible to access vdata from the thread function(WorkingVM). I cannot move out 'vdata' as a global variable since each VM instances must have its own job queue.

Any good design or solution about this?

Thank you in advance.
when you create the thread, pass this pointer as a data to the thread function. there cast it to VM class. now you can access anything in the class.
I'll try that.

Thank you so much, writetonsharma.
Topic archived. No new replies allowed.