Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - how to thread across cores / processors

Im (re)writing a socket server in ruby in hopes of simplifying it. Reading about ruby sockets I ran across a site that says multithreaded ruby apps only use one core / processor in a machine.

Questions:

  1. Is this accurate?
  2. Do I care? Each thread in this server will potentially run for several minutes and there will be lots of them. Is the OS (CentOS 6.5) smart enough to share the load?
  3. Is this any different from threading in C++? (language of the current socket server) IE do pthreads use multiple cores automatically?
  4. What if I fork instead of thread?
like image 927
ethrbunny Avatar asked Oct 27 '14 00:10

ethrbunny


People also ask

Can you multithread on one core?

In computer architecture, multithreading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system. This approach differs from multiprocessing.

Can threads run on different cores?

Yes, threads and processes can run concurrently on multi-core CPUs, so this works as you describe (regardless of how you create those threads and processes, OpenMP or otherwise). A single process or thread only runs on a single core at a time.

Is multithreading possible in Ruby?

Multi-threading is the most useful property of Ruby which allows concurrent programming of two or more parts of the program for maximizing the utilization of CPU. Each part of a program is called Thread. So, in other words, threads are lightweight processes within a process.

How are threads allocated to cores?

Thread allocation is managed by the operating system. Threads are created using OS system calls and, if the process happens to run on a multi-core processor, the OS automatically tries to allocate / schedule different threads on different cores. Thread allocation is managed by the programming language implementation.


2 Answers

There is a very nice gem called parallel which allows data processing with parallel threads or multiple processes by forking (work around GIL of current CRuby implementation).

like image 31
David Unric Avatar answered Oct 05 '22 11:10

David Unric


CRuby has a global interpreter lock, so it cannot run threads in parallel. Jruby and some other implementations can do it, but CRuby will never run any kind of code in parallel. This means that, no matter how smart your OS is, it can never share the load.

This is different in threading in C++. pthreads create real OS threads, and the kernal's scheduler will run them on multiple cores at the same time. Technically Ruby uses pthreads as well, but the GIL prevents them from running in parallel.

Fork creates a new process, and your OS's scheduler will almost certainly be smart enough to run it on a separate core. If you need parallelism in Ruby, either use an implementation without a GIL, or use fork.

like image 95
TheSuper Avatar answered Oct 05 '22 09:10

TheSuper