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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:45:36+00:00 2026-05-26T04:45:36+00:00

/*language C code*/ #include windows.h typedef struct object_s { SRWLOCK lock; int data; }

  • 0
/*language C code*/

#include "windows.h"

typedef struct object_s
{
    SRWLOCK lock;
    int data;
} object_t, *object_p; /*own and pointer type*/

void thread(object_p x)
{
    AcquireSRWLockExclusive(&x->lock);
    //...do something that could probably change x->data value to 0
    if(x->data==0)
        free(x);
    else
        ReleaseSRWLockExclusive(&x->lock);
}

void main()
{
    int i;
    object_p object=(object_p)malloc(sizeof(object_t));

    InitializeSRWLock(&object->lock);

    for(i=0;i<3;i++)
     CreateThread(0,0,thread,object,0);
}

As you can figure out in the codes above, what I have to accomplish is to let one thread conditionally free the object on which the other two may block. Codes above are obviously flawed because if object is set free along with the lock, all blocking threads give us nowhere but wrong.

A solution below

/*language C code*/

#include "windows.h"

typedef struct object_s
{
    /*change: move lock to stack in main()*/
    int data;
} object_t, *object_p; /*own and pointer type*/

void thread(void * x)
{
    struct {
    PSRWLOCK l;
    object_p o;
    } * _x=x;
    AcquireSRWLockExclusive(_x->l);
    //...do something that could probably change x->data value to 0
    if(_x->o->data==0)
        free(_x->o);
    ReleaseSRWLockExclusive(&x->lock);
}

void main()
{
    int i;
    SRWLOCK lock; /*lock over here*/
    object_p object=(object_p)malloc(sizeof(object_t));

    InitializeSRWLock(&lock);

    /*pack for thread context*/
    struct
    {
        PSRWLOCK l;
        object_p o;
    } context={&lock, object};

    for(i=0;i<3;i++)
     CreateThread(0,0,thread,&context,0);
}

works in this case but not applicable however, in my final project because there is actually a dynamic linked list of objects. By applying this solution it means that there must be a list of locks accordingly, each lock for an object and moreover, when a certain object is set free, its lock must be set free at the same time. There is nothing new compared with the first code section.

Now I wonder if there is an alternative solution to this. Thank you very much!

  • 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-26T04:45:36+00:00Added an answer on May 26, 2026 at 4:45 am

    The solution is to not allocate the lock together with the data. I would suggest that you move the data out of that struct and replace it with a pointer to the data. Your linked list can then free the data first, and then the node, without any problems. Here’s some pseudo code:

    typedef struct 
    {
      lock_t lock;
      int*   data_ptr;
    } something_t;
    
    void init_something (something_t* thing, ...)
    {
      thing->lock = init_lock();
      thing->data_ptr = malloc(...);  // whatever the data is supposed to be
    }
    
    void free_something (somthing_t* thing)
    {
      lock(thing->lock);
        free(thing->data_ptr);
        thing->data_ptr = NULL;
      unlock(thing->lock);
    }
    
    ...
    
    void linked_list_delete_node (...)
    {
      free_something(node_to_delete->thing);
      free(node_to_delete);
    }
    
    ...
    
    void thread (void* x)
    {
      lock(x->lock);
        //...do something that could probably change x->data_ptr->data... to 0
        if(x->data_ptr->data == 0)
        {
          free_something(x->data_ptr->data);
        }
      unlock(x->lock);
    }
    
    
    
     AcquireSRWLockExclusive(lock);
      if(_x->o->data==0)
        free(_x);
     ReleaseSRWLockExclusive(lock);
    

    As a sidenote, a C program for Windows can never return void. A hosted C program must always return int. Your program will not compile on a C compiler.

    Also, CreateThread() expects a function pointer to a function returning a 32-bit value and taking a void pointer as parameter. You pass a different kind of function pointer, function pointer casts aren’t allowed in C, nor am I sure what sort of madness Windows will execute if it gets a different function pointer than what it expects. You invoke undefined behavior. This can cause your program to crash or behave in unexpected or random ways.

    You need to change your thread function to DWORD WINAPI thread (LPVOID param);

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

Sidebar

Related Questions

Which programming language(s) were used to code Windows Vista?
While adding two language code files i.e. C# and VB.NET to App_Code, got an
I've got to develop a multi-language code generator in C#. Well actually the idea
This code compiles fine: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances, FlexibleContexts, EmptyDataDecls, ScopedTypeVariables, TypeOperators,
Language: asp This is sample of my code: str = www.example.com/gotobuy.aspx?id=1234 key_word = .obuy.
Language = C# .NET version = 3.5 When my code performs syntax highlighting the
I have code that relies heavily on yaml for cross-language serialization and while working
I am trying to code a flowchart generator for a language using Ruby. I
Suppose I am entering validation code into my model of multi-language publication database. The
Question: I want code for: syntax highlighting (of programming languages) Language: C# or assembly

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.