N
Neo
Hi,
I have come around an implementation which uses std::queues implementation namely "free" queue. Now as the queue elements are moved out and back in tofree queues I have noticed that (by debugging the corefile, also process is not attached) the virtual addresses of _M_start._M_cur and _M_finish._M_cur are bit odd i.e. _M_start._M_cur is great than _M_finish._M_cur.
Same code:
-----------------------
#include <iostream>
#include <list>
#include <queue>
#include <pthread.h>
using namespace std;
pthread_mutex_t _mutex;
queue<int> q;
queue<int> bq;
bool getLock()
{
unsigned int ret=0;
ret=pthread_mutex_lock(&_mutex);
if(ret != 0)
{
cout<<"\ngetLock()thread_mutex_lock Error:"<<ret<<endl;
return false;
}
return true;
}
bool releaseLock()
{
unsigned int ret=0;
ret=pthread_mutex_unlock(&_mutex);
if(ret != 0)
{
cout<<"\nreleaseLock()thread_mutex_unlock Error:"<<ret<<endl;
return false;
}
return true;
}
extern "C" void* popFrmQPushToBq(void * t)
{
while(1)
{
getLock();
bq.push(q.front());
q.pop();
releaseLock();
sleep(1);
}
}
extern "C" void* popFrmBqPushToQ(void * t)
{
getLock();
q.push(bq.front());
bq.pop();
releaseLock();
sleep(2);
}
int main()
{
/* Initialize the queue */
for (int i=0; i<38000; i++)
q.push(i);
//Event at this point when I try to print the values in the queue they arenot coming up correctly.
cout<<"\nAdded Items:"<<q.size();
pthread_attr_t _attr;
int retVal=pthread_attr_init(&_attr);
if(retVal!=0)
{
cout<<"\npthread_attr_init failed : "<<retVal;
}
retVal=pthread_attr_setdetachstate(&_attr, PTHREAD_CREATE_JOINABLE);
if(retVal!=0)
{
cout<<"\npthread_attr_init failed : "<<retVal;
}
pthread_t tId;
int rc = pthread_create(&tId, &_attr, popFrmQPushToBq, (void*)NULL);
if(rc != 0)
{
cout<<"\npthread creation failed : "<<rc;
rc=pthread_attr_destroy(&_attr);
if(rc != 0)
{
cout<<"\npthread destroy failed : "<<rc;
}
return -1;
}
while(1)
{
popFrmBqPushToQ(NULL);
}
}
I have tried GDB print pretty for this (script available at http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt) which fails to print the elememts in the above mentioned scenario. And even googling was not much help.
Snapshot of queue from gdb
(gdb) p q
$3 = {
c = {
<std::_Deque_base<int, std::allocator<int> >> = {
_M_impl = {
<std::allocator<int>> = {
<__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>
},
members of std::_Deque_base<int, std::allocator<int> >::_Deque_impl:
_M_map = 0x621790,
_M_map_size = 638,
_M_start = {
_M_cur = 0x605958, <<<<<<<<< Is greater
_M_first = 0x605940,
_M_last = 0x605b40,
_M_node = 0x621e48
},
_M_finish = {
_M_cur = 0x6050dc,
_M_first = 0x605010,
_M_last = 0x605210,
_M_node = 0x622780
}
}
Please let me know if there is some other way to get size of queue and its elements in gdb. Please note no process attached, so cannot use q.size() here.
Thanks
Neo
I have come around an implementation which uses std::queues implementation namely "free" queue. Now as the queue elements are moved out and back in tofree queues I have noticed that (by debugging the corefile, also process is not attached) the virtual addresses of _M_start._M_cur and _M_finish._M_cur are bit odd i.e. _M_start._M_cur is great than _M_finish._M_cur.
Same code:
-----------------------
#include <iostream>
#include <list>
#include <queue>
#include <pthread.h>
using namespace std;
pthread_mutex_t _mutex;
queue<int> q;
queue<int> bq;
bool getLock()
{
unsigned int ret=0;
ret=pthread_mutex_lock(&_mutex);
if(ret != 0)
{
cout<<"\ngetLock()thread_mutex_lock Error:"<<ret<<endl;
return false;
}
return true;
}
bool releaseLock()
{
unsigned int ret=0;
ret=pthread_mutex_unlock(&_mutex);
if(ret != 0)
{
cout<<"\nreleaseLock()thread_mutex_unlock Error:"<<ret<<endl;
return false;
}
return true;
}
extern "C" void* popFrmQPushToBq(void * t)
{
while(1)
{
getLock();
bq.push(q.front());
q.pop();
releaseLock();
sleep(1);
}
}
extern "C" void* popFrmBqPushToQ(void * t)
{
getLock();
q.push(bq.front());
bq.pop();
releaseLock();
sleep(2);
}
int main()
{
/* Initialize the queue */
for (int i=0; i<38000; i++)
q.push(i);
//Event at this point when I try to print the values in the queue they arenot coming up correctly.
cout<<"\nAdded Items:"<<q.size();
pthread_attr_t _attr;
int retVal=pthread_attr_init(&_attr);
if(retVal!=0)
{
cout<<"\npthread_attr_init failed : "<<retVal;
}
retVal=pthread_attr_setdetachstate(&_attr, PTHREAD_CREATE_JOINABLE);
if(retVal!=0)
{
cout<<"\npthread_attr_init failed : "<<retVal;
}
pthread_t tId;
int rc = pthread_create(&tId, &_attr, popFrmQPushToBq, (void*)NULL);
if(rc != 0)
{
cout<<"\npthread creation failed : "<<rc;
rc=pthread_attr_destroy(&_attr);
if(rc != 0)
{
cout<<"\npthread destroy failed : "<<rc;
}
return -1;
}
while(1)
{
popFrmBqPushToQ(NULL);
}
}
I have tried GDB print pretty for this (script available at http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt) which fails to print the elememts in the above mentioned scenario. And even googling was not much help.
Snapshot of queue from gdb
(gdb) p q
$3 = {
c = {
<std::_Deque_base<int, std::allocator<int> >> = {
_M_impl = {
<std::allocator<int>> = {
<__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>
},
members of std::_Deque_base<int, std::allocator<int> >::_Deque_impl:
_M_map = 0x621790,
_M_map_size = 638,
_M_start = {
_M_cur = 0x605958, <<<<<<<<< Is greater
_M_first = 0x605940,
_M_last = 0x605b40,
_M_node = 0x621e48
},
_M_finish = {
_M_cur = 0x6050dc,
_M_first = 0x605010,
_M_last = 0x605210,
_M_node = 0x622780
}
}
Please let me know if there is some other way to get size of queue and its elements in gdb. Please note no process attached, so cannot use q.size() here.
Thanks
Neo