Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 22/11/2012, à 17:48

gnomathibus

Pb d'uilisation de libsigc++ et pthread

Bonjour à tous, je débute en C++ et je suis confronté à un problème d'utilisation de la librairie libsigc++, mon but est d'avoir un callback  à partir d'un thread ,quand j’exécute mon programme, l’intérieur du thread marche bien mais lorsque la fonction  "cptr->signaldata.emit()"  est appelée il ne se passe rien, si quelqu'un d'érudit  peu m’éclairer çà serait avec joie car j'ai fais pas mal de recherches et j'ai pas trouvé beaucoup d'exemples pour mon cas avec libsigc++ et pthread, soyez indulgent avec moi svp je suis un noobs dans ce domaine
merci encore

ci dessous le code que j'utilise :

le fichier thread.h

#include <sigc++/sigc++.h>

class Thread : public sigc::trackable
{
public:

sigc::signal<void>signaldata;

Thread ();
virtual ~Thread ();

int Thread_enableChangeCallback(int);

int Thread_disableChangeCallback(int);

static void* monitorPinInterrupt(void *threadarg);

};

fichier thread.cpp

/*
 
#include "thread.h"

// constants
#define GPIO_FN_MAXLEN  32
#define POLL_TIMEOUT    10000
#define RDBUF_LEN       5



// monitoring thread data structure
struct monitor_data{
   int  thread_id;
   int  pin;
   int  edgePin;
   int  lastKnownState;
   int  running;
};

struct monitor_data monitor_data_array[21];

pthread_t threads[21];


Thread::Thread()
{

}

Thread::~Thread()
{

}

 void* Interrupt::monitorPinInterrupt(void *threadarg)
{

// obtain the monitoring data structure from the thread argument

        struct monitor_data *monitorData;
 	monitorData = (struct monitor_data *) threadarg;


	// cache a local pin value variabl
	int pin = monitorData->pin;
	int edgePin = monitorData->edgePin;

	printf("\nNATIVE (GpioInterrupt) MONITORING PIN %d @ EDGE %d\n", pin, edgePin);

    // monitoring instance variables
	char fn[GPIO_FN_MAXLEN];
	int fd,ret;
	struct pollfd pfd;
	char rdbuf[RDBUF_LEN];

	// allocate memory
	memset(rdbuf, 0x00, RDBUF_LEN);
	memset(fn, 0x00, GPIO_FN_MAXLEN);

	// attempt to access the pin state from the linux sysfs
	// (each GPIO pin value is stored in file: '/sys/class/gpio/gpio#/value' )
	snprintf(fn, GPIO_FN_MAXLEN-1, "/sys/class/gpio/gpio%d/value", edgePin);
	fd=open(fn, O_RDONLY);
	if(fd<0)
	{
		// return error; unable to get file descriptor
		// (this is likely because the pin has not been exported)
		perror(fn);
	//	return 2;

	}

	// set polling config structure
	pfd.fd=fd;
	pfd.events=POLLPRI; // High priority data may be read.

	// attempt to read the pin state from the linux sysfs
	ret=read(fd, rdbuf, RDBUF_LEN-1);
	if(ret<0)
	{
		// return error; unable to read the data file
		// (this is likely because the user has insufficient permissions)
		perror("read()");
	//	return 4;
	}

	printf("value is: %s\n", rdbuf);

	// set the running state of the instance monitor data structure
	monitorData->running = 1;

	// initialize last known value and cache the value as the last known state
	int compareLastKnown = strncmp(rdbuf, "1", 1); // only compare the first character; rdbuff may have more junk chars
	monitorData->lastKnownState = compareLastKnown;

	// continuous thread loop
	for(;;)
	{
		// clear/reset the data buffer
		memset(rdbuf, 0x00, RDBUF_LEN);

		// seek to the fist position in the data file
		lseek(fd, 0, SEEK_SET);

		// wait for data to be written to the GPIO value file
		// (timeout every 10 seconds and restart)
		ret=poll(&pfd, 1, POLL_TIMEOUT);

		// if the return value is less than '0' then
		// an error was thrown; bail out of the thread
		if(ret<0)
		{
			perror("poll()");
			close(fd);
		//	return 6;
		}

		// if the return value is equal to '0' then
		// the polling simply timed out and we can restart
		else if(ret==0)
		{
			printf("timeout\n");
			continue;
		}

		// if the return value is greater than '0' then
		// a change to the GPIO data file occurred
		else
		{
			// read the data from the file into the data buffer
			ret=read(fd, rdbuf, RDBUF_LEN-1);
			if(ret<0)
			{
				// data read error
				perror("read()");
				continue;
			}
	                printf("interrupt, value is: %s\n", rdbuf);

			// compare the data in the data buffer with the last known value state
			// (we do this to prevent double event invocation for the same value)
			int compareResult = strncmp(rdbuf, "1", 1); // only compare the first character; rdbuff may have more junk chars


			Thread* cptr = (Thread*)threadarg;
			cptr->signaldata.emit();

		
		}
	}

	// if we reached this code (unlikely),
	// then close the data file and exit the thread
	close(fd);
	return 0;
}



int Thread::Thread_enableChangeCallback (int pin)
{

	// get the index position for the requested pin number
	int index = pin;
	int edgePin = pin;

	// ensure that the requested pin index is valid
	if(index >= 0 && edgePin >= 0)
	{
		// only start this thread monitor if it has not already been started
		if(monitor_data_array[index].running <= 0)
		{
			// configure the monitor instance data
			monitor_data_array[index].thread_id = index;
			monitor_data_array[index].pin = pin;
			monitor_data_array[index].edgePin = edgePin;

			// create monitoring instance thread

			pthread_create(&threads[index], NULL,monitorPinInterrupt,static_cast<void*>(&monitor_data_array[index]));
			return 1;
		}

		// return '0' when no action was taken;
		// (monitor already running)

		return 0;

	}

	// return '-1' on error; not a valid pin
	return -1;
}

int Thread::Thread_disableChangeCallback (int pin)
{
	// get the index position for the requested pin number
	int index = pin;

	// ensure that the requested pin index is valid
	if(index >= 0)
	{
		// kill the monitoring thread
		if(monitor_data_array[index].running > 0)
		{;
			pthread_cancel(threads[index]);

			// return '1' when a thread was actively killed
			return 1;
		}

		// return '0' when no action was taken
		// (monitor is not currently active/running)
		return 0;
	}

	// return '-1' on error; not a valid pin
	return -1;
}

Le code ou j'utilise cette classe :

void IO5::Open() {

Thread thread;

thread.signaldata.connect(sigc::mem_fun(this,&IO5::Oninterrupt));

thread.Thread_enableChangeCallback(25);

}

void IO5::Oninterrupt(){

printf("Le signal !!!!\n");

}

Hors ligne