Asterisk
Asterisk

Reputation: 11

How to parallelize threads in C pthreads

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?

Upvotes: 1

Views: 438

Answers (4)

pierrotlefou
pierrotlefou

Reputation: 40721

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.

Upvotes: 0

dmityugov
dmityugov

Reputation: 4478

Use read/write locks if possible

Upvotes: 0

Michael F
Michael F

Reputation: 40832

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.

Upvotes: 0

Benno
Benno

Reputation: 5640

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.

Upvotes: 5

Related Questions