473,385 Members | 1,642 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,385 software developers and data experts.

Thread Local Linking

I have a legacy project with hundreds of unmanaged C programs, that I am
porting to .NET. There are a lot of globally scoped variables in the C
programs, which I have had to convert to thread local, so that multiple
threads of the same program do not interfere with each other. The syntax is
like this:

system.h contains:
#define Thread __declspec(thread)

prog1.c contains:
Thread int abc;

prog2.c contains:
Thread extern int abc;

That all works fine, when all the references to the variable have the Thread
prefix. The problem is when something is missing, and it is like this:

prog1.c contains:
Thread int abc;

prog2.c contains:
extern int abc;

In this case, the compiler treats them as totally separate variables, with
separate storage, which causes the program to crash in hard-to-fix ways. I
had expected that the linker would consider the extern int abc; to be an
unresolved external, since it considers it different than the Thread int
abc;, but it does not. It links with no error or warning, then crashes when
run. It will be difficult and unreliable for me to find all the instances of
this, I was hoping that they was a way of getting the linker to detect these,
or a way of making the extern int abc; be made to refer to the Thread int,
without having to find each instance in the source code. I appreciate any
suggestions anyone has.
Nov 23 '07 #1
4 1263
It will be difficult and unreliable for me to find all the instances of
this, I was hoping that they was a way of getting the linker to detect these,
or a way of making the extern int abc; be made to refer to the Thread int,
without having to find each instance in the source code.
1. Maybe you could use the compiler for assistance rather than the linker.
You could replace all 'extern' with 'EXTERN'. Then replace all 'Thread
EXTERN' with 'Thread extern'. The remaining EXTERNs are your problem area,
and the compiler will find them all. If it is a certainty that every
remaining EXTERN should be Thread extern, then you can change them all
without using the compiler at all. But I would guess that not all extern's
should be Thread extern.

2. When you said "hundreds of unmanaged C programs", I assume you meant
hundreds of .c files. You might consider collapsing several related files
into one file containing several C functions. If done adroitly, many
extern's will disappear.

Nov 24 '07 #2
Thanks for the suggestions. Yes, there are about 1,500 C program files. I
have done what I can in terms of combining programs to eliminate externals,
but it is a multi-programmer production environment, if I combine them too
much we can't work because large portions are booked to someone to work on.

Yes, as you say, the problem is that some of them must be thread extern, but
some can not be. I wrote a program to parse the source code and convert, but
there are cases that it did incorrectly, where a line was split, or there was
a misleading comment, or something like that, it is those that I am trying to
find.

Thanks again for the assistance, and if anyone knows a way to make the
linker detect this, I would appreciate hearing about it.
"AMercer" wrote:
It will be difficult and unreliable for me to find all the instances of
this, I was hoping that they was a way of getting the linker to detect these,
or a way of making the extern int abc; be made to refer to the Thread int,
without having to find each instance in the source code.

1. Maybe you could use the compiler for assistance rather than the linker.
You could replace all 'extern' with 'EXTERN'. Then replace all 'Thread
EXTERN' with 'Thread extern'. The remaining EXTERNs are your problem area,
and the compiler will find them all. If it is a certainty that every
remaining EXTERN should be Thread extern, then you can change them all
without using the compiler at all. But I would guess that not all extern's
should be Thread extern.

2. When you said "hundreds of unmanaged C programs", I assume you meant
hundreds of .c files. You might consider collapsing several related files
into one file containing several C functions. If done adroitly, many
extern's will disappear.
Nov 25 '07 #3
Hi Richard,

We have discussed this issue internally, it's still hard to say whether or
not it's a by design behavior or issue in the compiler/linker.

Unfortunately we don't see if there's any good workaround for your scenario
since you have a large code base that are already using the "extern"
declaration whenever it's needed. We are really sorry for the inconvenience
caused.

On the other hand, if we were using separate header files to declare those
extern global variables, then we might be able to use following workaround
to make the compiler find such issue for us. For example:

// a.h
extern int abc;

// a.c
#include "a.h"
void main()
{
abc = 1;
}

// b.c
#include "a.h"
__declspec(thread) int abc;
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 28 '07 #4
Thanks Walter, I had somewhat come to that conclusion myself. I don't see how
anyone could consider it intended behavior, since it is harmful and has no
advantage, but it is what it is. Yes, your solution with a header file is
useful, I have been going through the code and cleaning up all the different
places where things are defined. Thanks again for the help.

""Walter Wang [MSFT]"" wrote:
Hi Richard,

We have discussed this issue internally, it's still hard to say whether or
not it's a by design behavior or issue in the compiler/linker.

Unfortunately we don't see if there's any good workaround for your scenario
since you have a large code base that are already using the "extern"
declaration whenever it's needed. We are really sorry for the inconvenience
caused.

On the other hand, if we were using separate header files to declare those
extern global variables, then we might be able to use following workaround
to make the compiler find such issue for us. For example:

// a.h
extern int abc;

// a.c
#include "a.h"
void main()
{
abc = 1;
}

// b.c
#include "a.h"
__declspec(thread) int abc;
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 28 '07 #5

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

Similar topics

28
by: dcrespo | last post by:
Hi all, How can I get a raised exception from other thread that is in an imported module? For example: --------------- programA.py ---------------
4
by: Steve Jorgensen | last post by:
I'm restarting this thread with a different focus. The project I'm working on now id coming along and will be made to work, and it's too late to start over with a new strategy. Still, I'm not...
7
by: MariusI | last post by:
Are objects implicitly stored in the TLS of the currently running thread? When creating multithreaded applications i get errors when accessing data from a different thread than the thread used to...
2
by: Abubakar | last post by:
Hi, Lets say I have a method called "listen_proc" inside "class1". There is another method called "start" in the same class that has to start the "listen_proc" inside a new thread. I am using...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
10
by: Jon Slaughter | last post by:
Since a thread doesn't have a Stop feature and I'm not supose to use abort, I'm wondering how I stop a thread? My problem is that I simply want to excute a function in the background and...
4
by: =?iso-8859-1?q?Eir=EDkur_Fannar_Torfason?= | last post by:
I'm wrestling with a problem that I'm hoping someone can help me with. I have a web application written in VS.2003 and running on version 1.1 of the .NET Framework on XP pro and Windows server...
4
by: sethington | last post by:
Here is my situation. I have ODBC Rights to a SQL database but I have 4 users who need to get to this information but because they are contractors they are not allowed to get there own ODBC access. ...
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.