473,466 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to pass multiple parameters in CreateThread

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
Jul 22 '08 #1
17 6778
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.

--
SvenC

Jul 22 '08 #2
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

"SvenC" <Sv***@nospam.nospamwrote in message
news:18**********************************@microsof t.com...
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.

--
SvenC

Jul 22 '08 #3
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
}
--
SvenC
Jul 22 '08 #4
ok. Thanks.

"SvenC" <Sv***@nospam.nospamwrote in message
news:82**********************************@microsof t.com...
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
}
--
SvenC

Jul 22 '08 #5
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

"SvenC" <Sv***@nospam.nospamwrote in message
news:82**********************************@microsof t.com...
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
}
--
SvenC

Jul 22 '08 #6

"Daniel" <ne******@cableone.netwrote in message
news:es**************@TK2MSFTNGP05.phx.gbl...
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

--
Mark Salsbery
Microsoft MVP - Visual C++

>
Daniel

"SvenC" <Sv***@nospam.nospamwrote in message
news:82**********************************@microsof t.com...
>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
}
--
SvenC

Jul 22 '08 #7
Daniel wrote:
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.

--
David Wilkinson
Visual C++ MVP
Jul 22 '08 #8
Wow. I didn't realize it would be that easy. It even requires only one
simple parameter. Thanks.

"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in message
news:Op**************@TK2MSFTNGP04.phx.gbl...
>
"Daniel" <ne******@cableone.netwrote in message
news:es**************@TK2MSFTNGP05.phx.gbl...
>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

--
Mark Salsbery
Microsoft MVP - Visual C++

>>
Daniel

"SvenC" <Sv***@nospam.nospamwrote in message
news:82**********************************@microso ft.com...
>>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
}
--
SvenC


Jul 22 '08 #9
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

"David Wilkinson" <no******@effisols.comwrote in message
news:ek**************@TK2MSFTNGP04.phx.gbl...
Daniel wrote:
>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.

--
David Wilkinson
Visual C++ MVP

Jul 22 '08 #10
Daniel wrote:
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.

--
David Wilkinson
Visual C++ MVP
Jul 23 '08 #11
"Daniel" <ne******@cableone.netwrote in message
news:eb**************@TK2MSFTNGP06.phx.gbl...
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

--
Mark Salsbery
Microsoft MVP - Visual C++

>
"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in
message news:Op**************@TK2MSFTNGP04.phx.gbl...
>>
"Daniel" <ne******@cableone.netwrote in message
news:es**************@TK2MSFTNGP05.phx.gbl...
>>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

--
Mark Salsbery
Microsoft MVP - Visual C++

>>>
Daniel

"SvenC" <Sv***@nospam.nospamwrote in message
news:82**********************************@micros oft.com...
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
}
--
SvenC


Jul 23 '08 #12
What are the special objects?

Daniel

"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in message
news:eE**************@TK2MSFTNGP02.phx.gbl...
"Daniel" <ne******@cableone.netwrote in message
news:eb**************@TK2MSFTNGP06.phx.gbl...
>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

--
Mark Salsbery
Microsoft MVP - Visual C++

>>
"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in
message news:Op**************@TK2MSFTNGP04.phx.gbl...
>>>
"Daniel" <ne******@cableone.netwrote in message
news:es**************@TK2MSFTNGP05.phx.gbl...
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

--
Mark Salsbery
Microsoft MVP - Visual C++

Daniel

"SvenC" <Sv***@nospam.nospamwrote in message
news:82**********************************@micro soft.com...
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
}
>
>
--
SvenC



Jul 23 '08 #13
"Daniel" <ne******@cableone.netwrote in message
news:uu**************@TK2MSFTNGP06.phx.gbl...
What are the special objects?

DLLs, Processes, and Threads: Synchronization
http://msdn.microsoft.com/en-us/libr...53(VS.85).aspx
Mark

--
Mark Salsbery
Microsoft MVP - Visual C++
>
Daniel

"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in
message news:eE**************@TK2MSFTNGP02.phx.gbl...
>"Daniel" <ne******@cableone.netwrote in message
news:eb**************@TK2MSFTNGP06.phx.gbl...
>>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

--
Mark Salsbery
Microsoft MVP - Visual C++


Jul 23 '08 #14
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
"SvenC" <Sv***@nospam.nospamwrote in message
news:18**********************************@microsof t.com...
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.

--
SvenC

Jul 26 '08 #15
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

"Daniel" <ne******@cableone.netwrote in message
news:uH****************@TK2MSFTNGP04.phx.gbl...
>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
"SvenC" <Sv***@nospam.nospamwrote in message
news:18**********************************@microsof t.com...
>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.

--
SvenC


Jul 26 '08 #16
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.

--
SvenC
Jul 26 '08 #17
thanks
Jul 27 '08 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bore Biko | last post by:
I don't know hopw to do this, like "printf", or "sprintf", I know they use a stack but how to make my own...?? Thanks in advance !! robert.bralic@si.t-com.hr
1
by: moondaddy | last post by:
This is a basic question but I cant seem to find the answer anywhere and have never tried it before. Here's one of many syntax I'm trying, but should at least show you what I'm trying to do: ...
1
by: Steven K | last post by:
Hello, I am calling a SQL Server 2K parameter query with the following: Dim spWebDocGroup As OleDb.OleDbDataReader Dim prmWebDocGroup As OleDbParameter Dim cmdWebDocGroup As New...
1
by: Greg Reynolds | last post by:
Hi, I need to create datagrid hyperlink columns with multiple parameters in the URL. In my research on how to do this, it is recommended to create a template column which I have done. The code...
1
by: LB | last post by:
Hello everybody Using .net 2003 & vb,asp How can i pass multiple parameters in the URL Hyperlink for a datagrid ? or is there somewhere a sample on how to accomplish that ? I know how to pass...
5
by: Code Monkey | last post by:
Is there anyway I can start a get a job done by starting a new thread with MULTIPLE parameters? operations only take a maximum of 1 parameter? I need a way of passing (at minimum) at least 3...
3
by: raylopez99 | last post by:
I suspect the answer to this question is that it's impossible, but how do I make the below code work, at the point where it breaks (marked below). See error CS0411 This is the complete code. ...
2
Subin Ninan
by: Subin Ninan | last post by:
I want to use threading in my application. This is the method which i need to call using a thread. public static string ComputeHash(string text, string salt){...} how to call this method...
0
by: yshali | last post by:
Hi, I am trying to write sql for passing multiple arguments through a function and handle NULL conditions ? here is the part of the query which throws error: ... and a.itemnum in...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.