passing parameters to thread functions

T

teju

hi all,

i am doing multithreading program in VC++...can anyone please tell me
how to pass parameters to thread functions...

i mean i have used CreateThread(null,
0,LPTHREAD_START_ROUTINE(function1),NULL,0,&dwthreadId);

now i have to pass parameter to function1();

please reply

thanx
 
C

Christopher Pisz

teju said:
hi all,

i am doing multithreading program in VC++...can anyone please tell me
how to pass parameters to thread functions...

i mean i have used CreateThread(null,
0,LPTHREAD_START_ROUTINE(function1),NULL,0,&dwthreadId);

now i have to pass parameter to function1();

please reply

thanx

Multithreading is not part of standard C++ and is off topic in this
newsgroup. Your best bet is to ask on one of the miscrosoft.public groups.
If that fails, then examine the LPVOID lparam argument of CreateThread more
closely. MSDN gives very good examples of how to do exactly what you are
asking. It took me 3 seconds to google "createthread" and find the answer.
 
T

teju

Multithreading is not part of standard C++ and is off topic in this
newsgroup. Your best bet is to ask on one of the miscrosoft.public groups.
If that fails, then examine the LPVOID lparam argument of CreateThread more
closely. MSDN gives very good examples of how to do exactly what you are
asking. It took me 3 seconds to google "createthread" and find the answer.

thanx will try that
 
T

teju

Multithreading is not part of standard C++ and is off topic in this
newsgroup. Your best bet is to ask on one of the miscrosoft.public groups.
If that fails, then examine the LPVOID lparam argument of CreateThread more
closely. MSDN gives very good examples of how to do exactly what you are
asking. It took me 3 seconds to google "createthread" and find the answer.

thanx will try that
 
C

Chris Thomasson

[...]

You generally pass a thread-descriptor to the entry-function and use the
state contained therein which can contain references to all the information
the thread needs to proceed.

Here is super-simplistic example:

http://groups.google.com/group/comp.lang.c++/msg/3f52dbfe4a516e3c

http://groups.google.com/group/comp.lang.c++/msg/7391723c5dc8d199

You can apply RAII to than example to get a thread join on scope-exit by
using:

http://appcore.home.comcast.net/~appcore/vzoom/raii/scope-guard.cpp


Any questions?
 
J

James Kanze

Multithreading is not part of standard C++ and is off topic in
this newsgroup.

Not really. Multithreading isn't addressed by the standard yet,
but it will be real soon now, and there are certainly platform
independant, C++ questions which are related to multithreading.
Including this one, in fact---although teju formulated it in
terms of a Windows function, pthread_create, under Posix works
pretty much the same way.
Your best bet is to ask on one of the miscrosoft.public groups.

Which is likely to result in a wrong answer, which depends on a
bug in VC++ in order to work:).
If that fails, then examine the LPVOID lparam argument of
CreateThread more closely. MSDN gives very good examples of
how to do exactly what you are asking. It took me 3 seconds to
google "createthread" and find the answer.

A better solution would be to use a third party portable
threading library. Something like boost::threads, for example.
(I'm not particularly thrilled by the Boost interface to
threads, but most of the other libraries I've seen are even
worse---generally significantly worse.)

As for MSDN the examples I found there all use compiler
extensions. I'm not sure whether this is necessary, however; it
certainly isn't necessary with pthread_create, nor if you use
the boost::threads interface---although the implementation
obviously does contain more than a few platform dependencies,
that's not your problem.
 
J

James Kanze

"teju" <[email protected]> wrote in message
You can apply RAII to than example to get a thread join on
scope-exit by using:

Be very, very careful about this. I would be leary of doing a
join in a destructor, for two reasons:

-- If you're joining, you're probably recovering thread status
as well. Which means some sort of error reporting.
Destructors are not a good place for detecting errors which
need handling.

-- Join only returns when the thread terminates. If the
sub-thread hangs, then the joining thread will hang in the
destructor. In some cases, this may be the best you can do,
but it's certainly not something you want as a default
behavior. (I'm not sure what the default behavior should
be. Handling a thread that won't stop is tricky business.)
 
C

Chris Thomasson

Be very, very careful about this.

Very careful indeed! Thanks for making the valid points:

I would be leary of doing a
join in a destructor, for two reasons:
-- If you're joining, you're probably recovering thread status
as well. Which means some sort of error reporting.
Destructors are not a good place for detecting errors which
need handling.
-- Join only returns when the thread terminates. If the
sub-thread hangs, then the joining thread will hang in the
destructor. In some cases, this may be the best you can do,
but it's certainly not something you want as a default
behavior. (I'm not sure what the default behavior should
be. Handling a thread that won't stop is tricky business.)

Well, I am not sure about threads that hang/deadlock; IMO, that is a clear
sign of an error in the synchronization scheme. Of course you can explicitly
program your threads to hang, however, if they do it by themselves, that's
another story...

;^)

I think your talking about joining threads that you did not program (e.g.,
joining a thread created be a third-party library). Alls you can do is cross
your fingers and hope that the designers of the library knew what there were
doing.
 
J

James Kanze

Very careful indeed! Thanks for making the valid points:
Well, I am not sure about threads that hang/deadlock; IMO,
that is a clear sign of an error in the synchronization
scheme. Of course you can explicitly program your threads to
hang, however, if they do it by themselves, that's another
story...

Yes. I was pressed for time when I wrote that, and didn't
formulate it really well. Basically, the problem I was
worrying about was along the lines of:

start_thread_which_does_very_long_calculations() ;
do_something_in_parallel() ;
join_with_child_thread() ;

My issue is what happens if somewhere in
do_something_in_parallel(), an exception is raised. If you put
the join in a scope guard, then you block the propagation of the
error until the child thread has finished. While I certainly
wouldn't use exceptions to propagate an error which must be
handled in hard real time, somehow, blocking propagation for,
say 15 minutes, or an hour, or whatever, doesn't appeal to me
either.

The problem isn't necessarily so much that the thread really
hangs, in the absolute sense, but that it continues as if
nothing were wrong, and the parent thread rests blocks until it
finishes.

This is also one of the things I don't like about boost threads.
If you're using boost::threads in the above scenario, the
exception is propagated, because boost::thread will detach the
child thread. But the child thread continues working, possibly
taking up resources, etc. And of course, you can't do a clean
shutdown as long as a thread is running, and because the thread
has been detached, and because it wasn't designed to be used as
a detached thread, you probably have no way of knowing whether
it is still running or not.
I think your talking about joining threads that you did not
program (e.g., joining a thread created be a third-party
library). Alls you can do is cross your fingers and hope that
the designers of the library knew what there were doing.

Which is rarely the case. I've more or less given up on clean
shutdown when third party libraries use threads. I just design
my application so that "kill -9" won't ever leave corrupt data,
and count on the OS to clean up everything else. (I have to do
this anyway: if the program can survive a power failure in the
middle of a critical disk write---and it can---then "kill -9" is
no problem either.)

My comments were meant more to address the issues that you have
to consider when you master both threads. The original posting
seemed to suggest that scope guard could handle the join without
any other considerations, which is very naïve. (A scope guard
which behaves differently according to whether you've committed
or not might be useful in some cases, however.) The actual
issues are very complex, and far beyond what I can really
explain in a simple posting; I was only trying to hint at some
of the problems that you need to address. While not your case,
I have the impression that a lot of people fail to think of
these things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top