Assert in Threads

H

hyderabadblues

Hi ,

I have a doubt regarding the asserts. I have multiple threads running,
is there any chance that there can be assert in two threads at the
same time.
 
G

Guest

Hi ,

I have a doubt regarding the asserts. I have multiple threads running,
is there any chance that there can be assert in two threads at the
same time.

The current standard does not deal with threads or other forms of
concurrency, you will have to consult the documentation of the threading
library and/or C++ implementation. However, considering that a failed
assert will terminate the application the likelihood that a second
assert will fail before the first has terminated you application is
pretty slim.
 
J

Jim Langston

hyderabadblues said:
Hi ,

I have a doubt regarding the asserts. I have multiple threads running,
is there any chance that there can be assert in two threads at the
same time.

OT and I don't believe an Assert halts all threads, so yes. But I'm not
sure, you should ask in a thread newsgroup such as comp.programming.threads
where they would know.
 
J

James Kanze

I have a doubt regarding the asserts. I have multiple threads
running, is there any chance that there can be assert in two
threads at the same time.

Sure. The usual access rules apply to any variables used in the
assert (so you may need to protect it with a lock).

Of course, once an assert fails, you're over and done with. A
failed assertion calls abort(), which normally brings the whole
process down with a bang, by means of a SIGABRT. According to
the language standard (and Posix), if there is a signal handler
for this signal, and it returns, "The abort function causes
abnormal program termination to occur, unless the signal SIGABRT
is being caught and the signal handler does not return."

(I'm not sure of all of the details, but under Posix, at least,
the signal is delivered to the process, not just the thread.
I'm also not too sure what happens if one of the threads is in a
sigwait on SIGABRT, either. At least under Solaris, it doesn't
seem that you can successfully catch it with a sigwait, or block
it with pthread_sigmask.)
 
J

James Kanze

OT and I don't believe an Assert halts all threads, so yes.

Not really OT, threads are very much a part of C++, even if they
aren't (yet) specified in the standard. And assert definitly
kills the process (not just the thread) under Posix, and I
suspect also under Windows.
 
R

R Samuel Klatchko

hyderabadblues said:
I have a doubt regarding the asserts. I have multiple threads running,
is there any chance that there can be assert in two threads at the
same time.

That would depend on the specific implementation of assert and what you
consider an assertion occurrence to be. In that second point, is an
assertion failure is not an atomic event, do you consider an assertion
to be when the assertion first fails or when the process exits?

Consider this potential implementation of assert:

#define assert(expr) \
if (!(expr)) { \
assert_output(__FILE__, __LINE_, #expr); \
abort(); \
}

Assume once abort() begins, that no other thread will run.

It's possible that two threads could simultaneously evaluate an asserts
expression to false and both be able to output their log message. But
only one of the threads call to abort() will occur while the other
thread will be stopped before being able to call abort().

samuel
 
J

James Kanze

Consider this potential implementation of assert:
#define assert(expr) \
if (!(expr)) { \
assert_output(__FILE__, __LINE_, #expr); \
abort(); \
}

Just a nit, but that's not a legal implementation. The
expansion of assert() must result in an expression with type
void. (It can be used before the comma operator, for example.)
The typical implementation might be something like:

extern int assert_failed( char const*, int, char const* ) ;
#undef assert
#ifndef NDEBUG
#define assert( expr ) \
(void)( (expr) || assert_failed( __FILE__, __LINE__, #expr ) )
#else
#define assert( expr ) ((void)0)
#endif

The call to abort() would be in assert_failed().
 

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

Staff online

Members online

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top