🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
10 messages
10 total messages Started by Colin B Maharaj Tue, 12 Jun 2007 10:29
Thread safety
#1825
Author: Colin B Maharaj
Date: Tue, 12 Jun 2007 10:29
27 lines
1781 bytes
Hi all, I am trying to write some multi-threading Indy(9.0.50) code
and while I have learn't a lot so far, I think I am getting into trouble
by using unsafe thread code in my app.

Here is where I have reached so far.

1. First I thought I could not use ReadLn() WriteLn() in my code
because I thought AnsiString were not thread safe. Is this still the case?

2. I definitely learn't that sprintf is unsafe and try to create
my own simple char array string function with local storage.

3. Naturally I am not updating or accessing any visual controls from
my code.

4. I am using mutex/WaitForSingleObject when a thread needs
to update a single log file.

Thing is now Code Guard is crashing with an access violation.

Is there anything else I need to know w.r.t. thread safety
that will help my design. e.g. what further functions / code 
construction to avoid?

Thanks!

Colin
Re: Thread safety
#1826
Author: AlexB
Date: Tue, 12 Jun 2007 11:56
10 lines
1059 bytes
Colin B Maharaj wrote:

> 2. I definitely learn't that sprintf is unsafe

Really ? How you have determined it ?

Can anybody else confirm it ?

-- 
Alex
Re: Thread safety
#1827
Author: Colin B Maharaj
Date: Tue, 12 Jun 2007 12:28
48 lines
2321 bytes
Well Alex, I removed all of the sprintf calls
in my code and not an error. When I started using
code guard / memproof on my initial code
I was getting thousands of errors.

The programs is a multi TCP polling client
creating 10 threads to poll data from data
buffers.

Initially I tried to rewrite some indy routines
for string sending and receiving without the use of
AnsiStrings and this seem to work but there were
still some errors (hundreds).

I then decided to remove sprintf and all went very well.

The only problem I had was code guard crashing. But when
I recompiled without code guard - no problems at all (so far).

I even reintroduced, in some of my code the use of
AnsiString with Indy routines and still no problems.

As a responsible programmer, I will have to create a test
program to illustrate. (probably later on this week).


But this is my experience.


[C++ Builder 6.0 Enterprise with Update 4]
[Indy 9.0.50]
[Win XP Professional SP2 - with all updates]
[AMD Athlon 64 X2 Dual Core]
[Core Processor 4200+]
[2.21 Ghz, 2.00 GB of RAM]
[DEP off - conflicts with Builder]
[Avast Antivirus]


AlexB wrote:
> Colin B Maharaj wrote:
> 
>> 2. I definitely learn't that sprintf is unsafe
> 
> Really ? How you have determined it ?
> 
> Can anybody else confirm it ?
> 
Re: Thread safety
#1828
Author: Alan Bellingham
Date: Tue, 12 Jun 2007 12:52
11 lines
1337 bytes
Colin B Maharaj <noreply@myhost.com> wrote:

>I then decided to remove sprintf and all went very well.

It's perfectly possible to badly misuse sprintf() - that doesn't mean
that sprintf() itself is buggy, just the way it's being used.

Alan Bellingham
-- 
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK
Re: Thread safety
#1829
Author: Colin B Maharaj
Date: Tue, 12 Jun 2007 13:25
18 lines
1826 bytes
Well I hear you, and I am aware of the kinds of mistakes
you can make while using sprintf / printf, they could
be deadly. I guess the worse mistake you can make with
these functions is calling it badly and it does not outright
access violate. This means that the pin is out in the grenade
but no one has thrown it yet!
But it is possible. What I will do by weekend is to create a
test app to illustrate what I am saying.

Alan Bellingham wrote:
> Colin B Maharaj <noreply@myhost.com> wrote:
> 
>> I then decided to remove sprintf and all went very well.
> 
> It's perfectly possible to badly misuse sprintf() - that doesn't mean
> that sprintf() itself is buggy, just the way it's being used.
> 
> Alan Bellingham
Re: Thread safety
#1830
Author: Remy Lebeau (TeamB)
Date: Tue, 12 Jun 2007 15:04
22 lines
1484 bytes
"Colin B Maharaj" <noreply@myhost.com> wrote in message
news:466e7571$1@newsgroups.borland.com...

> First I thought I could not use ReadLn() WriteLn() in my code
> because I thought AnsiString were not thread safe. Is this still
> the case?

ReadLn() and WriteLn() work just fine in threads.

> I definitely learn't that sprintf is unsafe

That is true if you are calling sprintf() in one thread while another
thread is accessing the same AnsiString at the same time.

> Thing is now Code Guard is crashing with an access violation.

You need to be more specific.  What is the exact error message?  In
what code exactly is it being thrown?


Gambit
Re: Thread safety
#1831
Author: Remy Lebeau (TeamB)
Date: Tue, 12 Jun 2007 15:06
14 lines
1368 bytes
"Colin B Maharaj" <noreply@myhost.com> wrote in message
news:466e9171@newsgroups.borland.com...

> Initially I tried to rewrite some indy routines
> for string sending and receiving without the use of
> AnsiStrings

Why did you do that?  There is nothing wrong with using AnsiString
with them, as long as you are not doing anything overly stupid with
them in your code.  They work just fine as-is.


Gambit
Re: Thread safety
#1834
Author: Clayton Arends
Date: Tue, 12 Jun 2007 22:04
12 lines
1347 bytes
You might be running into a problem I found a while back.  CodeGuard doesn't 
like something about massive changes to an AnsiString in multi-threaded 
applications.  Check out this thread in which the problem is outlined a 
little more in-depth:

    http://tinyurl.com/2fghvy

It's discussed more in this thread:

    http://tinyurl.com/2eb85a

- Clayton
Re: Thread safety
#1837
Author: AlexB
Date: Wed, 13 Jun 2007 03:31
7 lines
987 bytes
Colin,

Just curious: what function do you actually use - sprintf or
AnsiString::sprintf ?

-- 
Alex
Re: Thread safety
#1765
Author: Colin B Maharaj
Date: Fri, 15 Jun 2007 10:26
9 lines
1337 bytes
Sorry I had not responded in a while - it was plain old sprintf


AlexB wrote:
> Colin,
> 
> Just curious: what function do you actually use - sprintf or
> AnsiString::sprintf ?
> 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads