LinuxQuestions.org
Visit Jeremy's Blog.
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 01-14-2011, 05:29 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Passing an object to a running thread


This is a client server issue where the server creates a new thread for listening to a new client, now when the thread starts execution and during its execution the server needs to pass it an object of some X class, how's that to be done?
 
Old 01-14-2011, 06:21 AM   #2
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by Anisha Kaul View Post
This is a client server issue where the server creates a new thread for listening to a new client, now when the thread starts execution and during its execution the server needs to pass it an object of some X class, how's that to be done?
I think there are multiple ways to achieve this:

* process memory (all threads have access to the process' memory)
* shared memory

In this case it is important to notifiy the client threads that new data is available


* message passing (IPC)


* if the object of class X is already known when the server starts the new thread, you could pass in a void pointer via pthread_create(3)

Andi
 
1 members found this post helpful.
Old 01-16-2011, 11:12 PM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks Andi,

One more way IMO is to create a look up table, and store in it the message which the thread needs to handle and then to create a function with mutex locks which does the job of looking up! Then every thread will use that function in its turn to pick up the relevant message. Is it possible?

Last edited by Aquarius_Girl; 01-16-2011 at 11:13 PM.
 
Old 01-19-2011, 02:12 AM   #4
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Yes, why not! Almost everything is possible ;-) The principle that you are describing is commonly used in master-worker scenarios. The master for example receives incoming messages, put the requests of these messages into a worker queue, wakes up one/all of the worker threads and a worker thread will finally pop the request out of the queue. IMHO, the more challenging part is the notification mechanism used (e.g. pthread cond vars) ...

Hope that helps,
Andi
 
1 members found this post helpful.
Old 01-19-2011, 02:28 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks Andi, its my first time with the threads, but I am confident Thanks again.
 
  


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
[SOLVED] Passing array to thread function zak100 Programming 4 08-12-2010 11:07 AM
passing an object from one form to the other (in qt applications using c++ ). cbarmpar Programming 3 10-08-2008 03:12 PM
passing a QPainter object to widgets vratojr Programming 2 12-21-2005 02:46 AM
passing int array to thread? Thinking Programming 2 09-21-2005 11:00 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM

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

All times are GMT -5. The time now is 12:08 AM.

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