...

View Full Version : C++ Casting Help



chiefbutz
12-03-2008, 12:16 AM
I need a bit of helping with casting. I am working on a program that is multi-threaded using pthread.h and there is one bit of casting that is being a pain.

Here is the output from g++:

g++ -g -o mainPThread pt_main.cpp -lpthread
pt_main.cpp: In function ‘int main(int, char*)’:
pt_main.cpp:86: error: invalid conversion from ‘void (*)(void*)’ to ‘void* (*)(void*)’
pt_main.cpp:86: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
make: *** [mainPThread] Error 1

Here is line 86:

int pt = pthread_create( &thread[a], NULL, runThreadedMult, (void*)rowRange[a]);



Here is my full code:

#include <iostream>
#include <pthread.h>
using namespace std;

void runThreadedMult(void *ptr);
float GMA[2][2];
float GMB[2][2];
float GMC[2][2];
int GN;

int main(int argc, char argv[])
{
int N = 2;
GN = N;
float MatrixA[N][N];
GMA[0][0] = 1.0F; GMA[0][1] = 2.0F;
GMA[1][0] = 2.0F; GMA[1][1] = 1.0F;
float MatrixB[N][N];
GMB[0][0] = 2.0F; GMB[0][1] = 1.0F;
GMB[1][0] = 1.0F; GMB[1][1] = 2.0F;
float MatrixC[N][N];
//GMA = MatrixA;
//GMB = MatrixB;
//GMC = MatrixC;
int tempN = N;
int counter = 0;
int rowRange[10][2]; // [][0] to [][1]
// There will also never
//be more than 10 threads

// We will make this all depend on N
if(N <= 10)
{
counter = 0;
tempN = N;
// We can use 1 thread per 2 rows here
// 1st section is automatically 0,1
rowRange[0][0] = 0;
rowRange[0][1] = 1;
counter++;
tempN -= 2;
//The rest are here
while(tempN > 0)
{
if(tempN > 1)
{
rowRange[counter][0] = rowRange[counter-1][1]+1;
rowRange[counter][1] = rowRange[counter][0] + 1;
counter++;
tempN -= 2;
}
else
{
rowRange[counter][0] = rowRange[counter-1][1];
rowRange[counter][1] = -1; // Used for error/ignore
counter++;
tempN = -1; // We are done if this is odd...
}
}
}
else
{
// Lets use 10 threads max, too many and the CPU will
// become... mad...
// Hopefully 10 will be ok....
tempN = N;
counter = 0;
int mod = N % 10; // This is added to thread 0
int newN = N - mod;
int numPerT = newN / 10;

// Do 1st one outside the loop
rowRange[0][0] = 0;
rowRange[0][1] = (numPerT - 1) + mod;
while(tempN > 0)
{
rowRange[counter][0] = rowRange[counter-1][1] + 1;
rowRange[counter][1] = rowRange[counter][0] + numPerT - 1;
}
}
pthread_t thread[counter];
for(int a = 0; a < counter; a++)
{
try
{
int pt = pthread_create( &thread[a], NULL, runThreadedMult, (void*)rowRange[a]);
}
catch(...)
{
cerr << "Error doing multi-threading" << endl;
}
}
for(int i = 0; i < counter; i++)
{
pthread_join(thread[i], NULL);
}
}

void runThreadedMult(void *ptr)
{
int range[2];
range[0] = (int)ptr;
range[1] = (int)(&ptr + 1);
//range = (int [2])ptr;
for (int i = range[0]; i <= range[1]; i++)
{
for (int j = 0; j < GN; j++)
{
GMC[i][j] = 0.0F;
for (int k = 0; k < GN; k++)
{
GMC[i][j] += GMA[i][k] * GMB[k][j];
}
}
}
}



I realize this may not be the best way to do it, and if you want to suggest another way to do multi-threaded square array multiplication please feel free to say so.

As for my converting from one format of arrays to another, that is due to the requirements of the assignment. Yes, this is an assignment. I just need a little help with this one part. I am pretty sure this doesn't cause any trouble with the rules or anything.

it career
12-06-2008, 01:27 PM
You need to use char *argv[] .



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum