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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:06:17+00:00 2026-06-14T08:06:17+00:00

I am writing a program for an assignment,which implements matrix multiplication between two given

  • 0

I am writing a program for an assignment,which implements matrix multiplication between two given arrays,using threads.

I must give the number of threads i want to use as a command line argument and if the number of them is smaller than the number of lines of the first array,reuse some the same threads until the whole job is done.

I have managed to make it work but only using a thread for each row of the array.
For example if a have a 5×5 multiplication and use less than 5 threads i take a segmentation fault,which makes sense.

My question is:How can i reuse a thread after this thread has finished its job?
In the previous example i mean that if i want to use 2 threads for a 5×5 my program should work like this:

thread 1->line 1

thread 2->line 2

thread 2->line 3

thread 1->line 4

etc.

  • 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-14T08:06:18+00:00Added an answer on June 14, 2026 at 8:06 am

    You have several possibilities, but the main idea is that you must monitor threads to detect when they have finished their job, and have a way to know if there’s still something left to do.

    The first way that comes to mind is to have a dedicated thread to track all the running threads, and recycle them once they’re finished. But in order to do that, you need a mechanism to let threads synchronize, which may be implemented using semaphores or mutex, or messages, but it could be cumbersome to code just for that purpose if you don’t have a need for it already.

    The second way is simply to ask them to recycle themselves, since they know when they are done. In many other languages, there’s a mechanism called continuations, which let you exactly that, but since we’re dealing with C, we need to do that by hand. Luckily, here the continuation is actually just one single task.

    So the mechanism to call the continuation is actually just a function, which will first run the task to be executed by the thread, and then either:

    • check a task list. This task list must be filled at setup time with all the work which need to be done by the tasks,
    • add the thread to a queue of available threads, which must be checked again by some other thread and reassigned a new task,

    Obviously, the first option would be easier, and in your case, you already know at setup time what has to be done, so your setup function could fill up a list of tasks and then launch as many threads as you want, and let them do the recycling themselves.

    Here’s a simple skeleton you could start with:

     typedef struct {
        /* task related data */
     } task_t;
    
     // basic list structure
     typedef struct {
       list_t *next;
       void *data; // here app specific data
     } list_t;
    
     list_t task_list; // your task list
    
     // a few operators to manipulate a list
     // implementation must use a mutex to avoid race conditions
     void list_push(list *l, void *data);
     void *list_pop(list *l);
    
    
     // thread function
    
     void do_task(task_t *task){
         while (task) {
           run_task(task); // that would be the matrix related function
           task = list_pop(&task_list);
     }
    
     // here a simple define for the number of threads
     // you might want to check the number of available cores instead
    
     #define MAX_THREAD_COUNT 4 
    
     int main() {
    
       pthread_t threads[MAX_THREAD_COUNT];
    
       setup_task_list();  // push all the work that must be done in the list
       int i;
       for (i = 0; i < MAX_THREAD_COUNT; i++) {
          pthread_create(threads + i, NULL, do_task, list_pop(&task_list));
       }
    
       // here wait for all the threads, or detach them
    
     }
    

    This is a basic outline of what you could do, and should get you started.
    There are several questions on SO dealing with C linked lists. The one here must be synchronized, should not block, and return NULL when empty.

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

Sidebar

Related Questions

I was writing a program to concatenate two arrays in C. I am allocating
I am writing a grading program for an assignment in which students are implementing
I'm writing a program for an assignment which has to implement LZW compression/decompression. I'm
For a uni assignment I'm writing a java program which needs to save and
I am writing a quote-matching program in which two Abstract Factory Patterns are required,
I am currently writing a program using Weka that builds a model (using one
I'm writing a program that's parsing an XML file to java objects using smooks.
I'm writing a program about image-processing. I need to store an int square matrix
I am writing a program in python and using tarfile to extract tarfiles. Some
Recently for a programming class, we were given the assignment to write a program

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.