C
chunghorng_lung
Hi All,
I have a question on the scope of variables for threads. The program
has a main thread which creates a few worker threads. The main thread
can access another class stored in another file, however, the worker
threads can't. I got link errors for the code inside of the worker
threads. The following contains code segments.
Multi_Queue is a class and getXXX() is a public member function inside
of Multi_Queue which is in another file.
// inside of the main thread
//global data
static Multi_Queue *test1;
.....
int abc;
abc = test1->getXXX(); // no link error
....
// create worker threads
for (i=0;i<numWorker;i++)
{
x = pthread_create(&workers, NULL, &workerThread, NULL);
...
}
}
// This is the worker thread
void *workerThread(void *inPtr)
{
int xyz = test1->getXXX(); // link error on getXXX()
}
My question is why there is no link error in the main thread but in
the worker thread it has. A global variable, in this case
Multi_Queus, created in the main thread should be available in other
threads. Appreciate any insights.
Regards,
Chung
I have a question on the scope of variables for threads. The program
has a main thread which creates a few worker threads. The main thread
can access another class stored in another file, however, the worker
threads can't. I got link errors for the code inside of the worker
threads. The following contains code segments.
Multi_Queue is a class and getXXX() is a public member function inside
of Multi_Queue which is in another file.
// inside of the main thread
//global data
static Multi_Queue *test1;
.....
int abc;
abc = test1->getXXX(); // no link error
....
// create worker threads
for (i=0;i<numWorker;i++)
{
x = pthread_create(&workers, NULL, &workerThread, NULL);
...
}
}
// This is the worker thread
void *workerThread(void *inPtr)
{
int xyz = test1->getXXX(); // link error on getXXX()
}
My question is why there is no link error in the main thread but in
the worker thread it has. A global variable, in this case
Multi_Queus, created in the main thread should be available in other
threads. Appreciate any insights.
Regards,
Chung