C Thread Safe and Reentrant Function Examples

http://www.thegeekstuff.com/2012/07/c-thread-safe-and-reentrant/

Re-entrance and thread-safety are two different concepts that can be associated with good programming practices. In this article we will try and understand both the concepts and their differences with the help of some code snippets.

1. Thread Safe Code

As the name suggests, a piece of code is thread safe when more than one thread can execute the same code without causing synchronization problems. Lets look at the following code snippet :

...
...
...

char arr[10];
int index=0;

int func(char c)
{
    int i=0;
    if(index >= sizeof(arr))
    {
        printf("\n No storage\n");
        return -1;
    }
    arr[index] = c;
    index++;
    return index;
}

...
...
...

The above function populates the array ‘arr’ with the character value passed to it as argument and then updates the ‘index’ variable so that subsequent calls to this function write on the updated index of the array.

Suppose this function is being used by two threads. Now, lets assume that thread one calls this function and updates the array index with value ‘c’. Now, before updating the ‘index’ suppose second thread gets the execution control and it also calls this function. Now since the index was not updated by thread one , so this thread writes on the same index and hence overwrites the value written by thread one.

So we see that lack of synchronization between the threads was the root cause of this problem.

Now, lets make this function thread safe :

...
...
...

char arr[10];
int index=0;

int func(char c)
{
    int i=0;
    if(index >= sizeof(arr))
    {
        printf("\n No storage\n");
        return -1;
    }

    /* ...
       Lock a mutex here
       ...
    */

    arr[index] = c;
    index++;

    /* ...
       unlock the mutex here
       ...
    */

    return index;
}

...
...
...

What we did above is that we made the array and index updates an atomic operation using the mutex locks. Now even if multiple threads are trying to use this function, there would be no synchronization problems as any thread which acquire the mutex will complete both the operations (array and index update) before any other thread acquires the mutex.

   

     
                 
   

So now the above piece of code becomes thread-safe.

2. Re-entrant Code

The concept of re-entrant code is slightly different from thread safe code. Usually in a single thread of execution if a function is called then before the completion of execution of that particular function, the flow cannot move ahead. But, there are some situations where in a single thread also the execution of a function can be interrupted by a call to same function again. So a piece of code that can successfully handle the this scenario is known as a re-entrant code. Lets look at the example below :

...
...
...

char *s;

void func()
{
    int new_length = 0;

    // initialize 'new_length'
    // with some new value here

    char *ptr = realloc(s, new_length);

    if(ptr)
    {
        s = ptr;
    }
    else
    {
        //Report Failure
    }

    // do some stuff here
}

...
...
...

if we analyze the re-entrance capability of the above code, we find that this code is not re-entrant. This is because of the fact that the above code is buggy in the sense that if the same function is being used by a signal handler (in response to handling of some signals) then in the situation where a call to function func() was between realloc() and the ‘if’ condition next to it and then this execution is interrupted by a call to this function from signal handler. In this scenario since ‘s’ is not updated with new allocated address so realloc might fail (or program might even crash).

So we see that the above code is not re-entrant. A re-entrant code is least expected to work with global variables. Following is an example of a re-entrant code:

...
...
...

int exchange_values(int *ptr1, int *ptr2)
{
    int tmp;

    tmp = *ptr1;
    *ptr1 = *ptr2;
    *ptr2 = *tmp;

    return 0;
}

...
...
...

3. Thread Safe but not Re-entrant

A piece of code can be thread safe but it’s not necessary that its re-entrant. Look at the following code :

...
...
...

int func()
{
    int ret = 0;

    // Lock Mutex here

    // Play with some
    // global data structures
    // here   

    // Unlock mutex

    return ret;
}

...
...
...

In the example above, since the critical section is protected by mutex so the code above is thread safe but its not re-entrant because if the execution of the above function is interrupted through some signal handler (calling the same function while handling a signal) then (if non-recursive mutexes are being used) the first execution is interrupted while the second execution will wait forever to acquire mutex. So overall the complete program will hang.

4. Re-entrant but not Thread Safe

Re-entrance is something which is associated with a function whose first execution gets interrupted by second call to it (from within the same thread) and this first execution resumes when the second execution completes. This is not the case with threads which can keep on stepping onto another thread’s toes multiple times. So if a function is re-entrant then it does not guarantee that its thread safe.

 

标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值