1)標頭檔中extern"C"聲明C函數,樣本如下:
//LamSemaphore.h
#ifndef LAMSEMAPHORE_H
#define LAMSEMAPHORE_H
#include <Global.h>
extern "C"{//C函式宣告
void* produce(void*);
void* consume(void *);
}
class LamSemaphore{
public:
LamSemaphore();
virtual ~LamSemaphore();
virtual void set(){};
virtual void get(){};
public:
pFun p_produce;
pFun p_consume;
};
#endif // LAMSEMAPHORE_H
2)cpp檔案中定義C函數
//LamSemaphore.cpp
#include "LamSemaphore.h"
void* produce(void *arg){
for(int i=0; i< nitems; ++i){
sem_wait(&(shared->nempty));
sem_wait(&(shared->mutex));
qDebug("producen\n");
shared->buff[i%NBUFF] = i;
sem_post(&(shared->mutex));
sem_post(&(shared->nstored));
}
}
void* consume(void* arg){
for(int i=0;i<nitems; ++i){
sem_wait(&(shared->nstored));
sem_wait(&(shared->mutex));
qDebug("consume\n");
if(shared->buff[i%NBUFF] != i){
qDebug("buff[%d] = %d",i,shared->buff[i%NBUFF]);
}
sem_post(&(shared->mutex));
sem_post(&(shared->nempty));
}
}
...