Click to See Complete Forum and Search --> : how to change and pass thread parameter back to main
dragondad
September 19th, 2005, 11:02 AM
I have a question no solution yet, when we call pthread_create, we can pass the parameter to thread function, but how can we change the passed parameter, and pass it back to main().
If you know how to do it, please give me a short sample, or just a URL you think will be helpful.
Thanks.
Siddhartha
September 19th, 2005, 11:14 AM
I have a question no solution yet, when we call pthread_create, we can pass the parameter to thread function, but how can we change the passed parameter, and pass it back to main().
If you know how to do it, please give me a short sample, or just a URL you think will be helpful.Optionally, instead of passing parameters to pthread_create you can have a class (a singleton class) object that holds data needed to shared between the threads.
To see how Singletons are used, read this -
Singleton Design Pattern Sample (http://www.codeguru.com/forum/showthread.php?p=1232679#post1232679)
As this Design Pattern allows only one instance to exist, all threads can share data in between themselves via this one instance.
Additionally, with this Design Pattern, it will also become easy for you to manage thread synchronization to protect the data that is accessed by multiple threads.
Siddhartha
September 19th, 2005, 11:16 AM
[ moved ]
Regards,
Siddhartha
Andreas Masur
September 19th, 2005, 11:43 AM
I have a question no solution yet, when we call pthread_create, we can pass the parameter to thread function, but how can we change the passed parameter, and pass it back to main().
If you know how to do it, please give me a short sample, or just a URL you think will be helpful.
Thanks.
Well...I am basically not following here...you are passing a value to the thread and use it inside the thread...then you want to change it and pass it back? How are you passing it? As a pointer? What kind of value is it? What are you trying to accomplish?
masg
October 7th, 2005, 08:08 AM
create struct pointer if you want different data types to be updated in the worker thread or you can use pointer to required data type.
Make a structure,
typedef struct data{
int input;//data required by the worker thread
int output;//data returned back
};
pthread_create(..., pointer to the stucture);
put the output in the structure and return back.
You can get the data in the main thread, as you have passed pointer to the structure.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.