How to get multithreads working properly using pthreads and not boost in class using C++
<p>I have a project that I have summarized here with some pseudo code to illustrate the problem.
I do not have a compiler issue and my code compiles well whether it be using boost or pthreads. Remember this is pseudo code designed to illustrate the problem and not directly compilable.</p>

<p>The problem I am having is that for a multithreaded function the memory usage and processing time is always greater than if the same function is acheived using serial programming e.g for/while loop.</p>

<p>Here is a simplified version of the problem I am facing:</p>

<pre><code>class aproject(){

public:

typedef struct
{
char** somedata;
double output,fitness;
}entity;

entity **entity_array;

int whichthread,numthreads;
pthread_mutex_t mutexdata;



aproject(){
numthreads = 100;
*entity_array=new entity[numthreads];
for(int i;i&lt;numthreads;i++){
entity_array[i]-&gt;somedata[i] = new char[100];
}

/*.....more memory allocations for entity_array.......*/
this-&gt;initdata();
this-&gt;eval_thread();

}
void initdata(){
/**put zeros and ones in entity_array**/

}
float somefunc(char *somedata){

float output=countzero();       //someother function not listed

return output;
}

void* thread_function()
{
    pthread_mutex_lock (&amp;mutexdata);

    int currentthread = this-&gt;whichthread;
    this-&gt;whichthread+=1;

        pthread_mutex_unlock (&amp;mutexdata);

    entity *ent = this-&gt;entity_array[currentthread];


    double A=0,B=0,C=0,D=0,E=0,F=0;
    int i,j,k,l;



         A = somefunc(ent-&gt;somedata[0]);
         B = somefunc(ent-&gt;somedata[1]);

        t4 = anotherfunc(A,B);



        ent-&gt;output   = t4;
        ent-&gt;fitness  = sqrt(pow(t4,2)); 





                    }



static void* staticthreadproc(void* p){


return reinterpret_cast&lt;ga*&gt;(p)-&gt;thread_function();

}


void eval_thread(){

//use multithreading to evaluate individuals in parallel 

int i,j,k;
nthreads = this-&gt;numthreads;
pthread_t threads[nthreads];

//create threads
pthread_mutex_init(&amp;this-&gt;mutexdata,NULL);
this-&gt;whichthread=0;

for(i=0;i&lt;nthreads;i++){

pthread_create(&amp;threads[i],NULL,&amp;ga::staticthreadproc,this);

//printf("creating thread, %d\n",i);
}


//join threads
for(i=0;i&lt;nthreads;i++){

pthread_join(threads[i],NULL);

}

}

};
</code></pre>

<p>I am using pthreads here because it works better than boost on machines with less memory.
Each thread is started in eval_thread and terminated there aswell. I am using a mutex to ensure every thread starts with the correct index for the entity_array, as each thread only applys its work to its respective entity_array indexed by the variable this->whichthread. This variable is the only thing that needs to be locked by the mutex as it is updated for every thread and must not be changed by other threads. You can happily ignore everything else apart from the thread_function, eval_threads, and the staticthreadproc as they are the only relevent functions assume that all the other functions apart from init to be both processor and memory intensive.</p>

<p>So my question is why is it that using multithreading in this way is IT more costly in memory and speed than the traditional method of <strong>not</strong> using threads at all? </p>

<p><strong>I MUST REITERATE THE CODE IS PSEUDO CODE AND THE PROBLEM ISNT WHETHER IT WILL COMPILE</strong></p>

<p>Thanks, I would appreciate any suggestions you might have for pthreads and/or boost solutions.</p>
