Solved

Create an arglist for _beginthread function

Posted on 2000-03-03
1
483 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

840 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