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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:03:19+00:00 2026-06-07T05:03:19+00:00

I am trying to add two matrices using pthreads.Just a beginners program,but I am

  • 0

I am trying to add two matrices using pthreads.Just a beginners program,but I am unable to initialize the matrix using threads.my code snippet is as follows:

#include "test.h"

CvMat * matA;       /* first matrix */
CvMat * matB;       /* second matrix */
CvMat * matRes;     /* result matrix */

int size_x_a; /* this variable will be used for the first  dimension */
int size_y_a; /* this variable will be used for the second dimension */

int size_x_b,size_y_b;
int size_x_res;
int size_y_res;

int main()
{
/* assigining the values of sizes */
size_x_a = 200;
size_y_a = 200;
size_x_b = 200;
size_y_b = 200;

/* resultant matrix dimensions */
size_x_res = size_x_a;
size_y_res = size_y_b;

matA = cvCreateMat(size_x_a,size_y_a,CV_32SC1);
matB = cvCreateMat(size_x_b,size_y_b,CV_32SC1);
matRes = cvCreateMat(size_x_res,size_y_res,CV_32SC1);

pthread_t thread1;
pthread_t thread2;

int res1;
int res2;
 /*******************************************************************************/ 

    /*Creating a thread*/
res1 = pthread_create(&thread1,NULL,initializeA,(void*)matA);
if(res1!=0)
{
    perror("thread creation of thread1 failed");
    exit(EXIT_FAILURE);
}

for(int i =0;i<size_x_a;i++)
{
    for(int j = 0;j<size_y_a;j++)
    {
        printf("%d ",cvmGet(matA,i,j));
    }
}
    /*Creating a thread*/
res2 = pthread_create(&thread2,NULL,initializeB,(void*)matB);

if(res2!=0)
{
    perror("thread creation of thread2 failed");
    exit(EXIT_FAILURE);
}
return 0;
 }

void * initializeA(void * arg)
{
CvMat * matA  = (CvMat*)arg;
//matA = (CvMat*)malloc(size_x_a * sizeof(CvMat *));

/*initialiazing random values*/
for (int i = 0; i < size_x_a; i++) 
{
    for (int j = 0; j < size_y_a; j++) 
    {
        cvmSet(matA,i,j,size_y_a + j); 
    }
}
return 0;
}

void * initializeB(void * arg)
{
CvMat* matB  = (CvMat*)arg;
//matB = (CvMat*)malloc(size_x_b * sizeof(CvMat *));

/*initialiazing random values*/
for (int i = 0; i < size_x_b; i++) 
{
    for (int j = 0; j < size_y_b; j++) 
    {
      cvmSet(matB,i,j,size_y_b + j); 
    }
}
return 0;
}

I have first created two threads to initialize the CvMat matrix.CvMat matrix is of opencv.
after initializing and calling pthread_create I am trying to access the data of CvMat but it is crashing,means it is not initialized.

Kindly suggest.

Thanks

  • 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-07T05:03:21+00:00Added an answer on June 7, 2026 at 5:03 am

    You obviously have to wait until the initialization is finished before use.

    pthread_create(&thr1, ..., (void *)matA);
    pthread_create(&thr2, ..., (void *)matB);
    
    pthread_join(thr1, &result1);
    pthread_join(thr2, &result2);
    
    for (...) { ... cvmGet(matA,i,j); ... }
    

    As it stands you’re trying to access the matrix during its initialization.

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

Sidebar

Related Questions

I'm trying to add two 4800x9600 matrices, but am running into difficulties... It's a
I am trying to add two decimal values but the returned sum is pure
I'm trying to add two strings with snprintf but apparently i dont know what
as the title states, I am trying to add two floats using javascript/jquery, and
I'm trying to add two tables to magento but it still dosen't work! I
I'm trying to create Pdf from java using iText. How Can I Add two
I'm trying to add two buttons to each cell in a tableview, but when
I'm trying to add two images together using NumPy and PIL. The way I
Today, while I was trying to write code to just add and subtract the
I am trying to add values from two different tables, but one of the

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.