[zeromq-dev] Pass arguments via zthread_fork
Thomas Rodgers
rodgert at twrodgers.com
Tue Apr 14 15:03:24 CEST 2015
The pointer you passed is the args[], which is stack local. By the time the
spawned thread starts, that stack frame has been unwound and the two
contained char* are pointing to who knows what.
You must guarantee the lifetime of anything passed by pointer is sufficient
that the receiving thread can read valid memory which is not what you have
in this case. Guaranteeing this can get tricky, which is why the usual
guidance is to share data via message passing.
On Tuesday, April 14, 2015, Bachmair Florian - flexSolution GmbH <
florian.bachmair at flexsolution.eu> wrote:
> Hi!
>
> I have a *char[] with parameter which I wanna hand over to my subscriber
> thread.
> I tried it with my own method which needs an void-pointer as well. There I
> can read the parameters sucesffuly, but not If I start a Thread via
> zthread_fork
>
> Here's some sample code to Illustrate my problem
>
> static void fillArguments(){
> char* args[2];
> args[1]="a";
> args[2]="b";
> anyMethod(args);
> zthread_fork(context, subscriber_thread, args);
> }
> //Here it works fine
> static void anyMethod(void *args) {
> puts("anyMethod begin");
> char **strargs = (char **) args;
> puts(strargs[0]);
> puts(strargs[1]);
> fflush(stdout);
> puts("anyMethod end");
> }
> //Here I can't read the parameters, Just get weired values
> static void subscriber_thread(void *args, zctx_t *ctx, void *pipe) {
> puts("Subscribe thread");
> fflush(stdout);
>
> char **strargs = (char **) args;
>
> int i;
> puts(strargs[0]);
> puts(strargs[1]);
> fflush(stdout);
> }
>
> Thx
> Florian
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20150414/7871e098/attachment.htm>
More information about the zeromq-dev
mailing list