Thread Not Working


 
Thread Tools Search this Thread
Top Forums Programming Thread Not Working
# 1  
Old 12-13-2011
Thread Not Working

Hy,
In my current knowledge, i write the code for multiply 3x3 matrix to it self . . . Problem is that pthread_create function is not working. Here is my code

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

int matrixA[3][3] = {{1,1,1},
{1,1,1},
{1,1,1}};
int resultA[3][3] = {0};

void *multiply(void *arg) {
int row = *((int *)(arg));

resultA[row][0] = matrixA[row][0] * matrixA[0][0]
+ matrixA[row][1] * matrixA[1][0]
+ matrixA[row][2] * matrixA[2][0];
resultA[row][1] = matrixA[row][0] * matrixA[0][1]
+ matrixA[row][1] * matrixA[1][1]
+ matrixA[row][1] * matrixA[2][1];
resultA[row][2] = matrixA[row][0] * matrixA[0][2]
+ matrixA[row][1] * matrixA[1][2]
+ matrixA[row][2] * matrixA[2][2];
return NULL;
}

int main( int argc, char *argv[] ) {

pthread_t tid[3];
printf("testing");
int i;
for( i = 0; i < 3; i++ )
{
pthread_create( &tid[0], NULL, multiply, &i);
}
int j;

for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
printf("%d", resultA[i][j]);
}
printf("\n");
}

return 0;
}
SmilieSmilieSmilieSmilie
Please help me to this problem ! Thanks
# 2  
Old 12-13-2011
You're not waiting for the thread to finish. Do so with pthread_join.
# 3  
Old 12-13-2011
is it necessary ?
# 4  
Old 12-13-2011
Yes it is required.
# 5  
Old 12-13-2011
Quote:
Originally Posted by UsmanUrRehman
is it necessary ?
If you don't, main() runs along and prints the results before the threads even start. Then it rolls along and quits before the threads even start. You have to explicitly wait for threads.
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Forum Support Area for Unregistered Users & Account Problems

Not able to post thread/reply to thread

Dear Moderator I am not able to post any new thread or post reply to mine old thread. Kindly help as i am stuck on one problem and needed suggestion. Regards Jaydeep (1 Reply)
Discussion started by: jaydeep_sadaria
1 Replies

2. Solaris

Thread Dump not working

Hello, I'm running a kill -QUIT on few process IDs (OS: Solaris) but the Thread Dump is empty. Can you please help me get it back on? Since I'm quite new to this, I'm not aware of the checks. Please let me know for the info required to debug this issue. Thanks! (3 Replies)
Discussion started by: DevendraG
3 Replies

3. UNIX for Advanced & Expert Users

How to know whether my perodic thread is working fine

Dear All, I am using xenomai-2.4 along with linux kernel 2.6 In my application having following threads. 8ms perodic thread (RT TASK) 1ms perodic thread(RT TASK) 16ms perodic thread(RT TASK) 256ms perodic thread(RT TASK) 22 - pthread are condition based it may execute or else in... (1 Reply)
Discussion started by: rajamohan
1 Replies

4. Programming

How to cancel a thread safely from the initial thread?

how about asynchronous canceling? or with signal? if with signal whether it effects the process? my english so badly :( :( (1 Reply)
Discussion started by: alan.zhao
1 Replies
Login or Register to Ask a Question