🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #17284

Re: Threads and CPUs

#17284
From: Early Ehlinger
Date: Thu, 27 Mar 2003 21:17
36 lines
2323 bytes
"Ilan Azbel" <ilan@mdio.net> wrote:
> I have a multi-threaded application running on a dual-CPU computer. Is
there
> any way to find out on which CPU a thread is running?

This question assumes that your thread will stay on the CPU once you ask.
This is not the case (by default):

int thread_func( )
    {
    /*  Pretend GetCurrentThreadCpuNumber() is an API
        that returns the current thread cpu number at
        the time the function was called. */
    int cpu_num = GetCurrentThreadCpuNumber( );

    /*  OOPS! The OS just preempted me and moved me to
        the other CPU, so the next line of output
        will be a flat-out lie! */
    std::cout
        << "I think I\'m running on cpu number "
        << cpu_num << std::endl;
    }

Note that you /can/ force your thread to run on a specific processor via
SetThreadAffinityMask or even suggest a processor to the OS via
SetThreadIdealProcessor.  But in both cases this is not likely to get you
any performance boost, because other threads can still run on that processor
and preempt your thread.


You might be able to get a tell which CPU your thread is running on by using
the CPUID assembly instruction, but it won't do you much good.

--
-- Early Ehlinger CEO, ResPower Inc - Toll-Free : 866-737-7697
-- www.respower.com -- 500+ GHz Supercomputer Starting At USD$0.50/GHz*Hour
Technical Details
Message-ID: <3e836a8a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3e8301a9@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,73f40241b1afbd5b
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-03-27 13:23:27 PST
From: "Early Ehlinger" <early@respower.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3e8301a9@newsgroups.borland.com>
Subject: Re: Threads and CPUs
Date: Thu, 27 Mar 2003 15:17:54 -0600
Lines: 39
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
NNTP-Posting-Host: 206.166.224.226
Message-ID: <3e836a8a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1048799882 206.166.224.226 (27 Mar 2003 13:18:02 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail