Re: passing arguments to pthreads

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

 



2014-03-14 16:08 UTC+01:00, Daniel Hilst Selli <danielhilst@xxxxxxxxx>:
> Hi, I have a question about passing multiple arguments to pthreads, the
> big deal is where the paremeters are kept.. I see two possible
> solutions.. keep it on static variables that are never deallocated.. or
> on heap.. so here is my first question
>
> Passing local (stack) variables as arguments to thread is trouble, since
> the scope of this variables can go away before my thread returns..right?
> So forget about local variables
>
> So here is the two options I see, static vs heap...
> I'm using this model on one of my applications, is the same senario, a
> function that receives 3 ints as arguments and is called as a thread.. I
> create a little wrapper... here is the code http://pastebin.com/Air7u0YD
>
>
> How gurus does this? I free the args on threadfd wrapper since, on my
> real application can't join the thread, to be honest, is and deatached
> thread.. Is there something wrong with this strategy, it seems ugly to
> me....
>
> Cheers,

Hello,

If you don't mind making the start time of the threads a bit slower,
you can make every thread copy its data into its local stack.
You can either allocate one set of arguments on the stack of the main
and then, with a semaphore wait for the thread to copy its data before
erasing it with the data for the second thread and so on.
Or you can allocate enough memory for the arguments of all the
threads, start all the threads, and still with a semaphore wait that
all the threads copied their own data to their stack.

You can also make something in-between by allocating enough memory for
a fixed number of arguments. But it's becoming complicated to handle
for probably no gain.

But actually, I don't really see why you wouldn't join the threads.
You must not terminate the function main while the threads are
running. If you do, all the threads will be terminated.


Celelibi
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux