Portable thread safety in C?

purpose

I am writing a small library for which portability is important. It was designed to assume only a mostly compatible C90 environment (ISO / IEC 9899: 1990) ... nothing more. The set of functions provided by the library works (read / write) in the internal data structure. I looked at some other design alternatives, but nothing else is possible for the library trying to achieve .

Question

Are there any portable algorithms, methods, or spells that can be used to ensure thread safety? I am not interested in performing functions repeatedly. Moreover, I'm not interested in speed or (possibly) depletion of resources if the algorithm / technique / spell is portable. Ideally, I don't want to depend on any libraries (such as GNU Pth) or system operations (such as atomic tests and settings).

I considered a modification of the Lamport bakery algorithm , but I do not know how to change it to work inside functions called threads instead of working in the threads themselves.

Any help is greatly appreciated.

+3
source share
4

Lamport bakery, , ; , . , CPU : , CPU, " " . - , .

: (1) -, , (2) . 2 , , .

0
+1

( Windows) libpthread .

+1

Functions cannot either be thread safe or innate threads, depending on how you want to look at them. And threading / locking is an innate platform. In fact, users of your library can handle threading issues.

0
source

Source: https://habr.com/ru/post/1714165/


All Articles