Domanda

I need to know how a thread can send its ID to another thread before it goes to wait state. I thought to pass a variable with its ID but I don't know how to do it.

È stato utile?

Soluzione

If it's only one thread and its parent, you could use a global variable, as they are shared between all threads. Make it volatile in case you expect concurrent access.

EDIT: I'm not sure if you're using POSIX threads on Linux but you probably have a way to pass a pointer (e.g. to a struct) when creating the thread. It could contain a variable to store its ID or a pointer to a function to call on the parent thread. I know you can do it with Windows threads.

Altri suggerimenti

You can create a pointer in the thread which is pointing to the function in the parent (by reference). By the time it goes to wait state then it can just use that pointer to trigger something to its parent.

Threads share memory, so you can allocate a global variable and let the child write on it.

Than for the synchronization (aka inform the parent that a value has been written) you have many choices: you can use a semaphore, can send a signal from the thread back to its parent, use a synchronization variable like explained here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top