thread-safe

Hello,

Some beginning QNX programmer question. What does it means thread-safe
library?

Best regards,
Darius

Darius wrote:

Some beginning QNX programmer question. What does it means thread-safe
library?

That the library’s functions are safe to use in a multithreaded application.


Chris Herborth (cherborth@qnx.com)
Never send a monster to do the work of an evil scientist.

“Chris Herborth” <cherborth@qnx.com> wrote in message
news:cfb56j$337$1@inn.qnx.com

Darius wrote:
Some beginning QNX programmer question. What does it means thread-safe
library?

That the library’s functions are safe to use in a multithreaded
application.

Imagine the following function


int foo ( void )
{

static char buffer[256];
int len’

len = sprintf( buffer, "…

return len;

}

If two threads execute this code “at the same time”, buffer will most
definitely contain garbage. The memory use by buffer would be the same in
the two threads.

int foo ( void )
{

char buffer[256];
int len’

len = sprintf( buffer, "…

return len;

}

Now buffer is on the stack and thread have their own stack so no problem.


Chris Herborth (> cherborth@qnx.com> )
Never send a monster to do the work of an evil scientist.

Mario Charest <nowheretobefound@8thdimension.com> wrote:

MC > “Chris Herborth” <cherborth@qnx.com> wrote in message
MC > news:cfb56j$337$1@inn.qnx.com
MC > Imagine the following function

MC > int foo ( void )
MC > {
MC > static char buffer[256];
MC > int len’
MC > len = sprintf( buffer, "…
MC > return len;
MC > }

MC > If two threads execute this code “at the same time”, buffer will most
MC > definitely contain garbage. The memory use by buffer would be the same in
MC > the two threads.

MC > int foo ( void )
MC > {
MC > char buffer[256];
MC > int len’
MC > len = sprintf( buffer, "…
MC > return len;
MC > }

MC > Now buffer is on the stack and thread have their own stack so no problem.

But be careful about putting a char array on the stack.
Imaging this function:

char * foo ( void )
{
char buffer[256];
sprintf( buffer, "…
return buffer;
}

That buffer will get tromped on by the next stack call, probibly before
it gets used.

Mario Charest <nowheretobefound@8thdimension.com> wrote:

MC > “Bill Caroselli” <qtps@earthlink.net> wrote in message

MC > Now buffer is on the stack and thread have their own stack so no
problem.

But be careful about putting a char array on the stack.

MC > You make it sound (at least to me) like it’s bad idea to but char array on
MC > the stack?

LOL. Sorry no. Only when your returning what’s in that char array.

Believe me, I’ve been bit on the butt by that one more times than I care
to admit. (Oh wait! I just admitted it.)

“Bill Caroselli” <qtps@earthlink.net> wrote in message
news:cfd57b$hgf$1@inn.qnx.com

Mario Charest <> nowheretobefound@8thdimension.com> > wrote:

MC > “Chris Herborth” <> cherborth@qnx.com> > wrote in message
MC > news:cfb56j$337$> 1@inn.qnx.com> …
MC > Imagine the following function

MC > int foo ( void )
MC > {
MC > static char buffer[256];
MC > int len’
MC > len = sprintf( buffer, "…
MC > return len;
MC > }

MC > If two threads execute this code “at the same time”, buffer will most
MC > definitely contain garbage. The memory use by buffer would be the
same in
MC > the two threads.

MC > int foo ( void )
MC > {
MC > char buffer[256];
MC > int len’
MC > len = sprintf( buffer, "…
MC > return len;
MC > }

MC > Now buffer is on the stack and thread have their own stack so no
problem.

But be careful about putting a char array on the stack.

You make it sound (at least to me) like it’s bad idea to but char array on
the stack?

Imaging this function:

char * foo ( void )
{
char buffer[256];
sprintf( buffer, "…
return buffer;
}

That buffer will get tromped on by the next stack call, probibly before
it gets used.

True, but not specificaly related to thread issues. Haven’t tried it with
gcc but watcom generates a warning for “return buffer” .

“Bill Caroselli” <qtps@earthlink.net> wrote in message
news:cfdbsc$lvr$5@inn.qnx.com

Mario Charest <> nowheretobefound@8thdimension.com> > wrote:

MC > “Bill Caroselli” <> qtps@earthlink.net> > wrote in message

MC > Now buffer is on the stack and thread have their own stack so no
problem.

But be careful about putting a char array on the stack.

MC > You make it sound (at least to me) like it’s bad idea to but char
array on
MC > the stack?

LOL. Sorry no. Only when your returning what’s in that char array.

Believe me, I’ve been bit on the butt by that one more times than I care
to admit. (Oh wait! I just admitted it.)

Compiler will issue warning, but I also rely on flint to catch these problem
(not in my code of course, lol)

  • Mario