LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I pass a struct to a thread ?

Hi,

I'm trying to pass a structure to a thread. I have trawled these forums for an answer and have found a few suggestions relating to scaler variables but not to structs. I have even tried substituting a struct in place of a var in the ThreadSafeVar example but to no avail.

If anyone can help here I would be really gratefull,

Thanks in advance for any feedback.
0 Kudos
Message 1 of 8
(4,352 Views)

Hi,

You can pass a structure to a thread by using a void *.

example:

typedef struct
{
    int a;
}MyStruct

ThreadFunc(void *)
{

 

     memcpy(

}

0 Kudos
Message 2 of 8
(4,337 Views)

Hi,

You can pass a structure to a thread by using a void *.

example:

typedef struct
{
    int a;
}MyStruct

ThreadFunc(void *)
{

 

     memcpy(

}

0 Kudos
Message 3 of 8
(4,337 Views)

Hi,

You can pass a structure to a thread by using a void *.

example:

typedef struct
{
    int a;
}MyStruct

ThreadFunc(void *)
{

  

     memcpy(

}

0 Kudos
Message 4 of 8
(4,337 Views)



Sorry I hit tab and then the space bar and it posted.

Hi,

You can pass a structure to a thread by using a void *.

example:

typedef struct
{
    int a;
}MyStruct

ThreadFunc(void *var)
{

     MyStruct tmp;   
     memcpy(&tmp,  var, sizeof(MyStruct)) 

}

As an alternate to copy you can perform a type cast.

0 Kudos
Message 5 of 8
(4,336 Views)
Thanks for your help.

My confusion stemmed from the NI documentation on variable protection which is a bit scatty to be honest. As I don't acually need to protect the struct at this moment in time then i've simply gone for:


static struct system testSystem;
...
CmtScheduleThreadPoolFunctionAdv (
                DEFAULT_THREAD_POOL_HANDLE,
                myFunction ,
                &testSystem,
                THREAD_PRIORITY_NORMAL,
                EnableInterface,
                EVENT_TP_THREAD_FUNCTION_END,
                0,
                RUN_IN_SCHEDULED_THREAD,
                &gThreadFunctionID);       

Then pick it up in the function via:

int myFunction (void *functionData) {   
    ...
    struct system* testSystem = functionData; 
    ...
}

I believe this gives me a ref to the actual mem rather than having to make a copy of it.

Thanks.







Message Edited by steorn on 03-25-2008 11:00 AM
0 Kudos
Message 6 of 8
(4,324 Views)

I know this thread is old, but I ran into an error and need a quick clarification.

 

I have this struct...

 

 

typedef struct
{
int id; //So we reference it correctly.  Issue may arise if we support instrument with different communication type (RS2323 or USB)
int funcID;  //Can derive which type of Instrument from funcID
} device_ref_table;

 

typedef struct{

 int id;

 int funcID;

} device_ref_table;

 

I want to do the following ThreadPoolFunctionCall and pass the struct as the pointer to void

 

device_ref_table drt;

CmtScheduleThreadPoolFunction(DEFAULT_THREAD_POOL_HANDLE,InstrumentFunctions,(void*)drt,NULL);

 

BUT, I get "Cast from 'device_ref_table' to 'pointer to void' is illegal

 

On the receiving end...

I have the following which may not be correct yet, since I keep changing it.

 

int CVICALLBACK InstrumentFunctions (void *functionData)

{

device_ref_table *drt = functionData;

 

 

I don't think I want to pass the address of the struct because if I make this same call twice with different values before the Callback function finishes, I believe I will have some issues.

 

If this can't be done, or if someone thinks its easier, should I pass a 2 variable integer array?

 

Thanks in Advance.

Nick

 

 

 

0 Kudos
Message 7 of 8
(3,847 Views)

You must pass the address of the structure. As in:

 

    CmtScheduleThreadPoolFunction(DEFAULT_THREAD_POOL_HANDLE,InstrumentFunctions,(void*)&drt,NULL);

 

This is compatible with your receiving code.

 

JR

0 Kudos
Message 8 of 8
(3,843 Views)