473,378 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

passing parameter to thread

How do I pass parameters to created thread..here is the code(but it
can't pass data..it uses global variable)

#include <windows.h>
#include <stdio.h>

DWORD Sum;

DWORD WINAPI Summation(LPVOID Param)
{
DWORD Upper = *(DWORD*)Param;
for(DWORD i=0; i<= Upper; i++)
Sum += i;
return 0;
}

int main(int argc, char *argv[])
{
DWORD ThreadId;
HANDLE ThreadHandle;
int Param;

if(argc != 2)
{
fprintf(stderr, "usage : %s number", argv[0]);
return 1;
}

Param = atoi(argv[1]);
if( Param < 0)
{
fprintf(stderr, "an integer >= 0 is required\n");
return 1;
}

ThreadHandle = CreateThread(
NULL, //default security attributes
0, //default stack size
Summation, //thread function
&Param, //parameter to thread function
0, //default creation flags
&ThreadId); //returns the thread identifier

if (ThreadHandle != NULL)
{
//wait for thread to finish
WaitForSingleObject(ThreadHandle, INFINITE);
//close the thread handle
CloseHandle(ThreadHandle);
printf("sum = %d\n",Sum);
}
}
Oct 6 '08 #1
8 9414
asit wrote:
>
How do I pass parameters to created thread..here is the code (but
it can't pass data..it uses global variable)

#include <windows.h>
#include <stdio.h>
Threads and windows are off-topic on c.l.c, which deals with the C
language as defined in the ISO C standards. C does not contain
threads, nor windows. Look for a group that deals with your system
(probably Windows).

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 6 '08 #2
asit wrote:
How do I pass parameters to created thread..here is the code(but it
can't pass data..it uses global variable)

#include <windows.h>
#include <stdio.h>

DWORD Sum;

DWORD WINAPI Summation(LPVOID Param)
{
DWORD Upper = *(DWORD*)Param;
for(DWORD i=0; i<= Upper; i++)
Sum += i;
return 0;
}

int main(int argc, char *argv[])
{
DWORD ThreadId;
HANDLE ThreadHandle;
int Param;

if(argc != 2)
{
fprintf(stderr, "usage : %s number", argv[0]);
return 1;
}

Param = atoi(argv[1]);
if( Param < 0)
{
fprintf(stderr, "an integer >= 0 is required\n");
return 1;
}

ThreadHandle = CreateThread(
NULL, //default security attributes
0, //default stack size
Summation, //thread function
&Param, //parameter to thread function
0, //default creation flags
&ThreadId); //returns the thread identifier

if (ThreadHandle != NULL)
{
//wait for thread to finish
WaitForSingleObject(ThreadHandle, INFINITE);
//close the thread handle
CloseHandle(ThreadHandle);
printf("sum = %d\n",Sum);
}
}
I can't understand your problem. You are passing the "Param" parameter
to your thread. What is actually your problem?
Which parameters you can't pass?
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Oct 6 '08 #3
sorry.....I didn't metion.

My objective is to pass a no of parameters..and also my child thread
needs to call some function of main thread.
Oct 6 '08 #4
asit wrote:
sorry.....I didn't metion.

My objective is to pass a no of parameters..and also my child thread
needs to call some function of main thread.
To pass several integers you could just pass the address of an
array. In the first position you would put the number of
integers in the array.

If you want to call something in the main thread you just call it.
All the functions of the main thread are available to you without any
restriction
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Oct 6 '08 #5
On Oct 6, 1:18 pm, jacob navia <ja...@nospam.comwrote:
asit wrote:
sorry.....I didn't metion.
My objective is to pass a no of parameters..and also my child thread
needs to call some function of main thread.

To pass several integers you could just pass the address of an
array. In the first position you would put the number of
integers in the array.

If you want to call something in the main thread you just call it.
All the functions of the main thread are available to you without any
restriction

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatiquehttp://www.cs.virginia.edu/~lcc-win32
Thank you....life is so easy because of this community !!!!

long live comp.lang.c community members
Oct 6 '08 #6
On 6 Oct 2008 at 8:38, asit wrote:
On Oct 6, 1:18 pm, jacob navia <ja...@nospam.comwrote:
[good advice]
Thank you....life is so easy because of this community !!!!
Because of a depressingly small subset of this community, unfortunately.

The sad truth is that the unhelpful response you got from CBF is
representative of the vast majority of the clc posting "community".

Oct 6 '08 #7
In article <ac**********************************@b38g2000prf. googlegroups.com>,
asit <li*****@gmail.comwrote:
>On Oct 6, 1:18 pm, jacob navia <ja...@nospam.comwrote:
>asit wrote:
sorry.....I didn't metion.
My objective is to pass a no of parameters..and also my child thread
needs to call some function of main thread.

To pass several integers you could just pass the address of an
array. In the first position you would put the number of
integers in the array.

If you want to call something in the main thread you just call it.
All the functions of the main thread are available to you without any
restriction

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatiquehttp://www.cs.virginia.edu/~lcc-win32

Thank you....life is so easy because of this community !!!!

long live comp.lang.c community members
Well, some of them, anyway...

Oct 6 '08 #8
In article <48**************@yahoo.com>,
senile old fool CBFalconer <cb********@maineline.netwrote:
>asit wrote:
>>
How do I pass parameters to created thread..here is the code (but
it can't pass data..it uses global variable)

#include <windows.h>
#include <stdio.h>

Threads and windows are off-topic on c.l.c, which deals with the C
language as defined in the ISO C standards. C does not contain
threads, nor windows. Look for a group that deals with your system
(probably Windows).
Way to go, Chuckie!

Oct 6 '08 #9

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

Similar topics

6
by: wiredless | last post by:
What is the advantage of passing an interface type? according to UML (visio) when i reverse engineer existing code to a UML diagram I get the error "UMLE00046: sample : - An interface cannot...
2
by: cmrchs | last post by:
Hi, Is there a way to pass a parameter to a thread ? Creating a thread is done using a ThreadStart-delegate object example : Thread thread1 = new Thread(new ThreadStart(MyFunction)) ...
3
by: Hardy Wang | last post by:
Hi, All I am wandering in 1.1 version of C#, how can I pass parameters to thread? In article http://www.yoda.arachsys.com/csharp/threadstart.html, it provides a way to pass one parameter. It...
2
by: Khamal | last post by:
need to passing parameter from 1st page to 2nd page... try to use public static void user_param(string namaku ,string umurku ) but still cannot why?? please help... -- :: KHAMAL ::
3
by: Sam Learner | last post by:
Hello everyone, I am developping an application, I create a thread for the application because it is about to download a large file, and wanted it to do it inside of a thread... Now, the function...
1
by: Joe Smith | last post by:
How to pass parameter to a Vb.Net application? ..Net Framework 1.1. Thanks, Joe
4
by: Ranginald | last post by:
Hi, I'm having trouble passing a parameter from my default.aspx page to my default2.aspx page. I have values from a query in a list box and the goal is to pass the "catID" from default.aspx...
1
by: anushree1631 | last post by:
hi I am working in C#.Net 2005. I want the whole code for Passing Parameter in Crystal report Please send it fast It's very urgent with regards, Anushree
7
by: AmiMitra | last post by:
i am having problem with passing parameter through navigateUrl property of a hyperlink. in the page where i am using the hyperlink , i am getting the data thorugh commonutility. i want to pass this...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.