I'm working on my first multi-threaded application in CVI, and I'm running into a problem.
There are two IDs associated with a threaded function in CVI. The
Thread ID is a 3-4 digit hex number that represents a thread. The
Thread Function ID is generated when a callback function is assigned to
a thread.
I have two potential threads running (excluding main) at the same time.
Each operates its own UIR controls via generic functions. These
functions compare the thread ID (from CmtGetCurrentThreadID) with the
thread ID of the parent/calling function (again, sampled with
CmtGetCurrentThreadID). Thus, a generic function called from function A
in thread A knows to write in textbox A, and a function called from
function B in thread B knows to write in textbox B.
My problem: Sometimes CVI runs function A in thread B! This
causes the generic function to think it's being run in thread B and
update thread B's controls.
A snippit of the threaded callback function:
int CVICALLBACK TSU_test_engine (void *functionData)
{
g_TSU_thread_ID = CmtGetCurrentThreadID ();
.
.
.
GENMessage_Clear();
.
.
.
return 0;
}
Here's a sample of one of these generic functions...
void GENMessage_Clear (void)
{
int current_ID;
current_ID = CmtGetCurrentThreadID();
if ( current_ID == g_FLY_thread_ID)
{
ResetTextBox(g_main_panel, MP_TXT_FLY, "");
}
else if (current_ID == g_TSU_thread_ID)
{
ResetTextBox(g_main_panel, MP_TXT_TSU, "");
}
}
Again, this method falls apart if the thread_ID of the parent function
(TSU_test_engine) was run in the same thread as a completed parent
function (FLY_test_engine - not listed but almost identical to
TSU_test_engine). The current thread ID = g_TSU_thread_ID =
g_FLY_thread_ID!
I know what I'm doing wrong, but I don't know what to do to make things right.
Any suggestions?