Solved

Create an arglist for _beginthread function

Posted on 2000-03-03
1
458 Views
Last Modified: 2008-02-01
For the beginthread function in the process library for c on windows, I am able to specify an argument list for the function I wish to call when starting the thread.  However, I can get this to work fine when there is one parameter and I pass in, say an int, just this one.  But what happens if I need to pass the function more than one parameter, e.g. an int and a char*?
I have tried to send the function a string containing the variable names created with sprintf, but when debugging the thread that has been called, the parameter values are all nonsense.
0
Comment
Question by:Greystoke
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 2580784
There are several ways to accomplish this, however themost common one is to pass a pointer to a data structure holding the information you need, e.g.

typedef struct _tagTHREAD_PARAMETER_BLOCK
{
 int nMyInt1;
 char acString1 [ 256];
 int nMyInt2;
 char acString2[ 256];
 /* whatever you need ... */
} THREAD_PARAMETER_BLOCK, *PTHREAD_PARAMETER_BLOCK;

THREAD_PARAMETER_BLOCK tpb;

tpb.nMyInt1 = 42;

strcpy ( tpb.acString1, "test");

_beginthread ( MyThread, 0, ( void*) tpb);

void MyThread ( void* pv)
{
PTHREAD_PARAMETER_BLOCK ptpb = (PTHREAD_PARAMETER_BLOCK) pv;
int n;

 n = ptpb->nMyInt1;

/* ... */
}


Feel free to ask if you need more information!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now