This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Deleting STL heap objects across threads


Sorry about that.. I was trying to hurriedly build a shorter test case
and messed up. The actual program involves a producer-consumer queue
where the producer sends a pointer to an object which has some STL
objects in it to the consumer which after processing it, deletes the
object. The Queue is protected by a pthread_mutex_lock :

	void Produce(MemoryTrace* qi)
	{
		pthread_mutex_lock(&protector);
		Q.push_back(qi);
		iTail = Q.end();
		Q.erase(Q.begin(), iHead);
		produced++;
		pthread_mutex_unlock(&protector);
	}
	
	bool Consume(MemoryTrace** qi)
	{
		pthread_mutex_lock(&protector);
		std::list<MemoryTrace*>::iterator iNext = iHead;
		++iNext;
		if (iNext != iTail)
		{
			iHead = iNext;
			*qi = *iHead;
			consumed++;
			pthread_mutex_unlock(&protector);
			return true;
		}
		pthread_mutex_unlock(&protector);
		return false;
	}

The error that i got in gdb was something similar to :

#0  0x08048856 in std::_Rb_tree<int, std::pair<int const,
std::string>, std::_Select1st<std::pair<int const, std::string> >,
std::less<int>, std::allocator<std::pair<int const, std::string> >
>::_M_begin ()

Now, after changing most of my STL containers to use the multi
threaded allocator (__gnu_cxx::_mt_alloc), it seems to run fine:

typedef __gnu_cxx::__mt_alloc<instr_id_t> instr_allocator;
typedef __gnu_cxx::__mt_alloc<std::pair<instr_id_t, instr_id_t> >
instr_pair_allocator;

typedef std::set<instr_id_t, std::less<instr_id_t>, instr_allocator>
set_of_instr_type;
typedef std::set<std::pair<instr_id_t, instr_id_t>, lessthanpair,
instr_pair_allocator> depend_graph_type;

Does that mean I can't use std::allocator in a multi threaded
application or should do something more to ensure its usage is
thread-safe ?

thanks,
Prakash




On Thu, Aug 7, 2008 at 11:37 PM, Brian Dessent <brian@dessent.net> wrote:
> Prakash Prabhu wrote:
>
>> void *f(void *arg)
>> {
>>   std::map<int, string, std::less<int> >* kk = (std::map<int, string,
>> std::less<int> >*)(kk);
>>   delete kk;
>> }
>
> It looks like you're trying to delete an uninitialized pointer.  I think
> you want (arg) not (kk) on the RHS above.
>
> Brian
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]