ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sending local data of one thread to another -- is an unusual questions. Here is something to think about related to getting the job done.
1. Define a Structure for this data you want to share.
2. Allocate a memory chunk large enough to hold that Structure.
3. Populate that structure in the memory block allocated
4. Send a pointer to that structure to the other thread, using a Pipe, Queue, or like mechanism
5. At the other thread, extract the value that are needed, and free the memory when done.
Hi,
In posix multithreadings is there ny mechanism to send local data of a one thread to other thread.
I'm not aware of such a function, but I don't think that you need to send data between threads, because all threads (usually) share a common address space and have access to the same data. Though, data created by one thread should be available to another thread (assuming that thread-local data is not used).
Though, data sending shouldn't be a problem, but you might need to communicate between threads when data is available and safe to access (race conditions). This could be done via mutexes and condition variables.
I'm not sure if this answers your questions. If not, could you please describe why you really need to send the data between threads?
Thanxs 4 d quick reply...!!. Basically my scenario looks like,
I have three threads,each have their own Linked list(LL)...Iam using these LL to post evt to other threads. Each thread is waiting on their own LL, when ever thread gets evt on their LL it serves and again go to sleep. These LL
struct ll_t {
int evt;
struct ll_t *next;
};
As u suggested in using LL to hold the entire structure...!! I cannot do that because for each event i need to send some different data...!!. Iam trying to use a void* to hold the address of the data to send. Is it the good way do like this...
A code structure like this works. It would allow you to allocate different data for each entry. If each event has only one type of data I see no problem in this approach. However, if each event could have more than one type of data, then you will need another field to specify the type of data this ll entry contains.
Code:
struct ll_t {
int evt;
int dataType; // optional
void *data;
struct ll_t *next;
};
However, I would not use a LL for this overall purpose. Adding and removing elements would seem to have its own problems. If one thread adds entries to the LL, and one and only one other thread removes entries -- that would alleviate my concerns with LL.
I would think using a Queue would handle the process of sending data in a cleaner way.
I thought of implementing like U specified, but the problem is - along with the evt i need to send the parameters...!! again these number of parameters varies for each event.
I wont find the problem with LL because i have taken care of synchronization issues, again each thread has their own LL so they only have to free the node after serving the evt.
I hope with LL we can dynamically create and free the memory, which is optimal compared to Queues...!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.