Solved

library and thread general question

Posted on 2003-12-09
3
245 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

864 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

22 Experts available now in Live!

Get 1:1 Help Now