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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:56:44+00:00 2026-06-13T18:56:44+00:00

I’m writing a program that reads and processes large amounts of data. To speed

  • 0

I’m writing a program that reads and processes large amounts of data. To speed up the process I’ve implemented multithreaded reading/processing using the Pthreads library for C++. When I call pthread_mutex_lock(&lock1) on my mutex however, my program errors out giving me an “access violation reading location” error on the address of lock1. I needed to run these threads on methods of a class, which required some maneuvering to pull off within the restrictions of the pthread library and it’s somewhere in that maneuvering that I made my mistake. Below is the abbreviated version of my code, sorry for the tl;dr. Shortened it as much as possible without leaving out any sections critical to the multithreading:

#include "pthread.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>


/*Ideally I need to create one instance of this object that has the run_stuff method 
being executed by two different threads.  I need both threads to have access to the 
same private variables, and have ensured that those variables won't be accessed at the 
same time. That is the end goal of my multithreading*/

class test_obj {
public:
    test_obj();
    void run_stuff(int); //the method I want to run on two different threads using the same private variables
private:
    int count;
    void switch_gate1();//switches the value of gate 1
    void switch_gate2();
    bool test_gate1(int); //checks to see if gate 1 equals thread id
    bool test_gate2(int);
    bool gate1, gate2; //can only be accessed under a locked mutex
    pthread_mutex_t lock1, lock2;
};

/*This structure is what I pass to pthread_create*/
struct thread_args { //arguements passed to a thread when it is created
    thread_args() {a = new test_obj;}; //constructor: create new test_obj instance
    test_obj *a; //pointer to a test_obj instance, I think both threads receive a pointer to the same instance
    int t_id; //ID of thread being created
};

class thread_helper { //managing function to create threads and call run_stuff method
public:
    thread_helper() {args = new thread_args;};
    void my_method(); //called from main, creates threads and runs them
private:
    thread_args *args; //pointer to a thread_args obj
    static void* execute(void *ptr); //method to be run by created threads
    pthread_t m_thread1, m_thread2; //the two threads
};

int main() {
    thread_helper c; 
    c.my_method();
    return 0;
}

test_obj::test_obj() {
    count = 0; //not important right now, test variable
    gate1 = 0; //thread 0 gets first access
    gate2 = 0; //thread 0 gets first access
}

void thread_helper::my_method() {
    int error_code;
    thread_args *a, *b;
    *a = *b = *args;
    a->t_id = 0;
    b->t_id = 1;
    error_code = pthread_create(&m_thread1, NULL, &thread_helper::execute, (void*)a); //create thread 0 to run execute method
    assert(0 == error_code); //check for success
    error_code = pthread_create(&m_thread2, NULL, &thread_helper::execute, (void*)b);
    assert(0 == error_code);
    error_code = pthread_join(m_thread1, NULL);//wait for thread 0
    assert(0 == error_code);
    error_code = pthread_join(m_thread2, NULL); //wait for thread 1
    assert(0 == error_code);
}

void* thread_helper::execute(void *ptr) {
    thread_args *b = (thread_args*)ptr;//recast ptr back into thread_args type
    int t_id = b->t_id;//save thread id
    b->a->run_stuff(t_id);//access test_obj object from thread_args (should be same object for both threads) and run method
    return NULL;
}

void test_obj::run_stuff(int t_id) {
    for(int i = 0; i < 2; i++) { //run twice (give a chance for threads to flip flop)
    //-----Part 1
    while(!test_gate1(t_id)){//while not allowed to proceed
        Sleep(500);//sleep
    }
    printf("thread %d reporting in\n", t_id);//thread has gained access to part 1, print id
    switch_gate1();//allow opposite thread in
    //-----Part 2
    while(!test_gate2(t_id)){//wait for access to part 2
        Sleep(500);
    }
    //count++; //just a dummy place for now, will be used later to check access
    switch_gate2();//allow opposite thread in
    }
}

bool test_obj::test_gate1(int t_id) {//returns true if id of calling thread matches value of gate 1
//a return value of true means a thread is allowed to proceed, false otherwise
    pthread_mutex_lock(&lock1);**//secure access to gate THIS IS WHAT ERRORS OUT**
    if(t_id == gate1){//check gate
        pthread_mutex_unlock(&lock1);//release gate
        return true;//return
    }
    else {
        pthread_mutex_unlock(&lock1);
        return false;
    }
}

bool test_obj::test_gate2(int t_id) {
    pthread_mutex_lock(&lock2);
    if(t_id == gate2){
        pthread_mutex_unlock(&lock2);
        return true;
    }
    else {
        pthread_mutex_unlock(&lock2);
        return false;
    }
}

/*Switch gate functions are designed to obtain access to the boolean gate (prevent the opposing thread from checking the gate by means of mutex), then switch the value of the boolean gate and release the gate for the other thread to check.*/
void test_obj::switch_gate1() {
    pthread_mutex_lock(&lock1);//secure access to gate
    gate1 = !gate1; //flip gate
    pthread_mutex_unlock(&lock1); //release gate
}

void test_obj::switch_gate2() {
    pthread_mutex_lock(&lock2);
    gate2 = !gate2;
    pthread_mutex_unlock(&lock2);
}

Sorry about the formatting. Any one have any idea why that lock would have access violation errors? I’ve tried putting it everywhere. Private, public, and even global as a last resort. They all give the same error. Thanks in advance.

EDIT: Fixed an error in passing my thread_args variable to pthreads_create. Works as expected now after that and the initialize/destroy fix.

  • 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-13T18:56:46+00:00Added an answer on June 13, 2026 at 6:56 pm

    You don’t seem to initialize your mutexes. I don’t see where you have declarations of your mutexes.

    Declare variables in your test_obj:

    pthread_mutex_t lock1;
    pthread_mutex_t lock2;
    

    Add this to your test_obj constructor:

    test_obj::test_obj() {
        ...
        lock1 = PTHREAD_MUTEX_INITIALIZER;
        lock2 = PTHREAD_MUTEX_INITIALIZER;
    }
    

    Add this to a test_obj destructor:

    test_obj::~test_obj() {
        pthread_mutex_destroy(&lock1);
        pthread_mutex_destroy(&lock2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

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.