Main Content

CERT C++: CON37-C

Do not call signal() in a multithreaded program

Description

This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).

Rule Definition

Do not call signal() in a multithreaded program.1

Polyspace Implementation

The rule checker checks for Signal call in multithreaded program.

Examples

expand all

Issue

Signal call in multithreaded program occurs when you use the signal() function in a program with multiple threads.

Risk

According to the C11 standard (Section 7.14.1.1), use of the signal() function in a multithreaded program is undefined behavior.

Fix

Depending on your intent, use other ways to perform an asynchronous action on a specific thread.

Example - Use of signal() Function to Terminate Loop in Thread
#include <signal.h>
#include <stddef.h>
#include <thread>

volatile sig_atomic_t flag = 0;

void handler(int signum) {
	flag = 1;
}

/* Runs until user sends SIGUSR1 */
int func(int data) {
	while (!flag) {
		/* ... */
	}
	return 0;
}

int main(void) {
	signal(SIGINT, handler); //Noncompliant
	int data;
	//...
	std::thread th1(func, data);

	return 0;
}

In this example, the signal function is used to terminate a while loop in the thread.

Correction — Use atomic_bool Variable to Terminate Loop

One possible correction is to use an std::atomic variable of bool type that multiple threads can access. In the corrected example, the std::atomic<bool> variable flag can be accessed by both the main thread and the child thread th1. Before every loop iteration, the child thread checks flag. After completing the program, you can modify this variable so that the child thread exits the loop.

#include <thread>
#include <atomic>
std::atomic<bool> flag(false);

int func(int data) {
	while (!flag) {
		/* ... */
	}
	return 0;
}

int main(void) {
	int data;
	//...
	std::thread th1(func, data);
	th1.join();
	flag = true;
	return 0;
}

Check Information

Group: 10. Concurrency (CON)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.