how to pass multiple parameters in CreateThread

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

When I use the CreateThread API method, what do I need to do when I want to
pass more than one parameter where LPVOID lpParameter is passed?

Daniel
 
Hi Daniel,
When I use the CreateThread API method, what do I need to do when I
want to pass more than one parameter where LPVOID lpParameter is
passed?

Define a class or struct. Create an instance of that class and pass a
pointer
to that instance as lpParameter.
Be sure to not use a stack based instance. Either use a heap allocated
variable
or static storage. If you use static storage think carefully about
synchronizing
access of multiple threads to that data. If you use heap allocated data
don't
forget to free that data in the thread which received the data.
 
I didn't realize I even had any control over whether the variable is stack
or heap -allocated. What determines how it is allocated?

Daniel
 
Hi Daniel.
I didn't realize I even had any control over whether the variable is
stack or heap -allocated. What determines how it is allocated?

Please, start reading a book about C++ before starting to think
about writing multi threaded apps!!!

If you want to avoid thinking about memory managment go with C#.

To get you started:

struct MyData
{
int id;
std:string name;
};

void foo()
{
MyData d; // this is allocated on the stack
MyData* pd = new MyData; // this is allocated on the heap

d.id = 42;
d.name = "I will die when this scope is left";

pd->id = 43;
pd->name = "I live on the stack, so care about calling delete somewhere";

// if you would store the address of d and use it after foo is left
// you would access random data

// you can pass around pd in your process as you like but be sure
// to synchronize access when multiple threads are around and think
// about the correct place where you call delete to avoid leaking memory
}
 
ok. Thanks.

SvenC said:
Hi Daniel.


Please, start reading a book about C++ before starting to think
about writing multi threaded apps!!!

If you want to avoid thinking about memory managment go with C#.

To get you started:

struct MyData
{
int id;
std:string name;
};

void foo()
{
MyData d; // this is allocated on the stack
MyData* pd = new MyData; // this is allocated on the heap

d.id = 42;
d.name = "I will die when this scope is left";

pd->id = 43;
pd->name = "I live on the stack, so care about calling delete somewhere";

// if you would store the address of d and use it after foo is left
// you would access random data

// you can pass around pd in your process as you like but be sure
// to synchronize access when multiple threads are around and think
// about the correct place where you call delete to avoid leaking memory
}
 
What function do I use to allow the thread function to temporarily
relinguish control of the cpu so that another thread could get a time slice
of the cpu?

Daniel
 
Daniel said:
What function do I use to allow the thread function to temporarily
relinguish control of the cpu so that another thread could get a time
slice of the cpu?


Sleep()

Mark
 
Daniel said:
What function do I use to allow the thread function to temporarily
relinguish control of the cpu so that another thread could get a time slice
of the cpu?

Daniel:

Why would you need to? The OS will take care of letting all your threads run.
 
Wow. I didn't realize it would be that easy. It even requires only one
simple parameter. Thanks.
 
I thought if it was a long process then it would put a freeze on the user
interface so that the user could not interact with it.

Daniel
 
Daniel said:
I thought if it was a long process then it would put a freeze on the user
interface so that the user could not interact with it.

Daniel:

No, that is what happens if you do *not* use a secondary thread. Stopping the
GUI from freezing is the major reason for using a secondary thread.
 
Daniel said:
Wow. I didn't realize it would be that easy. It even requires only one
simple parameter. Thanks.


It's simple, and rarely necessary :)

I recommend NOT getting in the habit of using it. It is NOT a thread
synchronization function. There are special objects for that :)

Cheers,
Mark
 
What are the special objects?

Daniel

Mark Salsbery said:
Daniel said:
Wow. I didn't realize it would be that easy. It even requires only one
simple parameter. Thanks.


It's simple, and rarely necessary :)

I recommend NOT getting in the habit of using it. It is NOT a thread
synchronization function. There are special objects for that :)

Cheers,
Mark
 
I found documentation in MSDN about using variables in threads of a
multithreaded environment that says:

"Because each thread has its own stack, you can avoid potential collisions
over data items by using as little static data as possible. Design your
program to use automatic stack variables for all data that can be private to
a thread. "

How are automatic stack variables created?

Daniel

What does it mean
 
I just did some research. It seems that automatic stack variables are
basically variables of local scope that is allocated automatically and on a
stack. It's what Sven said is opposed to a heap allocated variable. Is
that not correct?

Daniel
 
Hi Daniel,

first: please do not top post with a full quote of the former post.
Pick those sentences you answer to and put your answer or question
underneath that sentence. That helps keeping track of the real
questions. Delete the rest of the post which is not relevant any more.
Just look below: your sentence is just a fragment which does not
make any sense on its own anymore. People would need to read all
the old posts to get the content again - not many do -> post ignored
I just did some research. It seems that automatic stack variables are
basically variables of local scope that is allocated automatically
and on a stack. It's what Sven said is opposed to a heap allocated
variable. Is that not correct?

Yes automatic stack variables are value types in a scope and they can
only be used in that scope and deeper but not on outside scopes.

Typically you find errors like returning a stack variable by reference
or by pointer from a function to the caller.
int* foo()
{
int i = 0; // this is on the stack
int* p = &i; // this is a pointer on the stack
*p = 42; // now i will contain the value 42
return p;
}

The caller of foo would get a pointer to an int on a stack location
which is not valid any more.

So whenever you want to pass data from an inner scope to an outer
scope or between threads you will need heap or some global data.
But accessing global or heap data concurrently from different threads
poses risks. A typical case two thread increment a global variable:
GLOBAL contains value 1 when we start
Thread A reads the value of GLOBAL and finds 1
(thread switch)
Thread B reads the value of GLOBAL and still finds 1
(thread switch)
Thread A increments GLOBAL by 1 and so puts 2 back
(thread switch)
Thread B does another increment by 1 but on its old value 1 so
2 is written back to GLOBAL instead of 3

So you need to synchronize access from multiple threads to that
global variable.

If on the other hand you do have private state that no other part of
your code needs to touch then it is good to use stack variables as
you do not need to synchronize access any more.

This is a complex theme and you are not the first to ask. There are
many books and I guess many code samples/articles around in the
internet. To really understand the matter I would recommend that
you just try some code samples, especially those without synchro-
nization to see practically what the results are. That helps a lot to
better understand the problems.
 
Back
Top