How to get number of active threads in C ?

Discussion in 'Ruby' started by Marc-André Cournoyer, Apr 16, 2008.

  1. Hi all,

    I'm writing a Ruby C extension and I need a fast way to get the number
    of active threads. I got something working for 1.8.6 but it's not
    portable to other Ruby version like 1.9 (because I can't include the
    header for rb_thread_t struct and the like) and it doesn't look very
    sane.

    Here's my code for Ruby 1.8.6:

    #include "node.h"
    ...

    rb_thread_t mainth = (rb_thread_t) RDATA(rb_thread_main())->data;
    rb_thread_t th = mainth;
    size_t num_threads = 0;

    do {
    th = th->next;
    if (th->status != THREAD_KILLED)
    num_threads ++;
    } while (th != mainth);

    printf("%d active threads\n", num_threads);

    I'm wondering if there's a better way to do this. I know there's the
    rb_thread_list, but I would like to avoid creating a new Ruby Array on
    each call.

    Any suggestion?
    thx,
    Marc
    Marc-André Cournoyer, Apr 16, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Crouchez

    Get number of threads in JVM

    Crouchez, Sep 1, 2007, in forum: Java
    Replies:
    10
    Views:
    8,194
  2. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,406
    Arne Vajhøj
    Apr 9, 2008
  3. lmbutler

    Find number of active threads

    lmbutler, Oct 16, 2008, in forum: Python
    Replies:
    0
    Views:
    291
    lmbutler
    Oct 16, 2008
  4. carlos seramos
    Replies:
    2
    Views:
    465
    carlos seramos
    Aug 1, 2003
  5. Skellington
    Replies:
    1
    Views:
    300
    Robert Klemme
    Dec 11, 2012
Loading...

Share This Page