LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2010, 02:47 AM   #1
manohar
Member
 
Registered: Dec 2010
Posts: 42

Rep: Reputation: 2
Smile thread communication


Hi,

In posix multithreadings is there ny mechanism to send local data of a one thread to other thread.
 
Old 12-14-2010, 08:52 AM   #2
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
manohar,

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.

GLIB has a GAsyncQueue API that may be of interest glib-Asynchronous-Queues

Hope that gives you something to work with.

James,
 
1 members found this post helpful.
Old 12-14-2010, 08:57 AM   #3
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hi,

Quote:
Originally Posted by manohar View Post
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?

Best regards,
Andi
 
Old 12-15-2010, 04:16 AM   #4
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Hi,

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...

Pls give ur valuable options...!!
 
Old 12-15-2010, 08:22 AM   #5
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
manohar,

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.

James,
 
Old 12-15-2010, 08:42 AM   #6
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Wink

skoona,

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...!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
[SOLVED] Inter Thread Communication with Pthreads using Message Queues. hmatadhi Linux - Embedded & Single-board computer 2 08-02-2010 12:35 AM
Fault in KDE processes communication: Could not read network communication list Magnus Johansson MEPIS 0 03-30-2008 12:50 PM
the return value of getpid() called from main thread and new thread r identical !!! cryincold Programming 3 02-29-2008 01:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration