Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assert() safety in multithreaded context

so I cannot seem to find solid info on whether assert is useable in a mulththreaded context.

logically to me it seems if an assertion fails the thread get shutdown but not the other threads?

or does the entire process get killed?

so basically my question. is it safe to use assert in a multithreaded environment without leaking resources?

like image 637
rowan.G Avatar asked Nov 20 '14 12:11

rowan.G


People also ask

How would you avoid conflicts in a concurrent multi threaded system?

The main way we can avoid such concurrency issues and build reliable code is to work with immutable objects. This is because their state cannot be modified by the interference of multiple threads.

What are thread-safe functions?

A threadsafe function protects shared resources from concurrent access by locks. Thread safety concerns only the implementation of a function and does not affect its external interface. In C language, local variables are dynamically allocated on the stack.

What is called thread-safe?

In computer programming, thread-safe describes a program portion or routine that can be called from multiple programming threads without unwanted interaction between the threads. (A thread is an instance of the program running on behalf of some user or process.)


2 Answers

if you see the man page of assert(), it clearly states,

The purpose of this macro is to help the programmer find bugs in his program. The message "assertion failed in file foo.c, function do_bar(), line 1287" is of no help at all to a user.

This means, it's only useful [and should be used] in a developing environment, not in production software. IMO, in development stage, you need not to worry about leaks caused by assert(). YMMV.

Once you finished debugging your code, you can simply switch off the assert() functionality by defining [#define] NDEBUG.

like image 92
Sourav Ghosh Avatar answered Sep 28 '22 11:09

Sourav Ghosh


I'd say more than yes. If I'd see a multithreaded code without asserts I'd not trust it. If you simplify a bit its implementations to something like:

#define assert(x) if( !(x) ) abort()

You'll see that it does nothing special for thread-safety or thread-specific. It's your responsibility to provide race-free condition and if the assertion fails, the whole process is aborted.

like image 34
Anton Avatar answered Sep 28 '22 11:09

Anton