From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

multithread

I am having trouble with threading. Inside a TCP callback I am calling CmtScheduleThreadPoolFunction(). (see attachment). Inside the function that gets called by the thread, I am calling Ini_ReadGeneric(). For the data to pass to the multithread function, I am passing ptrMem which is a pointer to a node in a linked list. The compiler is complaining about the 1st and last parms in the call to Ini_ReadGeneric(). "Left operand of -> has incompatible type 'pointer to void'".

The first is ptrMem->itScript. This is the handle to the ini structure that I created. The last is ptrMem->iHandle, the pointer to the TCP handle. What is wrong with my first and last parms?

ALSO: the help about the CmtScheduleThreadPoolFunction() says not to pass any local var or va
r that might change. Now my ptrMem will probably change if the TCP callback is called again. How do I deal with this "not change" comment???? Do I need to lock the var? How do I do it?

Thanks, Rich
0 Kudos
Message 1 of 11
(3,625 Views)
Hi Rich,
For the problem you are having with Ini_ReadGeneric function, the thing is that ptrMem parameter is passed as a void pointer. So, inside your Ini_ReadGeneric function, you have to type cast it back to whatever the type you are expecting to be passed before accessing its memebers, like itScript or iHandle.

For the second part of your problem, are you referring to "threadFunction Data" that is a void pointer? If so, what it's saying that you cannot pass the address of a variable that might not be valid when the function (threadFunction, which is a callback function) is executed. It's not saying that you cannot pass a varialble if its value changes when the function gets called. This makes sense because you always want to pass a variable that is still valid w
hen the threadFunction gets called.

Mika Fukuchi
Applications Engineering
National Instruments
0 Kudos
Message 2 of 11
(3,628 Views)
Mike thanks for taking a look at it. My problem is that I can't seem to cast it back to a value that the compiler likes. I tried this:

IniReadGeneric((IniText)(ptrMem->itScript), InputFunc, (void *)(ptrMem->iHandle))

But again it complains about Left operand of -> has incompatible type 'pointer to void' for the itScript and also for the iHandle. What am I doing wrong? How can I pass in these two values? I am actually just pass in a pointer to a structure to the ThreadFunctionScript(). Perhaps the answer to this issue and the 2nd question is to just pass in a string to the ThreadFunctionScript(), and create the ini within the thread? Again, the string that I would pass in is local to the TCP callback function so it could change, unless I lock any other
calls to it until I am done.
0 Kudos
Message 3 of 11
(3,628 Views)
Hi!
You have to type cast ptrMem to the type you are expecting. Once ptrMem is type casted, you can access its memebers. For example, if you are expecting a type named myType, you'll have to do:

((myType*)ptrMem)->itScript

BTW, my name is Mika, not Mike. I'm a girl. 🙂

Mika
0 Kudos
Message 4 of 11
(3,628 Views)
Sorry Mika, I am losing my sight from looking at these two lines of code :). Here is the def

struct strSCs{IniText itScript}strSCMember;

I have now create a var called
IniText itS;

itS=(strSCMember)ptrMem->itScript;
doesn't work
itS=(strSCMember*)ptrMem->itScript;
doesn't work
itS=((strSCMember)ptrMem)->itScript;
doesn't work
itS=((strSCMember*)ptrMem)->itScript;
doesn't work
itS=(strSCs)ptrMem->itScript;
doesn't work
itS=(IniText)((strSCMember)ptrMem->itScript);
doesn't work


What will work?????
0 Kudos
Message 5 of 11
(3,628 Views)
What type do you except your ptrMem to be? If it is strSCMember, then itS=((strSCMember*)ptrMem)->itScript; is the correct way to type cast the void pointer to the pointer to strSCMember, then access itScript member of strSCMember. WHat error do you get when you tried that?

Mika
0 Kudos
Message 6 of 11
(3,628 Views)
I get "illegal expression". The cursor appears over the ")" in "*)".

Here is the functions it stands:
int CVICALLBACK ThreadFunctionScript(void *ptrMem)
{
IniText itS;

itS=((strSCMember*)ptrMem->itScript);
}

Here is the function that calls the function above:

CmtScheduleThreadPoolFunction(DEFAULT_THREAD_POOL_HANDLE, ThreadFunctionScript, (void *) ptrMem, NULL);
0 Kudos
Message 7 of 11
(3,628 Views)
You have to do:

itS=((strSCMember*)ptrMem)->itScript;

not:

itS=((strSCMember*)ptrMem->itScript);

The first one first type casts ptrMem to strSCMember pointer, then accesses itScript of strSCMember.

The second one tries to access itScript of a void pointer ptrMem (invalid), then try to typecast the invalid thing to strSCMember pointer.

You might want to creat an easier example program, and try to play around with pointer typecasting to see how typecasting in general works.

Regards,
Mika
0 Kudos
Message 8 of 11
(3,628 Views)
Mika, no good. Is there any other way to convert this value? I am still getting Illegal expression.
0 Kudos
Message 9 of 11
(3,628 Views)
Is your structure definition a typedef? If not, you will have to do:

itS=((struct strSCs*)ptrMem)->itScript;

--
Martin
--
Martin
Certified CVI Developer
0 Kudos
Message 10 of 11
(3,628 Views)