pthreads! - Chad Salinas ::: Data Scientist
Use Slidify to create motion charts to share with the world on rPubs Chad Salinas or GitHub.
Slidify, rPubs Chad Salinas, Motion Charts, Hans Rosling
972
post-template-default,single,single-post,postid-972,single-format-standard,qode-listing-1.0.1,qode-social-login-1.0,qode-news-1.0,qode-quick-links-1.0,qode-restaurant-1.0,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,qode-theme-ver-12.0.1,qode-theme-bridge,bridge,wpb-js-composer js-comp-ver-5.4.2,vc_responsive
a

pthreads!

Pthreads underlies the C++ threading implementations, so we may as well start there.

Try to avoid pthreads by using C++ threading directives, then go to GDB and find yourself right back in pthreads!

pthreads’ package uses void* to be as polymorphic as possible with respect to arguments and return type.
pthread_t is just an integer that is strongly-typed to remind us we are using a thread id.
We create threads by iterating over the pthread_t array with pthread_create.
We need to join the main thread against the spawned threads with a pthread_join so that the newly created threads aren’t being created as main() ends and the environment is being torn down.
printf is thread-safe.  That does not mean that it gets to print an entire string before it gets pulled off the processor, but rather that if any other thread tries to get into the printf it will be blocked as the previous printf thread has a lock.
Stack segment gets subdivided into mini-segments, one for each thread.
Thread manager, like the OS scheduler, during the time-slice for the process, round-robins through the threads, leveraging the same running, runnable,  & blocked constructs as for processes.
Thought Experiment
What’s wrong with trying to print the thread id in the threaded function using an array index?

Chad Salinas working on Plotly

Chad Salinas working on analytics project.

Chad Salinas late nighter

Let the dataset change your mindset

– Hans Rosling
No Comments

Sorry, the comment form is closed at this time.