Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 534371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:36:19+00:00 2026-05-13T09:36:19+00:00

imagine I write a library in C. Further, imagine this library to be used

  • 0

imagine I write a library in C. Further, imagine this library to be used from a multi-threaded environment. How do I make it thread-safe? More specific: How do I assure, that certain functions are executed only by one thread at a time?

In opposite to Java or C# for example, C has no means to deal with threads/locks/etc., nor does the C standard library. I know, that operating systems support threads, but using their api would restrict the compatibility of my library very much. Which possibilities do I have, to keep my library as compatible/portable as possible? (for example relying on OpenMP, or on Posix threads to keep it compatible with at least all unix-like operating systems?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:36:19+00:00Added an answer on May 13, 2026 at 9:36 am

    You can create wrappers with #ifdef. It’s really the best you can do. (Or you can use a third party library to do this).

    I’ll show how I did it as an example for windows and linux. It’s in C++ and not C but again it’s just an example:

    #ifdef WIN32
    typedef HANDLE thread_t;
    typedef unsigned ThreadEntryFunction;
    #define thread __declspec(thread)
    
    class Mutex : NoCopyAssign
    {
    public:
        Mutex() { InitializeCriticalSection(&mActual); }
        ~Mutex() { DeleteCriticalSection(&mActual); }
        void Lock() { EnterCriticalSection(&mActual); }
        void Unlock() { LeaveCriticalSection(&mActual); }
    private:
        CRITICAL_SECTION mActual;
    };
    
    class ThreadEvent : NoCopyAssign
    {
    public:
        ThreadEvent() { Actual = CreateEvent(NULL, false, false, NULL); }
        ~ThreadEvent() { CloseHandle(Actual); }
        void Send() { SetEvent(Actual); }
    
        HANDLE Actual;
    };
    #else
    typedef pthread_t thread_t;
    typedef void *ThreadEntryFunction;
    #define thread __thread
    extern pthread_mutexattr_t MutexAttributeRecursive;
    
    class Mutex : NoCopyAssign
    {
    public:
        Mutex() { pthread_mutex_init(&mActual, &MutexAttributeRecursive); }
        ~Mutex() { pthread_mutex_destroy(&mActual); }
        void Lock() { pthread_mutex_lock(&mActual); }
        void Unlock() { pthread_mutex_unlock(&mActual); }
    private:
        pthread_mutex_t mActual;
    };
    
    class ThreadEvent : NoCopyAssign
    {
    public:
        ThreadEvent() { pthread_cond_init(&mActual, NULL); }
        ~ThreadEvent() { pthread_cond_destroy(&mActual); }
    
        void Send() { pthread_cond_signal(&mActual); }
    private:
        pthread_cond_t mActual;
    };
    
    inline thread_t GetCurrentThread() { return pthread_self(); }
    #endif
    
    /* Allows for easy mutex locking */
    class MutexLock : NoAssign
    {
    public:
        MutexLock(Mutex &m) : mMutex(m) { mMutex.Lock(); }
        ~MutexLock() { mMutex.Unlock(); }
    private:
        Mutex &mMutex;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine you are on Windows 7 and you have to write a GUI for
I am trying to write a gsub for a regex match, but I imagine
imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and
Imagine I've got a database with lots of data, from which users can search.
Imagine I have this object (written with Ruby literals) stored in a MongoDB: {tags
I would like to write a transition where all the elements from State1 rotate
I'm using the Netty library (version 4 from GitHub). It works great in Scala,
Imagine you have a library for working with some sort of XML file or
Is it possible to write the complete C++ standard library (including STL of course,
Imagine there is a file of size 5 MB. I open it in write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.