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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:30:31+00:00 2026-06-14T21:30:31+00:00

I am attempting to learn about semaphores and multi-threading. The example I am working

  • 0

I am attempting to learn about semaphores and multi-threading. The example I am working with creates 1 to t threads with each thread pointing to the next and the last thread pointing to the first thread. This program allows each thread to sequentially take a turn until all threads have taken n turns. That is when the program ends. The only problem is in the tFunc function, I am busy waiting until it is a specific thread’s turn. I want to know how to use semaphores in order to make all the threads go to sleep and waking up a thread only when it is its turn to execute to improve efficiency.

int turn = 1;
int counter = 0;
int t, n;

struct tData {
        int me;
        int next;
};

void *tFunc(void *arg) {
        struct tData *data;
        data = (struct tData *) arg;
        for (int i = 0; i < n; i++) {
            while (turn != data->me) {
        }
        counter++;
        turn = data->next;
    }
}

int main (int argc, char *argv[]) {
    t = atoi(argv[1]);
    n = atoi(argv[2]);
    struct tData td[t];
    pthread_t threads[t];
    int rc;

    for (int i = 1; i <= t; i++) {
        if (i == t) {
            td[i].me = i;
            td[i].next = 1;
        }
        else {
            td[i].me = i;
            td[i].next = i + 1;
        }
        rc = pthread_create(&threads[i], NULL, tFunc, (void *)&td[i]);
        if (rc) {
            cout << "Error: Unable to create thread, " << rc << endl;
            exit(-1);
        }
    }
    for (int i = 1; i <= t; i++) {
        pthread_join(threads[i], NULL);
    }
    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-06-14T21:30:32+00:00Added an answer on June 14, 2026 at 9:30 pm

    Uses mutexes and condition variables. Here’s a working example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    int turn = 1;
    int counter = 0;
    int t, n;
    
    struct tData {
            int me;
            int next;
    };
    
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    
    void *tFunc(void *arg)
    {
        struct tData *data;
        data = (struct tData *) arg;
        pthread_mutex_lock(&mutex);
        for (int i = 0; i < n; i++)
        {
            while (turn != data->me)
                pthread_cond_wait(&cond, &mutex);
            counter++;
            turn = data->next;
            printf("%d goes (turn %d of %d), %d next\n", data->me, i+1, n, turn);
            pthread_cond_broadcast(&cond);
        }
        pthread_mutex_unlock(&mutex);
    }
    
    int main (int argc, char *argv[]) {
        t = atoi(argv[1]);
        n = atoi(argv[2]);
        struct tData td[t + 1];
        pthread_t threads[t + 1];
        int rc;
    
        pthread_mutex_init(&mutex, NULL);
        pthread_cond_init(&cond, NULL);
    
        for (int i = 1; i <= t; i++)
        {
            td[i].me = i;
            if (i == t)
                td[i].next = 1;
            else
                td[i].next = i + 1;
    
            rc = pthread_create(&threads[i], NULL, tFunc, (void *)&td[i]);
            if (rc)
            {
                printf("Error: Unable to create thread: %d\n", rc);
                exit(-1);
            }
        }
        void *ret;
        for (int i = 1; i <= t; i++)
            pthread_join(threads[i], &ret);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to learn about creating windows in c++, I have looked at
I'm attempting to learn a little more about handling sockets and network connections in
I am attempting to implement Heap sort in my program to learn more about
I'm following this tutorial , attempting to learn a bit about iPhone development, and
While attempting to learn more about sorting algorithims and F#, I wrote an Insertion
I am attempting to learn more about C and its arcane hidden powers, and
I am working on a class that utilises a UdpClient, and attempting to learn/utilise
Attempting to learn functional C#, I ported Dustin Campbell's fibonacci example to C#3. My
I am attempting to learn more about this to implement in my project. I
I'm attempting to learn a bit more about out how grid filtering works in

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.