You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this example, the library will prefer a free CPU on the same Socket as the first thread, otherwise it will pick any free CPU.
130
133
131
134
=== Getting the thread id.
132
135
You can get the current thread id using
133
136
[source, java]
134
137
----
135
-
int threadId = AffinitySupport.getThreadId();
138
+
139
+
int threadId = AffinitySupport.getThreadId();
136
140
----
137
141
=== Determining which CPU you are running on.
138
142
You can get the current CPU being used by
139
143
[source, java]
140
144
----
141
-
int cpuId = AffinitySupport.getCpu();
145
+
146
+
int cpuId = AffinitySupport.getCpu();
142
147
----
143
148
=== Controlling the affinity more directly.
144
149
The affinity of the process on start up is
145
150
[source, java]
146
151
----
147
-
long baseAffinity = AffinityLock.BASE_AFFINITY;
152
+
153
+
long baseAffinity = AffinityLock.BASE_AFFINITY;
148
154
----
149
155
The available CPU for reservation is
150
156
[source, java]
151
157
----
152
-
long reservedAffinity = AffinityLock.RESERVED_AFFINITY;
158
+
long reservedAffinity = AffinityLock.RESERVED_AFFINITY;
153
159
----
154
160
If you want to get/set the affinity directly you can do
155
161
[source, java]
156
162
----
157
-
long currentAffinity = AffinitySupport.getAffinity();
158
-
AffinitySupport.setAffinity(1L << 5); // lock to CPU 5.
163
+
long currentAffinity = AffinitySupport.getAffinity();
164
+
AffinitySupport.setAffinity(1L << 5); // lock to CPU 5.
159
165
----
166
+
160
167
=== Debugging affinity state
161
168
162
169
For a detailed of view of the current affinity state (as seen by the library),
@@ -188,18 +195,59 @@ For an article on how much difference affinity can make and how to use it http:/
188
195
189
196
== Questions and Answers
190
197
191
-
=== Question
198
+
=== Question: How to lock a specific cpuId
192
199
I am currently working on a project related to deadlock detection in multithreaded programs in java. We are trying to run threads on different processors and thus came across your github posts regarding the same. https://github.com/peter-lawrey/Java-Thread-Affinity/wiki/Getting-started
193
200
Being a beginner, I have little knowledge and thus need your assistance. We need to know how to run threads on specified cpu number and then switch threads when one is waiting.
0 commit comments