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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:44:28+00:00 2026-05-27T16:44:28+00:00

I’m asked to write a program that will have 2 threads and print 5

  • 0

I’m asked to write a program that will have 2 threads and print 5 random integers such that the first thread will generate a number, the second will print it. Then the first will generate the 2nd number, the second thread will print it… etc. using a mutex.

My code now execute it for one cycle. How can I extend it to make threads excute the methods 5 times?

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void* generate (void*);
void* print (void*);

pthread_mutex_t m;
int number = 5;
int genNumber;


int main()
{
    int i;
    srandom(getpid());
    pthread_t th[2];

    pthread_mutex_init(&m,NULL);

    pthread_create(&th[0],NULL,generate,NULL);
    pthread_create(&th[1],NULL,print, NULL);

    for (i = 0; i < 2; i++)
        pthread_join(th[i], NULL);

    pthread_mutex_destroy(&m);

    return 0;
}

void* generate(void* arg)
{
    pthread_mutex_lock(&m);
    genNumber = random() % 9;
    printf("Generated #1 \n");
    pthread_mutex_unlock(&m);
}

void* print(void* arg)
{
    pthread_mutex_lock(&m);
    printf("The number is %d " , genNumber);
    pthread_mutex_unlock(&m);
    pthread_exit(NULL);
}
  • 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-27T16:44:29+00:00Added an answer on May 27, 2026 at 4:44 pm

    Use condition variables to synchronize the two threads. When a thread has completed its work, it signals to the other thread to wake up, and then it goes to sleep to wait for more work. So something like this:

    // Pseudocode
    pthread_cond_t c1, c2;
    pthread_mutex_t mutex;
    
    // Thread 1 (producer):
    for(int i = 0; i < 5; i++)
    {
        lock(mutex);
        genNumber = random() % 9;
        signal(c2);
        wait(c1, mutex);
        unlock(mutex);
    }
    
    // Thread 2 (consumer):
    for(int i = 0; i < 5; i++)
    {
        lock(mutex);
        wait(c2, mutex);
        print("The number is %d\n", genNumber);
        signal(c1);
        unlock(mutex);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.