Solved

library and thread general question

Posted on 2003-12-09
3
233 Views
Last Modified: 2010-04-15
What are in <unistd.h> and <stdlib.h> ?  

To compile a thread on linux, we type

$cc -D_REENTRANT threadprog.c -o threadprog -lpthread

what does -D_REENTRANT do?  

Thanks

MD
0
Comment
Question by:mfaa
3 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9905843
When compiling code that uses threads we have to define the  _REENTRANT macro so that the thread-safe system functions are used instead.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 30 total points
ID: 9910354
 
Reentrant is an adjective that describes a computer program or routine that is written so that the same copy in memory can be shared by multiple users. Reentrant code is commonly required in operating systems and in application programs intended to be shared in multi-use systems. A programmer writes a reentrant program by making sure that no instructions modify the contents of variable values in other instructions within the program. Each time the program is entered for a user, a data area is obtained in which to keep all the variable values for that user. The data area is in another part of memory from the program itself. When the program is interrupted to give another user a turn to use the program, information about the data area associated with that user is saved. When the interrupted user of the program is once again given control of the program, information in the saved data area is recovered and the program can be reentered without concern that the previous user has changed some instruction within the program.

In a multithreaded environment, it is not uncommon to have race conditions
A race condition is what occurs when multiple threads read and write to the same memory without proper synchronization, resulting in an incorrect value being read or written. The result of a race condition may be a bit pattern which isn't even a valid value for the data type. A race condition results in undefined behavior.

A program is thread-safe if it has no race conditions, does not deadlock, and has no priority failures.Note that thread-safety does not necessarily imply efficiency, and than while some thread-safety violations can be determined statically at compile time, many thread-safety errors can only only be detected at runtime.

The -D_REENTRANT flag tells the compiler to enable reentrant flag which will lead to compiling and usage of thread safe functions
0
 

Author Comment

by:mfaa
ID: 9912193
Thanks for the reply...your help was greatly appreciated!

MD
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now