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

  • Home
  • SEARCH
  • 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 4618462
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:16:14+00:00 2026-05-22T02:16:14+00:00

Okay, so I’m having an issue with dynamically allocating work to pthreads in a

  • 0

Okay, so I’m having an issue with dynamically allocating work to pthreads in a queue.

For example, in my code I have a struct like below:

struct calc
{
    double num;
    double calcVal;
};

I store each struct in an array of length l like below.

struct calc **calcArray; 

/* then I initialize the calcArray to say length l and 
   fill each calc struct with a num*/

Now, based on num, I want to find the value of calcVal. Each struct calc has a different value for num.

I want to spawn 4 pthreads which is easy enough but I want to make it so at the start,

thread 0 gets calcArray[0]
thread 1 gets calcArray[1]
thread 2 gets calcArray[2]
thread 3 gets calcArray[3]

Now assuming that it will take different times for each thread to do the calculations for each calc,

if thread 1 finishes first, it will then get calcArray[4]

then thread 3 finishes and gets calcArray[5] to do

and this continues until it reaches the end of calcArray[l].

I know I could just split the array into l/4 (each thread gets one quarter of the calcs) but I don’t want to do this. Instead I want to make the work like a queue. Any ideas on how to do this?

  • 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-22T02:16:14+00:00Added an answer on May 22, 2026 at 2:16 am

    You could accomplish it pretty easily, by creating a variable containing the index of the next element to be assigned, and then having it secured by a mutex.

    Example:

    // Index of next element to be worked on
    int next_pos;
    
    // Mutex that secures next_pos-access
    pthread_mutex_t next_pos_lock;
    
    int main() {
        // ...
    
        // Initialize the mutex before you create any threads 
        pthread_mutex_init(&next_pos_lock, NULL);
    
        next_pos = NUM_THREADS;
    
        // Create the threads
    
        // ...
    }
    
    void *threadfunc(void *arg) {
        int index = ...;
    
        while (index < SIZE_OF_WORK_ARRAY) {
            // Do your work
    
            // Update your index
            pthread_mutex_lock(&next_pos_lock);
            index = next_pos;
            next_pos++;
            pthread_mutex_unlock(&next_pos_lock);
        }
    }
    

    See also: POSIX Threads Programming – Mutex Variables

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

Sidebar

Related Questions

Okay. I have this code on my site: <?php session_start(); include database.php; include bruger.php;
Okay, so I have a snippet of code from a website containing a button:
Okay I got some good advice for Mobile Detection but still having an issue
Okay, here's the scenario. I have a utility that processes tons of records, and
Okay. I know this looks like the typical Why didn't he just Google it
Okay. So I've got a a little jQuery gallery scroller I wrote to work
okay code: #!/usr/bin/python import wx import sys class XPinst(wx.App): def __init__(self, redirect=False, filename=None): wx.App.__init__(self,
Okay, I understand how to work delegation in a modal view to send a
Okay so I have two png files, a circle and a maze. Basically the
Okay, I've been struggling with this for a while now. I have a standard

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.