Skip to contentSkip to conversation

Meassage Queue c++

Xenomai-help Stephan Zimmermann 1 participant 1 message Jan 18, 2006

lore
Share & tools
lore
#0 lore raw

Hello again,
still experimenting with Xenomai and C++, I did wrap a class aroud the rt_queue functions. This produces a suspicious bug wich I am unable to track down. Perhaps someone here can help.

I start two Tasks, both are instances of my Thread-class. Both Tasks call the constructor of my queue-class, creating an queue-object. The first one really creates it, the second one gets an -EEXIST error inside the constructor and does bind to the queue. Now both tasks have their own queue-object, describing the same rt_queue. (in proc everything seems good)

Now, I can write messages to the queue, wich works well. I also can read the queue from the other task without problems. Only if the queue is empty and the task attempts to read from it, my program crashes without any error- or log-message.

A Testprogram in plain C works well, so It must be s.th. with my C++ Classes.

Here is some Code (compiles without warnings):

Constructor:

BM_Queue::BM_Queue(const char* name, size_t size, size_t qlimit, int mode){
	name = name;
	size = size;
	qlimit = qlimit;
	mode = mode;	
	int err = rt_queue_create(&q,name,size,qlimit,mode);
	std::cout << "Constructor queue: create " << err << std::endl;
	if(err == -EEXIST){
		err = rt_queue_bind(&q,name,100000000);
		std::cout << "Konstruktor bm_queue: bind " << err << std::endl;
	}
	switch(err){
		case 0:				usecount++;
							break;
		default:				throw queue_exception(err);
							break;
	}
}

Recieve Function:

void* BM_Queue::rcv(unsigned long int timeout){	
	void* message;
	void* data;
	size_t size = rt_queue_recv(&q,&message,TM_INFINITE);	// blocking won't work!
	std::cout << "Recieve from queue: " << size << std::endl;
	if(size < 0){
		std::cout << "ERROR Q throwing exception: " << size << std::endl; 
		/*--- This exception is not thrown, it simply crashes ---*/
		throw queue_exception(size);
	}
	data = malloc(size);	//using operator new here (and delete there), does not 
change things
	memcpy(data,message,size);
	rt_queue_free(&q,message);
	return data;
}

Any Ideas?