[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to store static data in a multi-thread program?



On Tue, 25 Dec 2001 01:55, Andrew Makhorin wrote:
> I've got a multi-thread program (in C) running under Debian/GNU Linux.
> And I need to store a pointer somewhere in a system place related to an
> particular thread in order to pass it to other routines running in the
> same thread. In other words I need to pass a pointer within a thread as
> if it would be declared as extern (i.e. not as a formal parameter), but
> in a thread safe manner. Could anyone please tell me how to do that?

Let's say we want an integer for each thread.

Have a global variable of type "int *", before spawning threads malloc enough 
memory for an int per thread and have the global variable point to it.  Then 
have each thread know it's number (in some suitable way) and use that index 
into the array.

Growing the array without locking is tricky but possible.  But let's move it 
to Debian-user as debian-devel is not the appropriate forum (debian-devel is 
about developing Debian packages not about teaching C programming).

-- 
http://www.coker.com.au/bonnie++/     Bonnie++ hard drive benchmark
http://www.coker.com.au/postal/       Postal SMTP/POP benchmark
http://www.coker.com.au/projects.html Projects I am working on
http://www.coker.com.au/~russell/     My home page



Reply to: