views:

116

answers:

4

I have N threads and they have to do job on shared data.

I am using following structure:

int main(){
    pthread_create(..., func, ...);
}

void *func(void *id){
    lock;
    do_job;
    unlock;
}

My problem is that threads seem to work sequentially. How to actually make them parallel?

+6  A: 

They're serialising because you're holding the lock across your entire operation. To actually get parallelism you'd need to do something like:

void func(void *id) {
    lock;
    do something serialised with shared data;
    unlock;

    do something that can be parallelised safely;

    lock;
    do something else with shared data;
    unlock;
}

The trick (as it is in anything threaded or otherwise parallelised) is working out where you need to serialise in order to not break things. This is not easy.

Benno
In your example, you're only creating 1 thread as well. If you do create multiple threads, you'll probably want to do a pthread_join in main on the created threads. If main exits prior to the threads (which is likely) I'm pretty sure the other threads die.
Vitali
I think the point is well taken, though.
San Jacinto
A: 

The sequence of which thread is scheduled to run depend on several things:

  • Thread priority
  • Schedule policy
  • Task attribution.

    On single CPU , unless some thread will be blocked (for example because of waiting I/O), creating several threads will not make the program running fast. Instead, it might make the program slower because of the the task switch overhead.

And also notice the difference between concurrency and parallelism.

pierr
A: 

Hold the lock for as small an operation as possible. If you don't, then the threads' order of execution degenerates into a sequential task (thread N executes and locks the resource, now thread N must finish before any other threads can resume work, thread N finished and thread N+1 executes, assumes the lock, and so forth..). Also try to interleave memory-I/O access (which I'm arbitrarily assuming is what the lock is protecting) with computation, to achieve some greater degree of parallelism.

Michael Foukarakis
A: 

Use read/write locks if possible

dmityugov