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.
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?
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)
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.
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) ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.