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 6896095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:59:29+00:00 2026-05-27T06:59:29+00:00

I am writing a simple multi-threaded buffer. It’s purpose is to hold data from

  • 0

I am writing a simple multi-threaded buffer. It’s purpose is to hold data from the sound card until I can process them. It is implemented as a simple linked list (Element = list element, DataQueue = actual buffer, DataChunk = object with data):

class Element{
private:
    Element *next;
    DataChunk *data;
public:
    Element(DataChunk *data);
    ~Element();
    DataChunk *getData();
    Element *nextElement();
    void setNextElement(Element *nextElement);
};

class DataQueue{
private:
    int size;
    Element *top;
    Element *last;
    HANDLE lock;
public:
    DataQueue();
    void append(DataChunk *chunk);
    Element *cut(int *cutSize);
};

Element::Element(DataChunk *data){
    this->data = data;
    this->next = NULL;
}

Element::~Element(){
    delete data;
}

DataChunk *Element::getData(){
    return data;
}

Element *Element::nextElement(){
    return next;
}

void Element::setNextElement(Element *nextElement){
    this->next = nextElement;
}

DataQueue::DataQueue(){
    size = 0;
    top = NULL;
    last = NULL;
    lock = CreateEvent(NULL, TRUE, FALSE, TEXT("Lock"));
    if(lock == NULL){
        printf("Error code: %ld\n", GetLastError());
        exit(EXIT_FAILURE);
    }
}

void DataQueue::append(DataChunk *chunk){
    WaitForSingleObject(lock, INFINITE);
    SetEvent(lock);

    Element *element = new Element(chunk);
    if(top == NULL){
        top = element;
    }
    else{
        last->setNextElement(element);
    }
    last = element;

    ResetEvent(lock);
}

Element *DataQueue::cut(int *cutSize){
    WaitForSingleObject(lock, INFINITE);
    SetEvent(lock);

    Element *toReturn = top;
    *cutSize = size;
    top = NULL;
    last = NULL;
    size = 0;

    ResetEvent(lock);

    return toReturn;
}

The usage pattern is like this:
The audio driver (ASIO) is using callback functions to notify me when its buffers are ready for processing. This happens A LOT (dozen times per second). But I need to process the data only once per 5 seconds or so. So I use my own buffer to store 5 secs worth of sound data. Driver creates its own thread and executes the callback function. From this thread, I call append() and append the data from device buffers to my buffer. When the actual processing thread comes along, it cuts the buffer by getting the first element and reseting the buffer to 0 elements.

The problem is thread synchronization. The cut() and access() functions are mutually exclusive. I have Event HANDLE and I wait on this Event. I have some experience with Java, and am totally new to these Win32 APIs. Am I doing it right? Just like this, the program crashes on WaitForSingleObject, probably because I don’t have SYNCHRONIZATION access right (c0000005). How can I set it to a thread created by a driver? Will this work even when there are multiple processing threads?

  • 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-27T06:59:30+00:00Added an answer on May 27, 2026 at 6:59 am

    EnterCriticalSection did the trick.

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

Sidebar

Related Questions

I am writing a fairly simple webapp that pulls data from 3 tables in
I am writing a multi-threaded program using OpenMP in C++. At one point my
I have a rather simple multi-threaded VCL gui application written with Delphi 2007. I
I am writing a multi threaded application and I have concerns with 2 threads
I have a multi-threaded gSOAP service running with enabled http-keepalive. How can I gracefully
I've read a lot recently about how writing multi-threaded apps is a huge pain
I am writing a multi-threaded program in C where one core periodically grabs an
I'm migrating a multi threaded application from HP-UX to Solaris and so far, everything
I am writing a simple multi-drop RS485 protocol for serial communications within a distributed
I am writing a simple multithreaded socketserver and I am wondering how best to

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.