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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:28:06+00:00 2026-06-09T09:28:06+00:00

I was trying to test the pthread by using mutex: #include <stdio.h> #include <pthread.h>

  • 0

I was trying to test the pthread by using mutex:

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int global = 0;
void thread_set();
void thread_read();



int main(void){
  pthread_t thread1, thread2;
  int re_value1, re_value2;
  int i;
  for(i = 0; i < 5; i++){
    re_value1 = pthread_create(&thread1,NULL, (void*)&thread_set,NULL);
    re_value2 = pthread_create(&thread2,NULL,(void*)&thread_read,NULL);
  }

  pthread_join(thread1,NULL);
  pthread_join(thread2,NULL);
  /* sleep(2); */ // without it the 5 iteration couldn't finish
  printf("exsiting\n");
  exit(0);
}

void thread_set(){
  pthread_mutex_lock(&mutex1);
  printf("Setting data\t");
  global = rand();
  pthread_mutex_unlock(&mutex1);
}
void thread_read(){
  int data;
  pthread_mutex_lock(&mutex1);
  data = global;
  printf("the value is: %d\n",data);
  pthread_mutex_unlock(&mutex1);
}

without the sleep(), the code won’t finish the 5 iteration:

Setting data the value is: 1804289383
the value is: 1804289383
Setting data the value is: 846930886
exsiting

Setting data the value is: 1804289383
Setting data the value is: 846930886
the value is: 846930886
exsiting

It works only add the sleep() to the main thread, I think it should work without the sleep(), because the join() function wait for each child thread terminate

Any one can tell me why is it?

  • 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-09T09:28:07+00:00Added an answer on June 9, 2026 at 9:28 am

    Your use of mutex objects looks fine, but this loop

    for(i = 0; i < 5; i++) {
         re_value1 = pthread_create(&thread1,NULL, (void*)&thread_set,NULL);
         re_value2 = pthread_create(&thread2,NULL,(void*)&thread_read,NULL);
    }
    

    is asking for trouble as you are reusing the same thread instances thread1 and thread2 for each iteration of your loop. Internally this must be causing problems although I do not know exactly how it would manifest itself. You should really use a separate instance of thread object per thread you want to ensure reliable running. I do not know what would happen if you called pthread_create using an instance of a thread object that is already running but I suspect it is not a wise thing to do. I would suspect that at best it will block until the thread function has exited.

    Also you are not cheking the return values from pthread_create() either which might be a good idea. In summary I would use a separate instance of thread objects, or add the pthread_join calls to the inside of your loop so that you are certain that the threads are finished running before the next call to pthread_create().

    Finally the function signature for functions passed to pthread_create() are of the type

     void* thread_function(void*);
    

    and not

     void thead_function()
    

    like you have in your code.

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

Sidebar

Related Questions

Im trying to test my successfully creates a new user after login (using authlogic).
I'm trying to test using SFML's full screen mode, however it crashes every time
I am trying to test a sinatra app using minitest and capybara but get
I'm trying to test in-app billing using my own sku/product ID upgrade_to_premium. As I
Benn trying to test a service layer method which returns a simple IList using
Trying to test deploy a simple Rails 3.1 app in production mode, using the
I'm trying to test a client/server application, and using Maven to handle the build/test/deploy.
Trying to test to see if a user is logged in I am using
I was trying to run a small test program on Xcode (4.2) using C++
I am trying to compare the performance of boost::atomic and pthread mutex on Linux:

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.