Microsoft MVP성태의 닷넷 이야기
멀티코어사용 방법에 궁금한것이 있습니다. [링크 복사], [링크+제목 복사],
조회: 15694
글쓴 사람
ㅇㅇ
홈페이지
첨부 파일
 

https://www.sysnet.pe.kr/2/0/10933 <-이글을 먼저 읽었음을 알려드립니다.

제가 이해한것을 먼저 말씀드리면, 일단 visualStrudio로 프로젝트 생성해서 프로그램을 실행시키게 되면 .exe파일이 하나의 프로세스 가 되는 것이고 이 프로세스안에 스레드를 100개든,1000개든 만들면 하나의 프로세스에서 각 스레드가 문맥전환이 일어나며 각각 개별 동작을 하는 것으로 알고 있습니다. 하드웨어적으로 보면 결국 CPU코어가 몇개든, 하드웨어스레드가 몇개든 1코어,1스레드만 사용하게 되는 것이죠.

그래서 하나의 프로그램(exe파일)에서 하드웨어 스레드를 2개 혹은 3개씩 쓸 수 있는 방법을 열심히 찾아봤습니다.
답은 내가 사용하고 있는 Thread에서 ProcessorAffinity를 설정하든지, IdealProcessor를 설정하든지 해주면 되는 것이라고 하더군요.

그런데 IdealProcessor를 설정하였더니 ProcessExplorer에서 IdealProcessor가 바뀐것은 확인되었는데.
각 스레드별로 Process.GetCurrentProcess().Id를 콘솔에서 출력해봤더니 같은 Id가 나오네요.
스레드별로 IdealProcessor를 다르게 설정해주었는데 어째서 Id는 같은가요?
IdealProcessor와 Process.GetCurrentProcess().Id의 차이는 무엇인가요?

아래는 제가 테스트해본 소스입니다.
using System;
using System.Threading;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ProcessorAffinity
{
    class Program
    {
        static void Main(string[] args)
        {
            Process Proc = Process.GetCurrentProcess();
            long AffinityMask = (long)Proc.ProcessorAffinity;
            AffinityMask &= 0x000F; // use only any of the first 4 available processors
            Proc.ProcessorAffinity = (IntPtr)AffinityMask;

            Thread t1 = new Thread(Task1Process);
            t1.Start();

            Thread t2 = new Thread(Task2Process);
            t2.Start();
        }


        static void Task1Process()
        {
            int mythreadid = Thread.CurrentThread.ManagedThreadId;
            Process proc = Process.GetCurrentProcess();
            proc.Threads[mythreadid].ProcessorAffinity = (IntPtr)2;

            
            while (true)
            {
                Process modifyproc = Process.GetCurrentProcess();
                Console.WriteLine("나는 1번 스레드이고 아이디는 {0}번, 프로세스 아이디는 {1}", proc.Threads[mythreadid].Id, modifyproc.Id);
                Task.Delay(2000).Wait();
            }
            //Process Id = Process.GetCurrentProcess().Threads;
        }

        static void Task2Process()
        {
            int mythreadid = Thread.CurrentThread.ManagedThreadId;
            Process proc = Process.GetCurrentProcess();
            proc.Threads[mythreadid].ProcessorAffinity = (IntPtr)4;

            

            while (true)
            {
                Process modifyproc = Process.GetCurrentProcess();
                Console.WriteLine("나는 2번 스레드이고 아이디는 {0}번, 프로세스 아이디는 {1}", proc.Threads[mythreadid].Id, modifyproc.Id);
                Task.Delay(2000).Wait();
            }
        }

    }
}








[최초 등록일: ]
[최종 수정일: 3/12/2019]


비밀번호

댓글 작성자
 



2019-03-12 07시33분
Process.GetCurrentProcess().Id는 프로세스의 ID입니다. 프로세서의 ID가 아닙니다.
정성태
2019-03-13 09시02분
[ㅇㅇ] 프로세서와 프로세스차이는 뭔가요?
[guest]
2019-03-13 09시22분
프로세서와 프로세스의 차이
; https://blogger.pe.kr/422
정성태

1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
5841guest3/3/202311668프로그램 판매 시 - Upgrade 버전 판매 [2]
5840joe3/2/202312513C# 클래스 라이브러리 수정 -> C++에서 참조시 함수 목록 갱신되지 않음. [4]파일 다운로드1
5839guest3/2/202312556윈도우 서비스 프로그램 - FORM 애플리케이션 감시서비스 [8]
5838랄랄라3/1/202310886event 사용 시 두 표현의 차이점이 있을까요? [1]
5837감사합니...2/28/202311673오라클 DB서버 접속관련 문의 드립니다.(Load Balancing, HA Events) [2]
5836박지범2/27/202310952static instance의 initialize 순서가 보장되나요? [6]
5835주민호2/25/202318568Windows Software Development Kit - 최신버전 1개 남기고 다 삭제하면 안되는지요? [10]파일 다운로드1
5834guest2/24/202310902Python IDE - 비주얼스튜디오 [3]
5833무지남2/23/202310124Async 메서드 그리고 나서 Bool 메서드 [5]
5832김지우2/21/202310902event와 delegate의 차이 , event를 써야하는 이유 [1]
5831이우람2/20/202312205ref 전역변수가 pinned가 될수 있나요? [2]
5830냉수마찰2/19/202311367C# GridView에 Column별 데이터 추가하는 방법에 대해 [1]
5829수박942/19/202313046키움 API를 윈폼과 WPF의 네임스페이스 없이 콘솔이나 WinUI3에서 사용할 수 있는 방법이 있나요? [2]파일 다운로드1
5828김재영2/19/202311763장기적으로는 this 구문을 안쓰는게 맞을까요? [2]
5827lee2/18/202313484파이썬 설치 오류 질문입니다 [1]
5826Syong2/14/202313271Socket 관련 Leak (OverlappedAsyncResult, OverlappedData) 관련 문의 [7]파일 다운로드1
5825박성원2/14/202312368Listview 컨트롤의 화면 전환 시 갱신 속도 [1]
5823검은콩2/13/202315388catch(Exception ex)의 line번호를 쉽게 알 수 없는지요? [7]
5822김지우2/11/202314724책을 보면서 sync, async 이해가 되지 않는 부분이 있습니다. [5]파일 다운로드2
5821검은콩2/9/202311441Async 신뢰성과 소켓데이터 [4]
5820차가워2/8/202311274다른 프로세스 실행 후 포커스 가져오기 [3]
5819취준생2/7/202311901WPF 관련 실무가 궁금합니다. [3]
5818윤길2/7/202311104ObservableCollection 에서 INotifyPropertyChanged 구현해야하나요? [2]
5817흰털너부리2/7/202310643배포 시 winform 실행 콘솔로그 보는 방법 [1]
5816흰털너부리2/6/202310970.net core json array validation 질문 드립니다. [1]
5815김재영2/6/202310819종단간 암호화에 대해 시나리오인데 타당한 시나리오일까요? [2]
1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...