473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

variable scope in threads

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[i], 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

Mar 25 '07 #1
1 4107
ch*************@hotmail.com wrote:
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[i], 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'.
Mar 25 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Brian | last post by:
If i declare a module level oledbconnection with each request for the page, the variable remains in scope? I get open.executing error message on subsequent calls for the page. I assumed ASP.Net...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
18
by: stephen | last post by:
Hi Just learning c and wonder if anyone can answer a question. I am writing a program that uses threads. These threads will call a specific function simultaneously. This specific function...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
6
by: charles_gero | last post by:
All: Quick question regarding reentrancy and C99 signals. If we have a C statement such as: variable = 123456; And the resulting compiled machine code for a particular fake platform is to...
14
by: subramanian100in | last post by:
In the following link, http://www.c-faq.com/malloc/retaggr.html The following ANSWER is given to a question(comp.lang.c FAQ list · Question 7.5a) : Whenever a function returns a pointer, make...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.