LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Threads and passing parameters to it

Solved!
Go to solution

Hello everybody,

 

at the moment I`m playing with threads and I'm wondering how to pass parameter to the thread function.

I have created a thread function i.e.

 

void CVICALLBACK doThread(char id[], char value[])

{

  //do something

}

 

and I want to call the thread like this

 

CmtScheduleThreadPoolFunction(DEFAULT_THREAD_HANDLE, doThread(FIX_ARG1,FIX_ARG2), NULL, &threadID);

 

But somehow it doesn't work. How can I pass to strings to the thread function?

 

Thanks to all...

 

 

0 Kudos
Message 1 of 4
(3,930 Views)
Solution
Accepted by topic author ChrisLambert

One common way to pass assorted data to a thread is to collect it all up into a structure, then pass the address of that structure to the function which installs the thread. Then, inside the thread, cast the passed parameter back to a pointer of the same structure type and you can then reference all the data. Like:

 

typedef struct {

    int run;

    short flag;

    char [32] string;

} structThread, *pstructThread;

 

...

 

structThread tData;

 

...

 

tData.run = 1; 

CmtScheduleThreadPoolFunction (..., &tData, ...)

 

...

 

int MyThread (void *param) {

 

    pstructThread td = (pstructThread) param;

 

    while (td->run) {

     ...

    }

     return 0;

}

 

JR

0 Kudos
Message 2 of 4
(3,926 Views)

Thanks a lot,

I think this will solve my problem at the moment, but is there no other way to put more than one pointer to a thread function?

Or is the thread function limited to one input parameter?

 

Greets

Chris

0 Kudos
Message 3 of 4
(3,924 Views)

The thread definitions, even if you use Windows thread functions in the SDK, only allow one parameter. But since this one parameter can point to a structure, or an array, or an array of structures, it is not really a limitation - more of a programming paradigm. Of course, you could just use global variables instead and access them directly, inside the thread. This can get tricky to manage and is not recommended unless you are confident about what can happen.

 

JR

0 Kudos
Message 4 of 4
(3,921 Views)