user289013
user289013

Reputation:

How this pthread actually works?

I am actually on my project on compiler with SMP, and want to code with pthreads and heard about many parallel things open mpi and so on, So to start with how this thread is allocated to core while calling pthread,Is there any way to give threads to different cores by pthreads?

Upvotes: 0

Views: 645

Answers (3)

J Teller
J Teller

Reputation: 1431

The simplest answer is yes -- in general different threads will likely run on different cores, automatically without having to specify where the threads run.

It seems like you need an intro/primer on multi-threaded programming in general before diving into a specific threading library/way of thinking. Before doing anything else, read the section "What is a thread" from Amit above, then start looking at either the specifics of POSIX threads, Windows threads, TBB, Boost Threads, or any other library/wrapper looks interesting to you.

Upvotes: 0

nos
nos

Reputation: 229264

The system will assign the threads to available cores, and move them around if there's free resources elsewhere.

The OS usually does a far better job than me in assigning resources, but if you really want to, most OSs have a way of assigning/restricting threads to a particular core/processors. On linux, use sched_setaffinity

Upvotes: 0

amit kumar
amit kumar

Reputation: 21042

Check out a pthreads tutorial such as this. But in general I would recommend using a higher level library than pthreads as it will be simpler/more systematic to use them. For example, Boost::Threads or Intel TBB.

Upvotes: 1

Related Questions