Reply by Bernhard Holzmayer●June 1, 20042004-06-01
Nicrosoft wrote:
> Hi, All,
>
> I want to pass a parameter to the run() in the Thread.Just like
> this:
>
> class Worker : public VDK::Thread
> {
> public:
> Worker(VDK::Thread::ThreadCreationBlock&,int myaccount);
> virtual ~Worker();
> virtual void Run();
> virtual int ErrorHandler();
> static VDK::Thread* Create(VDK::Thread::ThreadCreationBlock&);
>
> //Here is the parameter I want.
> int account;
> ///////////////////////////////////////
>
> };
>
>
> In the run()
>
> void
> Worker::Run()
> {
> /* TODO - Put the thread's "main" Initialzation HERE */
>
> printf("My account is %\n\n",account);
>
> if(account<5)
> {}
> else
> {}
>
> }
>
> The question is How can I pass the value to account when I using
> VDK:: CreateThread(kWorker)?
>
> Thanks.
It depends, who wants to pass the value.
If it's the managing thread, it has complete control.
Before it launches the worker thread, it might set the account value
using an additional set() method which you may implement.
If the thread runs automatically after creation, a static account
parameter in combination with a static method could help.
Another approach uses a semaphore:
Holding the worker thread just in the beginning of the run() method
by pend-on-semaphore, might give you control over the variable.
As soon as it has the correct value, the run() method is freed using
a post-semaphore command. Looks like this ...
worker::run()
{
PendSemaphore(account_is_set);
...work-with-account...
}
worker::set(const int new_account)
{
account = new_account;
PostSemaphore(account_is_set);
}
If different threads may write the account value, you'd better
make sure that they cannot interfere.
However, that would exceed the normal worker-thread situation.
Instead of a semaphore you might use an event flag if you work with
VDK.
Another approach would use messages, but to my opinion it doesn't
fit to the worker thread pattern, which you adopted.
And - it seems to be too expensive (ressources) for the tiny effect
which you need.
HTH,
Bernhard
Reply by Nicrosoft●May 31, 20042004-05-31
Hi, All,
I want to pass a parameter to the run() in the Thread.Just like this:
class Worker : public VDK::Thread
{
public:
Worker(VDK::Thread::ThreadCreationBlock&,int myaccount);
virtual ~Worker();
virtual void Run();
virtual int ErrorHandler();
static VDK::Thread* Create(VDK::Thread::ThreadCreationBlock&);
//Here is the parameter I want.
int account;
///////////////////////////////////////
};
In the run()
void
Worker::Run()
{
/* TODO - Put the thread's "main" Initialzation HERE */
printf("My account is %\n\n",account);
if(account<5)
{}
else
{}
}
The question is How can I pass the value to account when I using
VDK:: CreateThread(kWorker)?
Thanks.