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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:14:49+00:00 2026-05-28T00:14:49+00:00

Here is the thing: there is a float array float bucket[5] and 2 threads,

  • 0

Here is the thing: there is a float array float bucket[5] and 2 threads, say thread1 and thread2.

Thread1 is in charge of tanking up the bucket, assigning each element in bucket a random number. When the bucket is tanked up, thread2 will access bucket and read its elements.

Here is how I do the job:

float bucket[5];
pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
pthread_t thread1, thread2;

void* thread_1_proc(void*);  //thread1's startup routine, tank up the bucket 
void* thread_2_proc(void*);  //thread2's startup routine, read the bucket 

int main()
{
    pthread_create(&thread1, NULL, thread_1_proc, NULL);
    pthread_create(&thread2, NULL, thread_2_proc, NULL);
    pthread_join(thread1);
    pthread_join(thread2);
}

Below is my implementation for thread_x_proc:

void* thread_1_proc(void*)
{
    while(1) { //make it work forever
        pthread_mutex_lock(&mu);  //lock the mutex, right?

        cout << "tanking\n";
        for(int i=0; i<5; i++)
            bucket[i] = rand();  //actually, rand() returns int, doesn't matter

        pthread_mutex_unlock(&mu); //bucket tanked, unlock the mutex, right?

        //sleep(1); /* this line is commented */
    }
}

void* thread_2_proc(void*)
{
    while(1) { 
        pthread_mutex_lock(&mu);

        cout << "reading\n";
        for(int i=0; i<5; i++)
            cout << bucket[i] << " ";  //read each element in the bucket

        pthread_mutex_unlock(&mu); //reading done, unlock the mutex, right?

        //sleep(1); /* this line is commented */
    }
}

Question

Is my implementation right? Cuz the output is not as what I expected.

...
reading
5.09434e+08 6.58441e+08 1.2288e+08 8.16198e+07 4.66482e+07 7.08736e+08 1.33455e+09 
reading
5.09434e+08 6.58441e+08 1.2288e+08 8.16198e+07 4.66482e+07 7.08736e+08 1.33455e+09 
reading
5.09434e+08 6.58441e+08 1.2288e+08 8.16198e+07 4.66482e+07 7.08736e+08 1.33455e+09 
reading
tanking
tanking
tanking
tanking
...

But if I uncomment the sleep(1); in each thread_x_proc function, the output is right, tanking and reading follow each other, like this:

...
tanking
reading
1.80429e+09 8.46931e+08 1.68169e+09 1.71464e+09 1.95775e+09 4.24238e+08 7.19885e+08 
tanking
reading
1.64976e+09 5.96517e+08 1.18964e+09 1.0252e+09 1.35049e+09 7.83369e+08 1.10252e+09 
tanking
reading
2.0449e+09 1.96751e+09 1.36518e+09 1.54038e+09 3.04089e+08 1.30346e+09 3.50052e+07
...

Why? Should I use sleep() when using mutex?

  • 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-28T00:14:50+00:00Added an answer on May 28, 2026 at 12:14 am

    Your code is technically correct, but it does not make a lot of sense, and it does not do what you assume.

    What your code does is, it updates a section of data atomically, and reads from that section, atomically. However, you don’t know in which order this happens, nor how often the data is written to before being read (or if at all!).

    What you probably wanted is generate exactly one sequence of numbers in one thread every time and read exactly one new sequence each time in the other thread. For this, you would use either have to use an additional semaphore or better a single-producer-single-consumer queue.

    In general the answer to “when should I use a mutex” is “never, if you can help it”. Threads should send messages, not share state. This makes a mutex most of the time unnecessary, and offers parallelism (which is the main incentive for using threads in the first place).

    The mutex makes your threads run lockstep, so you could as well just run in a single thread.

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

Sidebar

Related Questions

I have created database table. But there is a strange thing I can insert
Let's say I have a class like so: class Gerbil{ int id; float x,y,z;
How to compare the similarity between two arrays? Say I have: Base Array: [.5,0,0,0,.25,0,0,.25,0,0,0,0]
Is there anything I've forgotten to do here in order to speed things up
I have a List, MyStuff has a property of Type Float. There are objects
Here are two methods that return a dictionary of my custom four-propery objects. They
I'm a beginner with iPhone dev in Objective C and one thing I find
Working on some matrix code, I'm concerned of performance issues. here's how it works
Here is my issue. I have a std::vector<POINTFLOAT> Which stores verticies. The issue is
The basic idea is very easy. Simplified you could say... a snake like line

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.