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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:24:23+00:00 2026-06-17T01:24:23+00:00

I have this code that does the following: -generates a linked list that contains

  • 0

I have this code that does the following:

-generates a linked list that contains “e” nodes, each node has assigned a random value, and worker number

-creates w threads, each thread ckecks in the list if the worker number of the node is equal to his thread id, if so, it makes sqrt from the value

-when 5 or all of the values from the list are smaller than 2, the main thread deletes from the list the elements smaller than 2

-only 3 threads can work at once.

Here is the code:

DWORD WINAPI work(LPVOID argument){
    int tid = *((int *)argument);
    elem* p = head;
    while(p!=NULL){
        if (tid == p->worker){
            WaitForSingleObject(semaphor, 0L);
            double val = p->value;
            p->value = sqrt(val);
            if (p->value < 2){
                EnterCriticalSection(&countMutex);
                count++;
                if(count == maxCount){
                    WakeConditionVariable (&conditionalVar);
                }
                LeaveCriticalSection(&countMutex);
            }       
            Sleep(1);
            ReleaseSemaphore(semaphor, 1L, NULL);
        }
        if (p->next == NULL)
            p = head;
        else 
            p = p->next;
    }

    return NULL;
}

int _tmain(int argc, _TCHAR* argv[])
{
    //generate list, create threads (the threads are assigned to the work function)

    EnterCriticalSection(&countMutex);
    while(e > 0){
        SleepConditionVariableCS(&conditionalVar, &countMutex, INFINITE);
        deleteElements();
        if (e < 5)
            maxCount = e;
        count = 0;
        cout << "After erasure: \n";
        printList();
        cout << "\n";
    }
    LeaveCriticalSection(&countMutex);


    //free space
}

The problem is that after the first deletion, i get the error “Unhandled exception at 0x00fe1c8e in mainW.cpp.exe: 0xC0000005: Access violation reading location 0xfeeefef2.”

I do realise that this means that somewhere i’m trying to acces a value that has already been deleted, but i don’t know why, since the list is synchronized.

  • 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-17T01:24:24+00:00Added an answer on June 17, 2026 at 1:24 am

    You are traversing the list without any locks.

    if (p->next == NULL)
        p = head;
    else 
        p = p->next;
    

    What if the main thread just deleted p->next?

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

Sidebar

Related Questions

I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this email sending code that does not show a chooser (good). How
I have code that does a web-service request. While doing this request I need
I have code that all over the place(probably 20-30 instances) does this: <widget>.setVisible((condition ==
I have a script that generates a csv file using the following code: header('Content-type:
I have a legacy application that, at some point, generates the following code in
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have this code that runs but never stops. class A { public static
I have this code that reads from XML file. It gets five strings (groupId,
I have this code that suppose to place the marker by the latlng public

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.