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

  • SEARCH
  • Home
  • 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 7883709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:36:21+00:00 2026-06-03T04:36:21+00:00

I am trying to build a library that would write to a single file,

  • 0

I am trying to build a library that would write to a single file, and would be able to work in a multi-threaded environment. The requirements are:

  1. No concurrency problems will occur while writing to the file.
  2. The order in which threads are handled is not important.
  3. The library should be non blocking, i.e. the write and flush functions will return before the given buffer had been written.

Here’s what I have so far:

int write2device(char *buffer, int length) {
    Task * task = new Task(id++,buffer,length);
    pthread_t * thread = new pthread_t;
    Argument * arg = new Argument; //A sturct with pthread_t and task fields
    arg->task = task;
    arg->thread = thread;
    pthread_create(thread,NULL,deamonWrite,arg);
    return 0;
}

void wait(Argument * arg) {
    //manager is a singleton class that handles the threads database and related
    //issues
    manager->pushDeamon(arg->thread);
    manager->lock(arg->task->getId()); //mutex - only one thread can write
}

void * deamonWrite(void * arg) {
    Argument * temp = (Argument *) arg;
    wait(temp);
    //critical section
    //will add signal() later
    return NULL;
}

The idea is that for every thread calling write2device I open a thread that runs deamonWrite(). This function has the structure of wait() -> critical section -> signal().
In wait, if someone else is writing I will (haven’t done yet) suspend the thread so that the user won’t wait till it’s done writing.

I have two questions:

  1. How do I implement the mutex (lock function)? I understand that This must be an atomic function, sense several threads trying to acquire a lock might result in chaos.
  2. Is my general structure in the right way?

I am new to concurrency and would appreciate any thoughts on this matter – thanks!

  • 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-06-03T04:36:22+00:00Added an answer on June 3, 2026 at 4:36 am

    Push the Task structures to a queue/vector and process them sequentially from a single thread instead of multiple threads for each task individually. The only place where you’ll need a mutex is when pushing to and pulling from the queue. As Ben correctly noted in the comments, you should leave the implementation of thread synchronization primitives (mutex, critical section) to the OS and/or whatever system API you’re allowed to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I just trying to build a library using the NDK. I believe that
I'm trying to build an Eclipse plugin that depends on a library which has
I am trying to write a go library that will act as a front-end
I'm trying to build a static library (lib.a file) for use in creating an
So I'm trying to build this library that is a .NET binding for Mapnik
I am trying to build a library for solving different constrained problems . I
I'm trying to build a wrapper library with VC++'s compiler. ErlDriver.c #define __WIN32__ #define
I'm trying to build xml for a library I'm making. What I'm trying to
I'm trying to build some .ascx controls into a class library for plugins for
I'm trying to use the post build command line to get my class library

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.