variable scope in threads

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
 
L

Larry Smith

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


Only if all of the code (main thread & worker threads)
is in ONE source file.

You declared 'test1' as 'static', so it is NOT global;
it's only visible within the source file where it
is declared.

One (of several) solutions might look like this:

// in main.cpp
Multi_Queue *test1;

// in worker.cpp
extern Multi_Queue *test1;

When main.cpp & worker.cpp are compiled and then
Linked together, code in 'worker.cpp' will have
access to 'test1'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,498
Latest member
yelene6679

Latest Threads

Top