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

The Archive Base Latest Questions

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

So basically I am working on a class project where we make our own

  • 0

So basically I am working on a class project where we make our own implementation of a lock. I am fairly certain my lock code is correct, however my testing code is not working properly. we are using a version of pthreads called sthreads that my professor gave to the class.
here is my testing code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sync.h"
#include "sthread.h"

sthread_mutex_t *mutex;
int locktest(void* arg){
    int threadNum = (int)arg;
    //int i;
    for(;;){ 
        int x  = sthread_mutex_lock(mutex);
        printf("return value: %d\n", x);
        //sleep(1);
        printf("thread %d has the lock\n", threadNum);
        sleep(1);
        sthread_mutex_unlock(mutex);
    }
    return 0;
}


int main(){
sthread_t thr1, thr2, thr3, thr4, thr5;

if (sthread_init() == -1){
    return -1;
}

if(sthread_mutex_init(mutex)){
    return -1;
}

sthread_mutex_lock(mutex);

if (sthread_create(&thr1, locktest, (void *)1) == -1){
    return -1;
}
if (sthread_create(&thr2, locktest, (void *)2) == -1){
    return -1;
}
if (sthread_create(&thr3, locktest, (void *)3) == -1){
    return -1;
}
if (sthread_create(&thr4, locktest, (void *)4) == -1){
    return -1;
}
if (sthread_create(&thr5, locktest, (void *)5) == -1){
    return -1;
}

sleep(100);
sthread_mutex_unlock(mutex);
sthread_mutex_destroy(mutex);
return 0;

}

I’ve descovered that for some reason, even though mutex is global, each thread has a different instance of it. I know this because the return value for each call to sthread_mutex_lock(mutex) in the function locktest (which each thread runs) is not 0. this indicates the lock has not been initialized and is thus a null pointer, even though you see I initialized it in main in the second if statement.

does anyone have any clue as to why this is happening?

  • 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-13T00:44:04+00:00Added an answer on June 13, 2026 at 12:44 am

    You never initialize mutex to point at anything, so it has the value NULL. sthread_mutex_init(mutex) isn’t going to work, since it was basically passed the value NULL.

    You probably want sthread_mutex_init(&mutex) (declare sthread_mutex_init as taking a sthread_mutex_t **).

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

Sidebar

Related Questions

I have been working on a project for school. Basically you have to make
I'm working on a project for my computer science class that basically uses an
I'm currently working on a class project, and we're doing a basic implementation of
I'm working on a web app for a class. It's basically a project management
In a project I'm working on, I have a fairly large templated class that
So basically this code was working fine before. I had some computer issues and
I am working on a project which is basically a knowledge based question answering
I am working on some code, basically I have a form. On that form
Basically, my client wants to make new visitors enter the site I'm working on
I am trying to refactor a working code. The code basically derives an interface

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.