sl@0
|
1 |
/** @file ../inc/pthread.h
|
sl@0
|
2 |
@internalComponent
|
sl@0
|
3 |
*/
|
sl@0
|
4 |
|
sl@0
|
5 |
/** @def POSIX_THREAD_KEYS_MAX
|
sl@0
|
6 |
|
sl@0
|
7 |
Represents maximum value
|
sl@0
|
8 |
|
sl@0
|
9 |
@publishedAll
|
sl@0
|
10 |
@externallyDefinedApi
|
sl@0
|
11 |
*/
|
sl@0
|
12 |
|
sl@0
|
13 |
/** @def PTHREAD_STACK_MIN
|
sl@0
|
14 |
|
sl@0
|
15 |
Minimum supported stack size for a thread
|
sl@0
|
16 |
|
sl@0
|
17 |
@publishedAll
|
sl@0
|
18 |
@externallyDefinedApi
|
sl@0
|
19 |
*/
|
sl@0
|
20 |
|
sl@0
|
21 |
/** @def POSIX_THREAD_THREADS_MAX
|
sl@0
|
22 |
|
sl@0
|
23 |
Thread max. Value is 64
|
sl@0
|
24 |
|
sl@0
|
25 |
@publishedAll
|
sl@0
|
26 |
@externallyDefinedApi
|
sl@0
|
27 |
*/
|
sl@0
|
28 |
|
sl@0
|
29 |
/** @def PTHREAD_THREADS_MAX
|
sl@0
|
30 |
|
sl@0
|
31 |
Maximum number of threads supported per process. Value is 1024
|
sl@0
|
32 |
|
sl@0
|
33 |
@publishedAll
|
sl@0
|
34 |
@externallyDefinedApi
|
sl@0
|
35 |
*/
|
sl@0
|
36 |
|
sl@0
|
37 |
/** @def SEM_VALUE_MAX
|
sl@0
|
38 |
|
sl@0
|
39 |
Semaphores have a maximum value past which they cannot be incremented. The macro SEM_VALUE_MAX is defined to be this maximum value.
|
sl@0
|
40 |
|
sl@0
|
41 |
@publishedAll
|
sl@0
|
42 |
@externallyDefinedApi
|
sl@0
|
43 |
*/
|
sl@0
|
44 |
|
sl@0
|
45 |
/** @def PTHREAD_COND_INITIALIZER
|
sl@0
|
46 |
|
sl@0
|
47 |
Value is 4. Describes condition initializer.
|
sl@0
|
48 |
|
sl@0
|
49 |
@publishedAll
|
sl@0
|
50 |
@externallyDefinedApi
|
sl@0
|
51 |
*/
|
sl@0
|
52 |
|
sl@0
|
53 |
/** @def SEM_NSEMS_MAX
|
sl@0
|
54 |
|
sl@0
|
55 |
The maximum number of semaphores a process can have.value is 1024
|
sl@0
|
56 |
|
sl@0
|
57 |
@publishedAll
|
sl@0
|
58 |
@released
|
sl@0
|
59 |
*/
|
sl@0
|
60 |
|
sl@0
|
61 |
/** @def POSIX_SEM_NSEMS_MAX
|
sl@0
|
62 |
|
sl@0
|
63 |
Number of semaphores a process can have.
|
sl@0
|
64 |
|
sl@0
|
65 |
@publishedAll
|
sl@0
|
66 |
@released
|
sl@0
|
67 |
*/
|
sl@0
|
68 |
|
sl@0
|
69 |
/** @def POSIX_THREAD_DESTRUCTOR_ITERATIONS
|
sl@0
|
70 |
|
sl@0
|
71 |
Controlling the iterations of destructors for thread-specific data.
|
sl@0
|
72 |
|
sl@0
|
73 |
@publishedAll
|
sl@0
|
74 |
@released
|
sl@0
|
75 |
*/
|
sl@0
|
76 |
|
sl@0
|
77 |
/** @typedef typedef int pthread_once_t
|
sl@0
|
78 |
|
sl@0
|
79 |
Used for dynamic package initialization.
|
sl@0
|
80 |
|
sl@0
|
81 |
@publishedAll
|
sl@0
|
82 |
@externallyDefinedApi
|
sl@0
|
83 |
*/
|
sl@0
|
84 |
|
sl@0
|
85 |
/** @typedef typedef struct _pthread_condattr_t* pthread_condattr_t
|
sl@0
|
86 |
|
sl@0
|
87 |
Used to identify a condition attribute object
|
sl@0
|
88 |
|
sl@0
|
89 |
@publishedAll
|
sl@0
|
90 |
@externallyDefinedApi
|
sl@0
|
91 |
*/
|
sl@0
|
92 |
|
sl@0
|
93 |
/** @struct pthread_mutex_t
|
sl@0
|
94 |
|
sl@0
|
95 |
Thread mutex type. Used for mutexes.
|
sl@0
|
96 |
|
sl@0
|
97 |
@publishedAll
|
sl@0
|
98 |
@externallyDefinedApi
|
sl@0
|
99 |
*/
|
sl@0
|
100 |
|
sl@0
|
101 |
/** @struct pthread_mutexattr_t
|
sl@0
|
102 |
|
sl@0
|
103 |
Used to identify a mutex attribute object.
|
sl@0
|
104 |
|
sl@0
|
105 |
@publishedAll
|
sl@0
|
106 |
@externallyDefinedApi
|
sl@0
|
107 |
*/
|
sl@0
|
108 |
|
sl@0
|
109 |
/** @struct pthread_cond_t
|
sl@0
|
110 |
|
sl@0
|
111 |
Used for condition variables.
|
sl@0
|
112 |
|
sl@0
|
113 |
@publishedAll
|
sl@0
|
114 |
@externallyDefinedApi
|
sl@0
|
115 |
*/
|
sl@0
|
116 |
|
sl@0
|
117 |
/** @fn pthread_create(pthread_t *threadhdl, pthread_attr_t *attrib, thread_begin_routine begin_routine, void *param)
|
sl@0
|
118 |
@param threadhdl
|
sl@0
|
119 |
@param attrib
|
sl@0
|
120 |
@param begin_routine
|
sl@0
|
121 |
@param *param
|
sl@0
|
122 |
@return If successful, the pthread_create function will return zero.
|
sl@0
|
123 |
Otherwise an error number will be returned to
|
sl@0
|
124 |
indicate the error.
|
sl@0
|
125 |
|
sl@0
|
126 |
The pthread_create function is used to create a new thread, with attributes specified by attrib ,
|
sl@0
|
127 |
within a process.
|
sl@0
|
128 |
If attrib is NULL ,
|
sl@0
|
129 |
the default attributes are used.
|
sl@0
|
130 |
If the attributes specified by attrib are modified later, the thread's attributes are not affected.
|
sl@0
|
131 |
Upon
|
sl@0
|
132 |
successful completion pthread_create will store the ID of the created thread in the location specified by threadhdl .
|
sl@0
|
133 |
|
sl@0
|
134 |
The thread is created executing begin_routine with param as its sole argument.
|
sl@0
|
135 |
If the begin_routine returns, the effect is as if there was an implicit call to pthread_exit using the return value of start_routine as the exit status.
|
sl@0
|
136 |
Note that the thread in which main was originally invoked differs from this.
|
sl@0
|
137 |
When it returns from main ,
|
sl@0
|
138 |
the effect is as if there was an implicit call to exit using the return value of main as the exit status.
|
sl@0
|
139 |
|
sl@0
|
140 |
|
sl@0
|
141 |
|
sl@0
|
142 |
Examples:
|
sl@0
|
143 |
@code
|
sl@0
|
144 |
void *a_thread_func_1_1(void*)
|
sl@0
|
145 |
{
|
sl@0
|
146 |
return NULL;
|
sl@0
|
147 |
}
|
sl@0
|
148 |
int XYZ()
|
sl@0
|
149 |
{
|
sl@0
|
150 |
pthread_t new_th;
|
sl@0
|
151 |
if(pthread_create(&new;_th, NULL, a_thread_func_1_1, NULL) != 0)
|
sl@0
|
152 |
{
|
sl@0
|
153 |
perror("Error creating thread
|
sl@0
|
154 |
");
|
sl@0
|
155 |
return -1;
|
sl@0
|
156 |
}
|
sl@0
|
157 |
}
|
sl@0
|
158 |
|
sl@0
|
159 |
@endcode
|
sl@0
|
160 |
@see pthread_exit()
|
sl@0
|
161 |
@see pthread_join()
|
sl@0
|
162 |
|
sl@0
|
163 |
|
sl@0
|
164 |
|
sl@0
|
165 |
|
sl@0
|
166 |
@publishedAll
|
sl@0
|
167 |
@externallyDefinedApi
|
sl@0
|
168 |
*/
|
sl@0
|
169 |
|
sl@0
|
170 |
/** @fn pthread_self(void)
|
sl@0
|
171 |
|
sl@0
|
172 |
@return The pthread_self function returns the thread ID of the calling thread.
|
sl@0
|
173 |
|
sl@0
|
174 |
The pthread_self function returns the thread ID of the calling thread.
|
sl@0
|
175 |
|
sl@0
|
176 |
|
sl@0
|
177 |
|
sl@0
|
178 |
Examples:
|
sl@0
|
179 |
@code
|
sl@0
|
180 |
pthread_t new_th2;
|
sl@0
|
181 |
new_th2=pthread_self();
|
sl@0
|
182 |
|
sl@0
|
183 |
@endcode
|
sl@0
|
184 |
@see pthread_create()
|
sl@0
|
185 |
@see pthread_equal()
|
sl@0
|
186 |
|
sl@0
|
187 |
|
sl@0
|
188 |
|
sl@0
|
189 |
|
sl@0
|
190 |
@publishedAll
|
sl@0
|
191 |
@externallyDefinedApi
|
sl@0
|
192 |
*/
|
sl@0
|
193 |
|
sl@0
|
194 |
/** @fn pthread_equal(pthread_t t1, pthread_t t2)
|
sl@0
|
195 |
@param t1
|
sl@0
|
196 |
@param t2
|
sl@0
|
197 |
@return The pthread_equal function will return non-zero if the thread IDs t1 and t2 correspond to the same thread, otherwise it will return zero.
|
sl@0
|
198 |
|
sl@0
|
199 |
The pthread_equal function compares the thread IDs t1 and t2 .
|
sl@0
|
200 |
|
sl@0
|
201 |
Examples:
|
sl@0
|
202 |
@code
|
sl@0
|
203 |
void *a_thread_func_1_2(void*)
|
sl@0
|
204 |
{
|
sl@0
|
205 |
pthread_exit(0);
|
sl@0
|
206 |
return NULL;
|
sl@0
|
207 |
}
|
sl@0
|
208 |
void XYZ()
|
sl@0
|
209 |
{
|
sl@0
|
210 |
pthread_t new_th1, new_th2;
|
sl@0
|
211 |
/* Create a new thread. */
|
sl@0
|
212 |
if(pthread_create(&new;_th1, NULL, a_thread_func_1_2, NULL) != 0)
|
sl@0
|
213 |
{
|
sl@0
|
214 |
perror("Error creating thread
|
sl@0
|
215 |
");
|
sl@0
|
216 |
return -1;
|
sl@0
|
217 |
}
|
sl@0
|
218 |
/* Create another new thread. */
|
sl@0
|
219 |
if(pthread_create(&new;_th2, NULL, a_thread_func_1_2, NULL) != 0)
|
sl@0
|
220 |
{
|
sl@0
|
221 |
perror("Error creating thread
|
sl@0
|
222 |
");
|
sl@0
|
223 |
return -1;
|
sl@0
|
224 |
}
|
sl@0
|
225 |
/* Call pthread_equal() and pass to it the 2 new threads.
|
sl@0
|
226 |
* It should return a zero value, indicating that
|
sl@0
|
227 |
* they are not equal. */
|
sl@0
|
228 |
if(pthread_equal(new_th1, new_th2) != 0)
|
sl@0
|
229 |
{
|
sl@0
|
230 |
printf("pthread_equal FAILED
|
sl@0
|
231 |
");
|
sl@0
|
232 |
return -1;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
}
|
sl@0
|
235 |
|
sl@0
|
236 |
@endcode
|
sl@0
|
237 |
@see pthread_create()
|
sl@0
|
238 |
@see pthread_exit()
|
sl@0
|
239 |
|
sl@0
|
240 |
|
sl@0
|
241 |
|
sl@0
|
242 |
|
sl@0
|
243 |
@publishedAll
|
sl@0
|
244 |
@externallyDefinedApi
|
sl@0
|
245 |
*/
|
sl@0
|
246 |
|
sl@0
|
247 |
/** @fn pthread_join(pthread_t thrHandle, void **retValPtr)
|
sl@0
|
248 |
@param thrHandle
|
sl@0
|
249 |
@param retValPtr
|
sl@0
|
250 |
@return If successful, the pthread_join function will return zero.
|
sl@0
|
251 |
Otherwise an error number will be returned to
|
sl@0
|
252 |
indicate the error.
|
sl@0
|
253 |
|
sl@0
|
254 |
The pthread_join function suspends execution of the calling thread until the target thrHandle terminates unless the target thrHandle has already terminated.
|
sl@0
|
255 |
|
sl@0
|
256 |
On return from a successful pthread_join call with a non-NULL retValPtr argument, the value passed to pthread_exit by the terminating thread is stored in the location referenced by retValPtr .
|
sl@0
|
257 |
When a pthread_join returns successfully, the target thread has been terminated.
|
sl@0
|
258 |
The results
|
sl@0
|
259 |
of multiple simultaneous calls to pthread_join specifying the same target thread are undefined.
|
sl@0
|
260 |
|
sl@0
|
261 |
|
sl@0
|
262 |
|
sl@0
|
263 |
Examples:
|
sl@0
|
264 |
@code
|
sl@0
|
265 |
/* Thread's function. */
|
sl@0
|
266 |
void *a_thread_func_1_1(void*)
|
sl@0
|
267 |
{
|
sl@0
|
268 |
int i;
|
sl@0
|
269 |
printf("Wait for 3 seconds for thread to finish execution:0);
|
sl@0
|
270 |
for(i=1;i<4;i++)
|
sl@0
|
271 |
{
|
sl@0
|
272 |
printf("Waited (%d) second0, i);
|
sl@0
|
273 |
sleep(1);
|
sl@0
|
274 |
}
|
sl@0
|
275 |
return NULL;
|
sl@0
|
276 |
}
|
sl@0
|
277 |
int XYZ()
|
sl@0
|
278 |
{
|
sl@0
|
279 |
pthread_t new_th;
|
sl@0
|
280 |
/* Create a new thread. */
|
sl@0
|
281 |
if(pthread_create(&new;_th, NULL, a_thread_func_1_1, NULL) != 0)
|
sl@0
|
282 |
{
|
sl@0
|
283 |
perror("Error creating thread
|
sl@0
|
284 |
");
|
sl@0
|
285 |
return -1;
|
sl@0
|
286 |
}
|
sl@0
|
287 |
/* Wait for thread to return */
|
sl@0
|
288 |
if(pthread_join(new_th, NULL) != 0)
|
sl@0
|
289 |
{
|
sl@0
|
290 |
perror("Error in pthread_join()
|
sl@0
|
291 |
");
|
sl@0
|
292 |
return -1;
|
sl@0
|
293 |
}
|
sl@0
|
294 |
|
sl@0
|
295 |
@endcode
|
sl@0
|
296 |
@see wait()
|
sl@0
|
297 |
@see pthread_create()
|
sl@0
|
298 |
|
sl@0
|
299 |
|
sl@0
|
300 |
|
sl@0
|
301 |
|
sl@0
|
302 |
@publishedAll
|
sl@0
|
303 |
@externallyDefinedApi
|
sl@0
|
304 |
*/
|
sl@0
|
305 |
|
sl@0
|
306 |
/** @fn pthread_detach(pthread_t thrHandle)
|
sl@0
|
307 |
@param thrHandle
|
sl@0
|
308 |
@return If successful, the pthread_detach function will return zero.
|
sl@0
|
309 |
Otherwise an error number will be returned to
|
sl@0
|
310 |
indicate the error.
|
sl@0
|
311 |
|
sl@0
|
312 |
The pthread_detach function is used to indicate to the implementation that storage for the
|
sl@0
|
313 |
thread thrHandle can be reclaimed when the thread terminates.
|
sl@0
|
314 |
If thrHandle has not terminated, pthread_detach will not cause it to terminate.
|
sl@0
|
315 |
The effect of multiple pthread_detach calls on the same target thread is unspecified.
|
sl@0
|
316 |
|
sl@0
|
317 |
Examples:
|
sl@0
|
318 |
@code
|
sl@0
|
319 |
void *a_thread_func_1_1(void*)
|
sl@0
|
320 |
{
|
sl@0
|
321 |
sleep(10);
|
sl@0
|
322 |
return NULL;
|
sl@0
|
323 |
}
|
sl@0
|
324 |
int main_1_1()
|
sl@0
|
325 |
{
|
sl@0
|
326 |
pthread_attr_t new_attr;
|
sl@0
|
327 |
pthread_t new_th;
|
sl@0
|
328 |
int ret;
|
sl@0
|
329 |
/* Initialize attribute */
|
sl@0
|
330 |
if(pthread_attr_init(&new;_attr) != 0)
|
sl@0
|
331 |
{
|
sl@0
|
332 |
perror("Cannot initialize attribute object
|
sl@0
|
333 |
");
|
sl@0
|
334 |
return -1;
|
sl@0
|
335 |
}
|
sl@0
|
336 |
/* Set the attribute object to be joinable */
|
sl@0
|
337 |
if(pthread_attr_setdetachstate(&new;_attr, PTHREAD_CREATE_JOINABLE) != 0)
|
sl@0
|
338 |
{
|
sl@0
|
339 |
perror("Error in pthread_attr_setdetachstate()
|
sl@0
|
340 |
");
|
sl@0
|
341 |
return -1;
|
sl@0
|
342 |
}
|
sl@0
|
343 |
/* Create the thread */
|
sl@0
|
344 |
if(pthread_create(&new;_th, &new;_attr, a_thread_func_1_1, NULL) != 0)
|
sl@0
|
345 |
{
|
sl@0
|
346 |
perror("Error creating thread
|
sl@0
|
347 |
");
|
sl@0
|
348 |
return -1;
|
sl@0
|
349 |
}
|
sl@0
|
350 |
/* Detach the thread. */
|
sl@0
|
351 |
if(pthread_detach(new_th) != 0)
|
sl@0
|
352 |
{
|
sl@0
|
353 |
printf("Error detaching thread
|
sl@0
|
354 |
");
|
sl@0
|
355 |
return -1;
|
sl@0
|
356 |
}
|
sl@0
|
357 |
|
sl@0
|
358 |
@endcode
|
sl@0
|
359 |
@see pthread_join()
|
sl@0
|
360 |
|
sl@0
|
361 |
|
sl@0
|
362 |
|
sl@0
|
363 |
|
sl@0
|
364 |
@publishedAll
|
sl@0
|
365 |
@externallyDefinedApi
|
sl@0
|
366 |
*/
|
sl@0
|
367 |
|
sl@0
|
368 |
/** @fn pthread_exit(void *retValPtr)
|
sl@0
|
369 |
@param retValPtr
|
sl@0
|
370 |
@return The pthread_exit function cannot return to its caller.
|
sl@0
|
371 |
|
sl@0
|
372 |
The pthread_exit function terminates the calling thread and makes the value retValPtr available to any successful join with the terminating thread.
|
sl@0
|
373 |
Thread termination does not release any application
|
sl@0
|
374 |
visible process resources, including, but not limited to, mutexes and
|
sl@0
|
375 |
file descriptors, nor does it perform any process level cleanup
|
sl@0
|
376 |
actions, including, but not limited to, calling atexit routines that may exist.
|
sl@0
|
377 |
|
sl@0
|
378 |
An implicit call to pthread_exit is made when a thread other than the thread in which main was first invoked returns from the start routine that was used to create
|
sl@0
|
379 |
it.
|
sl@0
|
380 |
The function's return value serves as the thread's exit status.
|
sl@0
|
381 |
|
sl@0
|
382 |
The behavior of pthread_exit is undefined if called from a cancellation handler or destructor function
|
sl@0
|
383 |
that was invoked as the result of an implicit or explicit call to pthread_exit .
|
sl@0
|
384 |
|
sl@0
|
385 |
After a thread has terminated, the result of access to local (auto)
|
sl@0
|
386 |
variables of the thread is undefined.
|
sl@0
|
387 |
Thus, references to local variables
|
sl@0
|
388 |
of the exiting thread should not be used for the pthread_exit retValPtr parameter value.
|
sl@0
|
389 |
|
sl@0
|
390 |
The process will exit with an exit status of 0 after the last thread has
|
sl@0
|
391 |
been terminated.
|
sl@0
|
392 |
The behavior is as if the implementation called exit with a zero argument at thread termination time.
|
sl@0
|
393 |
|
sl@0
|
394 |
Examples:
|
sl@0
|
395 |
@code
|
sl@0
|
396 |
/* Thread's function. */
|
sl@0
|
397 |
void *a_thread_func_1_1(void*)
|
sl@0
|
398 |
{
|
sl@0
|
399 |
/* .. Do some thing .. */
|
sl@0
|
400 |
pthread_exit((void*)RETURN_CODE);
|
sl@0
|
401 |
}
|
sl@0
|
402 |
|
sl@0
|
403 |
@endcode
|
sl@0
|
404 |
@see _exit()
|
sl@0
|
405 |
@see exit()
|
sl@0
|
406 |
@see pthread_create()
|
sl@0
|
407 |
@see pthread_join()
|
sl@0
|
408 |
|
sl@0
|
409 |
|
sl@0
|
410 |
|
sl@0
|
411 |
|
sl@0
|
412 |
@publishedAll
|
sl@0
|
413 |
@externallyDefinedApi
|
sl@0
|
414 |
*/
|
sl@0
|
415 |
|
sl@0
|
416 |
/** @fn pthread_attr_init(pthread_attr_t *attrib)
|
sl@0
|
417 |
@param attrib
|
sl@0
|
418 |
|
sl@0
|
419 |
Note: This description also covers the following functions -
|
sl@0
|
420 |
pthread_attr_destroy() pthread_attr_setstacksize() pthread_attr_getstacksize() pthread_attr_setdetachstate() pthread_attr_getdetachstate() pthread_attr_setschedparam() pthread_attr_getschedparam() pthread_attr_setschedpolicy() pthread_attr_getschedpolicy() pthread_attr_setscope() pthread_attr_getscope()
|
sl@0
|
421 |
|
sl@0
|
422 |
@return If successful, these functions return 0.
|
sl@0
|
423 |
Otherwise, an error number is returned to indicate the error.
|
sl@0
|
424 |
|
sl@0
|
425 |
Thread attributes are used to specify parameters to pthread_create .
|
sl@0
|
426 |
One attribute object can be used in multiple calls to pthread_create ,
|
sl@0
|
427 |
with or without modifications between calls.
|
sl@0
|
428 |
|
sl@0
|
429 |
The pthread_attr_init function initializes attrib with all the default thread attributes.
|
sl@0
|
430 |
|
sl@0
|
431 |
The pthread_attr_destroy function destroys attrib .
|
sl@0
|
432 |
|
sl@0
|
433 |
The pthread_attr_set* functions set the attribute that corresponds to each function name.
|
sl@0
|
434 |
|
sl@0
|
435 |
The pthread_attr_get* functions copy the value of the attribute that corresponds to each function name
|
sl@0
|
436 |
to the location pointed to by the second function parameter.
|
sl@0
|
437 |
|
sl@0
|
438 |
Examples:
|
sl@0
|
439 |
@code
|
sl@0
|
440 |
/* ***************************************************** */
|
sl@0
|
441 |
/* Example for pthread_attr_init & pthread_attr_destroy */
|
sl@0
|
442 |
/* ***************************************************** */
|
sl@0
|
443 |
pthread_attr_t new_attr;
|
sl@0
|
444 |
/* Initialize attribute */
|
sl@0
|
445 |
if(pthread_attr_init(&new;_attr) != 0)
|
sl@0
|
446 |
{
|
sl@0
|
447 |
printf("Cannot initialize attribute object
|
sl@0
|
448 |
");
|
sl@0
|
449 |
return -1; /* or exit */
|
sl@0
|
450 |
}
|
sl@0
|
451 |
/* Create thread */
|
sl@0
|
452 |
/* Destroy attribute */
|
sl@0
|
453 |
if(pthread_attr_destroy(&new;_attr) != 0)
|
sl@0
|
454 |
{
|
sl@0
|
455 |
perror("Cannot destroy the attribute object
|
sl@0
|
456 |
");
|
sl@0
|
457 |
return -1; /* or exit*/
|
sl@0
|
458 |
}
|
sl@0
|
459 |
|
sl@0
|
460 |
@endcode
|
sl@0
|
461 |
@code
|
sl@0
|
462 |
/* ***************************************************** */
|
sl@0
|
463 |
/* Example for pthread_attr_getdetachstate */
|
sl@0
|
464 |
/* ***************************************************** */
|
sl@0
|
465 |
pthread_attr_t new_attr;
|
sl@0
|
466 |
int detach_state;
|
sl@0
|
467 |
/* Initialize attribute */
|
sl@0
|
468 |
if(pthread_attr_init(&new;_attr) != 0)
|
sl@0
|
469 |
{
|
sl@0
|
470 |
printf("Cannot initialize attribute object
|
sl@0
|
471 |
");
|
sl@0
|
472 |
return -1;
|
sl@0
|
473 |
}
|
sl@0
|
474 |
if(pthread_attr_getdetachstate(&new;_attr, &detach;_state) != 0)
|
sl@0
|
475 |
{
|
sl@0
|
476 |
printf("Cannot get the state
|
sl@0
|
477 |
");
|
sl@0
|
478 |
return -1;
|
sl@0
|
479 |
}
|
sl@0
|
480 |
if(detach_state == PTHREAD_CREATE_JOINABLE)
|
sl@0
|
481 |
{
|
sl@0
|
482 |
printf("State is joinable
|
sl@0
|
483 |
");
|
sl@0
|
484 |
}
|
sl@0
|
485 |
else
|
sl@0
|
486 |
{
|
sl@0
|
487 |
printf("State is detached
|
sl@0
|
488 |
");
|
sl@0
|
489 |
}
|
sl@0
|
490 |
/* Create thread */
|
sl@0
|
491 |
/* Destroy attribute */
|
sl@0
|
492 |
|
sl@0
|
493 |
@endcode
|
sl@0
|
494 |
@code
|
sl@0
|
495 |
/* ***************************************************** */
|
sl@0
|
496 |
/* Example for pthread_attr_getschedpolicy */
|
sl@0
|
497 |
/* ***************************************************** */
|
sl@0
|
498 |
pthread_attr_t new_attr;
|
sl@0
|
499 |
int rc;
|
sl@0
|
500 |
int policy;
|
sl@0
|
501 |
/* Initialize attribute */
|
sl@0
|
502 |
if(pthread_attr_init(&new;_attr) != 0)
|
sl@0
|
503 |
{
|
sl@0
|
504 |
printf("Cannot initialize attribute object
|
sl@0
|
505 |
");
|
sl@0
|
506 |
return -1; /* or exit */
|
sl@0
|
507 |
}
|
sl@0
|
508 |
rc = pthread_attr_getschedpolicy(new_attr, &policy;);
|
sl@0
|
509 |
if( rc != 0) {
|
sl@0
|
510 |
printf("pthread_attr_getschedpolicy failed
|
sl@0
|
511 |
");
|
sl@0
|
512 |
return -1;
|
sl@0
|
513 |
}
|
sl@0
|
514 |
switch(policy) {
|
sl@0
|
515 |
case SCHED_FIFO:
|
sl@0
|
516 |
printf("Scheduling policy: SCHED_FIFO 0);
|
sl@0
|
517 |
break;
|
sl@0
|
518 |
case SCHED_RR:
|
sl@0
|
519 |
printf("Scheduling policy: SCHED_RR
|
sl@0
|
520 |
");
|
sl@0
|
521 |
break;
|
sl@0
|
522 |
case SCHED_OTHER:
|
sl@0
|
523 |
printf("Scheduling policy: SCHED_OTHER
|
sl@0
|
524 |
");
|
sl@0
|
525 |
break;
|
sl@0
|
526 |
}
|
sl@0
|
527 |
/* Create thread */
|
sl@0
|
528 |
/* Destroy attribute */
|
sl@0
|
529 |
|
sl@0
|
530 |
@endcode
|
sl@0
|
531 |
@code
|
sl@0
|
532 |
/* ***************************************************** */
|
sl@0
|
533 |
/* Example for pthread_attr_getscope */
|
sl@0
|
534 |
/* ***************************************************** */
|
sl@0
|
535 |
pthread_attr_t new_attr;
|
sl@0
|
536 |
int rc;
|
sl@0
|
537 |
int scope;
|
sl@0
|
538 |
/* Initialize attribute */
|
sl@0
|
539 |
if(pthread_attr_init(&new;_attr) != 0)
|
sl@0
|
540 |
{
|
sl@0
|
541 |
printf("Cannot initialize attribute object
|
sl@0
|
542 |
");
|
sl@0
|
543 |
return -1; /* or exit */
|
sl@0
|
544 |
}
|
sl@0
|
545 |
rc = pthread_attr_getscope(new_attr, &scope;);
|
sl@0
|
546 |
if( rc != 0) {
|
sl@0
|
547 |
printf("pthread_attr_getscope failed");
|
sl@0
|
548 |
return -1;
|
sl@0
|
549 |
}
|
sl@0
|
550 |
switch(scope) {
|
sl@0
|
551 |
case SYSTEMSCOPE:
|
sl@0
|
552 |
printf("scope: System
|
sl@0
|
553 |
");
|
sl@0
|
554 |
break;
|
sl@0
|
555 |
case PROCESSSCOPE:
|
sl@0
|
556 |
printf("scope: Process
|
sl@0
|
557 |
");
|
sl@0
|
558 |
break;
|
sl@0
|
559 |
}
|
sl@0
|
560 |
/* Create thread */
|
sl@0
|
561 |
/* Destroy attribute */
|
sl@0
|
562 |
|
sl@0
|
563 |
@endcode
|
sl@0
|
564 |
@code
|
sl@0
|
565 |
/* ***************************************************** */
|
sl@0
|
566 |
/* Example for pthread_attr_getstacksize */
|
sl@0
|
567 |
/* ***************************************************** */
|
sl@0
|
568 |
pthread_attr_t new_attr;
|
sl@0
|
569 |
size_t ssize;
|
sl@0
|
570 |
int rc;
|
sl@0
|
571 |
/* Initialize attribute */
|
sl@0
|
572 |
if(pthread_attr_init(&new;_attr) != 0)
|
sl@0
|
573 |
{
|
sl@0
|
574 |
printf("Cannot initialize attribute object
|
sl@0
|
575 |
");
|
sl@0
|
576 |
return -1; /* or exit */
|
sl@0
|
577 |
}
|
sl@0
|
578 |
/* Get the default stack_size value */
|
sl@0
|
579 |
rc = pthread_attr_getstacksize(&new;_attr, &stack;_size);
|
sl@0
|
580 |
if( rc != 0) {
|
sl@0
|
581 |
printf("pthread_attr_getstacksize failed");
|
sl@0
|
582 |
return -1;
|
sl@0
|
583 |
}
|
sl@0
|
584 |
printf("ssize = %lu
|
sl@0
|
585 |
", stack_size);
|
sl@0
|
586 |
/* Create thread */
|
sl@0
|
587 |
/* Destroy attribute */
|
sl@0
|
588 |
|
sl@0
|
589 |
@endcode
|
sl@0
|
590 |
@code
|
sl@0
|
591 |
/* ***************************************************** */
|
sl@0
|
592 |
/* Example for pthread_attr_getschedparam */
|
sl@0
|
593 |
/* ***************************************************** */
|
sl@0
|
594 |
pthread_attr_t attr;
|
sl@0
|
595 |
int rc=0;
|
sl@0
|
596 |
struct sched_param param;
|
sl@0
|
597 |
struct sched_param param2;
|
sl@0
|
598 |
rc = pthread_attr_init(&attr;);
|
sl@0
|
599 |
if(rc != 0) {
|
sl@0
|
600 |
printf("pthread_attr_init failed
|
sl@0
|
601 |
");
|
sl@0
|
602 |
return -1;
|
sl@0
|
603 |
}
|
sl@0
|
604 |
param.sched_priority = 50;
|
sl@0
|
605 |
rc = pthread_attr_setschedparam(&attr;, & param);
|
sl@0
|
606 |
if(rc != 0) {
|
sl@0
|
607 |
printf("pthread_attr_setschedparam failed
|
sl@0
|
608 |
");
|
sl@0
|
609 |
return -1;
|
sl@0
|
610 |
}
|
sl@0
|
611 |
rc = pthread_attr_getschedparam(&attr;, & param2);
|
sl@0
|
612 |
if(rc != 0) {
|
sl@0
|
613 |
printf("pthread_attr_getschedparam failed
|
sl@0
|
614 |
");
|
sl@0
|
615 |
return -1;
|
sl@0
|
616 |
}
|
sl@0
|
617 |
/* priority will be stored in param2.sched_priority */
|
sl@0
|
618 |
/* Create thread */
|
sl@0
|
619 |
/* Destroy attribute */
|
sl@0
|
620 |
|
sl@0
|
621 |
@endcode
|
sl@0
|
622 |
@code
|
sl@0
|
623 |
/* ***************************************************** */
|
sl@0
|
624 |
/* Example for pthread_attr_setdetachstate */
|
sl@0
|
625 |
/* ***************************************************** */
|
sl@0
|
626 |
pthread_attr_t new_attr;
|
sl@0
|
627 |
int detach_state;
|
sl@0
|
628 |
/* Initialize attribute */
|
sl@0
|
629 |
if(pthread_attr_init(&new;_attr) != 0)
|
sl@0
|
630 |
{
|
sl@0
|
631 |
perror("Cannot initialize attribute object
|
sl@0
|
632 |
");
|
sl@0
|
633 |
return -1;
|
sl@0
|
634 |
}
|
sl@0
|
635 |
/* Set the attribute object to PTHREAD_CREATE_JOINABLE. */
|
sl@0
|
636 |
if(pthread_attr_setdetachstate(&new;_attr, PTHREAD_CREATE_JOINABLE) != 0)
|
sl@0
|
637 |
{
|
sl@0
|
638 |
perror("Error in pthread_attr_setdetachstate()
|
sl@0
|
639 |
");
|
sl@0
|
640 |
return -1;
|
sl@0
|
641 |
}
|
sl@0
|
642 |
/* Create thread */
|
sl@0
|
643 |
/* Destroy attribute */
|
sl@0
|
644 |
|
sl@0
|
645 |
@endcode
|
sl@0
|
646 |
@code
|
sl@0
|
647 |
/* ***************************************************** */
|
sl@0
|
648 |
/* Example for pthread_attr_setschedparam */
|
sl@0
|
649 |
/* ***************************************************** */
|
sl@0
|
650 |
pthread_attr_t attr;
|
sl@0
|
651 |
int rc=0;
|
sl@0
|
652 |
struct sched_param param;
|
sl@0
|
653 |
rc = pthread_attr_init(&attr;);
|
sl@0
|
654 |
if(rc != 0) {
|
sl@0
|
655 |
printf("pthread_attr_init failed
|
sl@0
|
656 |
");
|
sl@0
|
657 |
return -1;
|
sl@0
|
658 |
}
|
sl@0
|
659 |
param.sched_priority = 50;
|
sl@0
|
660 |
rc = pthread_attr_setschedparam(&attr;, & param);
|
sl@0
|
661 |
if(rc != 0) {
|
sl@0
|
662 |
printf("pthread_attr_setschedparam failed
|
sl@0
|
663 |
");
|
sl@0
|
664 |
return -1;
|
sl@0
|
665 |
}
|
sl@0
|
666 |
/* Create thread */
|
sl@0
|
667 |
/* Destroy attribute */
|
sl@0
|
668 |
|
sl@0
|
669 |
@endcode
|
sl@0
|
670 |
@code
|
sl@0
|
671 |
/* ***************************************************** */
|
sl@0
|
672 |
/* Example for pthread_attr_setschedpolicy */
|
sl@0
|
673 |
/* ***************************************************** */
|
sl@0
|
674 |
pthread_attr_t attr;
|
sl@0
|
675 |
int rc;
|
sl@0
|
676 |
int policy = SCHED_RR;
|
sl@0
|
677 |
if(pthread_attr_init(&attr;) != 0) {
|
sl@0
|
678 |
printf("Error on pthread_attr_init()
|
sl@0
|
679 |
");
|
sl@0
|
680 |
return -1;
|
sl@0
|
681 |
}
|
sl@0
|
682 |
|
sl@0
|
683 |
if((rc=pthread_attr_setschedpolicy(&attr;,policy)) != 0) {
|
sl@0
|
684 |
printf("Error on pthread_attr_setschedpolicy() rc=%d
|
sl@0
|
685 |
", rc);
|
sl@0
|
686 |
return -1;
|
sl@0
|
687 |
}
|
sl@0
|
688 |
/* Create thread */
|
sl@0
|
689 |
/* Destroy attribute */
|
sl@0
|
690 |
|
sl@0
|
691 |
@endcode
|
sl@0
|
692 |
@code
|
sl@0
|
693 |
/* ***************************************************** */
|
sl@0
|
694 |
/* Example for pthread_attr_setstacksize */
|
sl@0
|
695 |
/* ***************************************************** */
|
sl@0
|
696 |
pthread_attr_t attr;
|
sl@0
|
697 |
int rc;
|
sl@0
|
698 |
int policy = SCHED_RR;
|
sl@0
|
699 |
if(pthread_attr_init(&attr;) != 0) {
|
sl@0
|
700 |
printf("Error on pthread_attr_init()
|
sl@0
|
701 |
");
|
sl@0
|
702 |
return -1;
|
sl@0
|
703 |
}
|
sl@0
|
704 |
|
sl@0
|
705 |
if((rc=pthread_attr_setstacksize(&attr;,8192)) != 0) {
|
sl@0
|
706 |
printf("Error on pthread_attr_setstacksize() rc=%d
|
sl@0
|
707 |
", rc);
|
sl@0
|
708 |
return -1;
|
sl@0
|
709 |
}
|
sl@0
|
710 |
/* Create thread */
|
sl@0
|
711 |
/* Destroy attribute */
|
sl@0
|
712 |
|
sl@0
|
713 |
@endcode
|
sl@0
|
714 |
@code
|
sl@0
|
715 |
/* ***************************************************** */
|
sl@0
|
716 |
/* Example for pthread_attr_setscope */
|
sl@0
|
717 |
/* ***************************************************** */
|
sl@0
|
718 |
pthread_attr_t attr;
|
sl@0
|
719 |
int rc;
|
sl@0
|
720 |
/* Initialize attr */
|
sl@0
|
721 |
rc = pthread_attr_init(&attr;);
|
sl@0
|
722 |
if( rc != 0) {
|
sl@0
|
723 |
perror("pthread_attr_init failed");
|
sl@0
|
724 |
return -1;
|
sl@0
|
725 |
}
|
sl@0
|
726 |
rc = pthread_attr_setscope(&attr;, PTHREAD_SCOPE_SYSTEM);
|
sl@0
|
727 |
if (rc != 0 ) {
|
sl@0
|
728 |
perror("PTHREAD_SCOPE_SYSTEM is not supported");
|
sl@0
|
729 |
return -1;
|
sl@0
|
730 |
}
|
sl@0
|
731 |
/* Create thread */
|
sl@0
|
732 |
/* Destroy attribute */
|
sl@0
|
733 |
|
sl@0
|
734 |
@endcode
|
sl@0
|
735 |
@see pthread_create()
|
sl@0
|
736 |
|
sl@0
|
737 |
|
sl@0
|
738 |
|
sl@0
|
739 |
|
sl@0
|
740 |
@publishedAll
|
sl@0
|
741 |
@externallyDefinedApi
|
sl@0
|
742 |
*/
|
sl@0
|
743 |
|
sl@0
|
744 |
/** @fn pthread_attr_destroy(pthread_attr_t *attrib)
|
sl@0
|
745 |
@param attrib
|
sl@0
|
746 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
747 |
@see pthread_create()
|
sl@0
|
748 |
|
sl@0
|
749 |
|
sl@0
|
750 |
|
sl@0
|
751 |
|
sl@0
|
752 |
@publishedAll
|
sl@0
|
753 |
@externallyDefinedApi
|
sl@0
|
754 |
*/
|
sl@0
|
755 |
|
sl@0
|
756 |
/** @fn pthread_attr_getdetachstate(const pthread_attr_t *attrib, int *detState)
|
sl@0
|
757 |
@param attrib
|
sl@0
|
758 |
@param detState
|
sl@0
|
759 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
760 |
@see pthread_create()
|
sl@0
|
761 |
|
sl@0
|
762 |
|
sl@0
|
763 |
|
sl@0
|
764 |
|
sl@0
|
765 |
@publishedAll
|
sl@0
|
766 |
@externallyDefinedApi
|
sl@0
|
767 |
*/
|
sl@0
|
768 |
|
sl@0
|
769 |
/** @fn pthread_attr_setdetachstate(pthread_attr_t *attrib, int detState)
|
sl@0
|
770 |
@param attrib
|
sl@0
|
771 |
@param detState
|
sl@0
|
772 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
773 |
@see pthread_create()
|
sl@0
|
774 |
|
sl@0
|
775 |
|
sl@0
|
776 |
|
sl@0
|
777 |
|
sl@0
|
778 |
@publishedAll
|
sl@0
|
779 |
@externallyDefinedApi
|
sl@0
|
780 |
*/
|
sl@0
|
781 |
|
sl@0
|
782 |
/** @fn pthread_attr_getstacksize(const pthread_attr_t *attrib, size_t *stkSize)
|
sl@0
|
783 |
@param attrib
|
sl@0
|
784 |
@param stkSize
|
sl@0
|
785 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
786 |
@see pthread_create()
|
sl@0
|
787 |
|
sl@0
|
788 |
|
sl@0
|
789 |
|
sl@0
|
790 |
|
sl@0
|
791 |
@publishedAll
|
sl@0
|
792 |
@externallyDefinedApi
|
sl@0
|
793 |
*/
|
sl@0
|
794 |
|
sl@0
|
795 |
/** @fn pthread_attr_setstacksize(pthread_attr_t *attrib, size_t stkSize)
|
sl@0
|
796 |
@param attrib
|
sl@0
|
797 |
@param stkSize
|
sl@0
|
798 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
799 |
@see pthread_create()
|
sl@0
|
800 |
|
sl@0
|
801 |
|
sl@0
|
802 |
|
sl@0
|
803 |
|
sl@0
|
804 |
@publishedAll
|
sl@0
|
805 |
@externallyDefinedApi
|
sl@0
|
806 |
*/
|
sl@0
|
807 |
|
sl@0
|
808 |
/** @fn pthread_once(pthread_once_t *once_control, thread_init_routine init_routine)
|
sl@0
|
809 |
@param once_control
|
sl@0
|
810 |
@param init_routine
|
sl@0
|
811 |
@return If successful, the pthread_once function will return zero.
|
sl@0
|
812 |
Otherwise an error number will be returned to
|
sl@0
|
813 |
indicate the error.
|
sl@0
|
814 |
|
sl@0
|
815 |
The first call to pthread_once by any thread in a process, with a given once_control ,
|
sl@0
|
816 |
will call the init_routine with no arguments.
|
sl@0
|
817 |
Subsequent calls to pthread_once with the same once_control will not call the init_routine .
|
sl@0
|
818 |
On return from pthread_once ,
|
sl@0
|
819 |
it is guaranteed that init_routine has completed.
|
sl@0
|
820 |
The once_control parameter is used to determine whether the associated initialization
|
sl@0
|
821 |
routine has been called.
|
sl@0
|
822 |
|
sl@0
|
823 |
The constant PTHREAD_ONCE_INIT is defined by header \#include \<pthread.h\> .
|
sl@0
|
824 |
|
sl@0
|
825 |
The behavior of pthread_once is undefined if once_control has automatic storage duration or is not initialized by PTHREAD_ONCE_INIT .
|
sl@0
|
826 |
|
sl@0
|
827 |
Examples:
|
sl@0
|
828 |
@code
|
sl@0
|
829 |
/* The init function that pthread_once calls */
|
sl@0
|
830 |
void an_init_func_1_1()
|
sl@0
|
831 |
{
|
sl@0
|
832 |
printf (" Initialized ..
|
sl@0
|
833 |
");
|
sl@0
|
834 |
}
|
sl@0
|
835 |
int XYZ()
|
sl@0
|
836 |
{
|
sl@0
|
837 |
pthread_once_t once_control = PTHREAD_ONCE_INIT;
|
sl@0
|
838 |
/* Call pthread_once, passing it the once_control */
|
sl@0
|
839 |
pthread_once(&once;_control, an_init_func_1_1);
|
sl@0
|
840 |
/* Call pthread_once again. The init function should not be
|
sl@0
|
841 |
* called. */
|
sl@0
|
842 |
pthread_once(&once;_control, an_init_func_1_1);
|
sl@0
|
843 |
}
|
sl@0
|
844 |
|
sl@0
|
845 |
@endcode
|
sl@0
|
846 |
|
sl@0
|
847 |
|
sl@0
|
848 |
@publishedAll
|
sl@0
|
849 |
@externallyDefinedApi
|
sl@0
|
850 |
*/
|
sl@0
|
851 |
|
sl@0
|
852 |
/** @fn pthread_mutexattr_init(pthread_mutexattr_t *attr)
|
sl@0
|
853 |
@param attr
|
sl@0
|
854 |
Note: This description also covers the following functions -
|
sl@0
|
855 |
pthread_mutexattr_destroy() pthread_mutexattr_settype() pthread_mutexattr_gettype() pthread_mutexattr_getpshared() pthread_mutexattr_setpshared()
|
sl@0
|
856 |
|
sl@0
|
857 |
@return If successful, these functions return 0.
|
sl@0
|
858 |
Otherwise, an error number is returned to indicate the error.
|
sl@0
|
859 |
|
sl@0
|
860 |
Mutex attributes are used to specify parameters to pthread_mutex_init .
|
sl@0
|
861 |
One attribute object can be used in multiple calls to pthread_mutex_init ,
|
sl@0
|
862 |
with or without modifications between calls.
|
sl@0
|
863 |
|
sl@0
|
864 |
The pthread_mutexattr_init function initializes attr with all the default mutex attributes.
|
sl@0
|
865 |
|
sl@0
|
866 |
The pthread_mutexattr_destroy function destroys attr .
|
sl@0
|
867 |
|
sl@0
|
868 |
The pthread_mutexattr_set* functions set the attribute that corresponds to each function name.
|
sl@0
|
869 |
|
sl@0
|
870 |
The pthread_mutexattr_get* functions copy the value of the attribute that corresponds to each function name
|
sl@0
|
871 |
to the location pointed to by the second function parameter.
|
sl@0
|
872 |
|
sl@0
|
873 |
Examples:
|
sl@0
|
874 |
@code
|
sl@0
|
875 |
pthread_mutexattr_t mta;
|
sl@0
|
876 |
int rc;
|
sl@0
|
877 |
/* Initialize a mutex attributes object */
|
sl@0
|
878 |
if((rc=pthread_mutexattr_init(&mta;)) != 0)
|
sl@0
|
879 |
{
|
sl@0
|
880 |
fprintf(stderr,"Cannot initialize mutex attributes object
|
sl@0
|
881 |
");
|
sl@0
|
882 |
return -1;
|
sl@0
|
883 |
}
|
sl@0
|
884 |
/* Destroy the mutex attributes object */
|
sl@0
|
885 |
if(pthread_mutexattr_destroy(&mta;) != 0)
|
sl@0
|
886 |
{
|
sl@0
|
887 |
fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d
|
sl@0
|
888 |
", rc);
|
sl@0
|
889 |
return -1;
|
sl@0
|
890 |
}
|
sl@0
|
891 |
|
sl@0
|
892 |
@endcode
|
sl@0
|
893 |
@code
|
sl@0
|
894 |
pthread_mutexattr_t mta;
|
sl@0
|
895 |
int pshared;
|
sl@0
|
896 |
/* Initialize a mutex attributes object */
|
sl@0
|
897 |
if(pthread_mutexattr_init(&mta;) != 0)
|
sl@0
|
898 |
{
|
sl@0
|
899 |
perror("Error at pthread_mutexattr_init()
|
sl@0
|
900 |
");
|
sl@0
|
901 |
return -1;
|
sl@0
|
902 |
}
|
sl@0
|
903 |
/* The default 'pshared' attribute is PTHREAD_PROCESS_PRIVATE */
|
sl@0
|
904 |
if(pthread_mutexattr_getpshared(&mta;, &pshared;) != 0)
|
sl@0
|
905 |
{
|
sl@0
|
906 |
fprintf(stderr,"Error obtaining the attribute process-shared
|
sl@0
|
907 |
");
|
sl@0
|
908 |
return -1;
|
sl@0
|
909 |
}
|
sl@0
|
910 |
if (pshared != PTHREAD_PROCESS_PRIVATE)
|
sl@0
|
911 |
printf (" pshared is NOT PTHREAD_PROCESS_PRIVATE
|
sl@0
|
912 |
");
|
sl@0
|
913 |
|
sl@0
|
914 |
@endcode
|
sl@0
|
915 |
@code
|
sl@0
|
916 |
pthread_mutexattr_t mta;
|
sl@0
|
917 |
int type;
|
sl@0
|
918 |
/* Initialize a mutex attributes object */
|
sl@0
|
919 |
if(pthread_mutexattr_init(&mta;) != 0)
|
sl@0
|
920 |
{
|
sl@0
|
921 |
perror("Error at pthread_mutexattr_init()
|
sl@0
|
922 |
");
|
sl@0
|
923 |
return -1;
|
sl@0
|
924 |
}
|
sl@0
|
925 |
/* The default 'type' attribute is PTHREAD_MUTEX_DEFAULT */
|
sl@0
|
926 |
if(pthread_mutexattr_gettype(&mta;, &type;) != 0)
|
sl@0
|
927 |
{
|
sl@0
|
928 |
fprintf(stderr,"pthread_mutexattr_gettype(): Error obtaining the attribute 'type'
|
sl@0
|
929 |
");
|
sl@0
|
930 |
return -1;
|
sl@0
|
931 |
}
|
sl@0
|
932 |
if(type != PTHREAD_MUTEX_DEFAULT)
|
sl@0
|
933 |
{
|
sl@0
|
934 |
printf("FAILED: Incorrect default mutexattr 'type' value: %d
|
sl@0
|
935 |
", type);
|
sl@0
|
936 |
return -1;
|
sl@0
|
937 |
}
|
sl@0
|
938 |
|
sl@0
|
939 |
@endcode
|
sl@0
|
940 |
@code
|
sl@0
|
941 |
pthread_mutexattr_t mta;
|
sl@0
|
942 |
int rc;
|
sl@0
|
943 |
/* Initialize a mutex attributes object */
|
sl@0
|
944 |
if((rc=pthread_mutexattr_init(&mta;)) != 0)
|
sl@0
|
945 |
{
|
sl@0
|
946 |
fprintf(stderr,"Cannot initialize mutex attributes object
|
sl@0
|
947 |
");
|
sl@0
|
948 |
return -1;
|
sl@0
|
949 |
}
|
sl@0
|
950 |
/* Destroy the mutex attributes object */
|
sl@0
|
951 |
if(pthread_mutexattr_destroy(&mta;) != 0)
|
sl@0
|
952 |
{
|
sl@0
|
953 |
fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d
|
sl@0
|
954 |
", rc);
|
sl@0
|
955 |
return -1;
|
sl@0
|
956 |
}
|
sl@0
|
957 |
|
sl@0
|
958 |
@endcode
|
sl@0
|
959 |
@code
|
sl@0
|
960 |
pthread_mutexattr_t mta;
|
sl@0
|
961 |
int ret;
|
sl@0
|
962 |
/* Initialize a mutex attributes object */
|
sl@0
|
963 |
if(pthread_mutexattr_init(&mta;) != 0)
|
sl@0
|
964 |
{
|
sl@0
|
965 |
perror("Error at pthread_mutexattr_init()
|
sl@0
|
966 |
");
|
sl@0
|
967 |
return -1;
|
sl@0
|
968 |
}
|
sl@0
|
969 |
/* Set the 'pshared' attribute to PTHREAD_PROCESS_PRIVATE */
|
sl@0
|
970 |
if((ret=pthread_mutexattr_setpshared(&mta;, PTHREAD_PROCESS_PRIVATE)) != 0)
|
sl@0
|
971 |
{
|
sl@0
|
972 |
printf("FAILED: Cannot set pshared attribute to PTHREAD_PROCESS_PRIVATE. Error: %d
|
sl@0
|
973 |
", ret);
|
sl@0
|
974 |
return -1;
|
sl@0
|
975 |
}
|
sl@0
|
976 |
|
sl@0
|
977 |
@endcode
|
sl@0
|
978 |
@code
|
sl@0
|
979 |
pthread_mutexattr_t mta;
|
sl@0
|
980 |
int type;
|
sl@0
|
981 |
/* Initialize a mutex attributes object */
|
sl@0
|
982 |
if(pthread_mutexattr_init(&mta;) != 0)
|
sl@0
|
983 |
{
|
sl@0
|
984 |
perror("Error at pthread_mutexattr_init()
|
sl@0
|
985 |
");
|
sl@0
|
986 |
return -1;
|
sl@0
|
987 |
}
|
sl@0
|
988 |
if(pthread_mutexattr_gettype(&mta;, &type;) != 0)
|
sl@0
|
989 |
{
|
sl@0
|
990 |
printf("Error getting the attribute 'type'
|
sl@0
|
991 |
");
|
sl@0
|
992 |
return -1;
|
sl@0
|
993 |
}
|
sl@0
|
994 |
if(type != PTHREAD_MUTEX_DEFAULT)
|
sl@0
|
995 |
{
|
sl@0
|
996 |
printf("FAILED: Default value of the 'type' attribute is not PTHREAD_MUTEX_DEFAULT
|
sl@0
|
997 |
");
|
sl@0
|
998 |
return -1;
|
sl@0
|
999 |
}
|
sl@0
|
1000 |
if(pthread_mutexattr_settype(&mta;, PTHREAD_MUTEX_NORMAL) != 0)
|
sl@0
|
1001 |
{
|
sl@0
|
1002 |
printf("FAILED: Error setting the attribute 'type'
|
sl@0
|
1003 |
");
|
sl@0
|
1004 |
return -1;
|
sl@0
|
1005 |
}
|
sl@0
|
1006 |
|
sl@0
|
1007 |
@endcode
|
sl@0
|
1008 |
@see pthread_mutex_init()
|
sl@0
|
1009 |
|
sl@0
|
1010 |
|
sl@0
|
1011 |
|
sl@0
|
1012 |
|
sl@0
|
1013 |
@publishedAll
|
sl@0
|
1014 |
@externallyDefinedApi
|
sl@0
|
1015 |
*/
|
sl@0
|
1016 |
|
sl@0
|
1017 |
/** @fn pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
|
sl@0
|
1018 |
@param attr
|
sl@0
|
1019 |
Refer pthread_mutexattr_init() for the documentation
|
sl@0
|
1020 |
@see pthread_mutex_init()
|
sl@0
|
1021 |
|
sl@0
|
1022 |
|
sl@0
|
1023 |
|
sl@0
|
1024 |
|
sl@0
|
1025 |
@publishedAll
|
sl@0
|
1026 |
@externallyDefinedApi
|
sl@0
|
1027 |
*/
|
sl@0
|
1028 |
|
sl@0
|
1029 |
/** @fn pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared)
|
sl@0
|
1030 |
@param attr
|
sl@0
|
1031 |
@param pshared
|
sl@0
|
1032 |
Refer pthread_mutexattr_init() for the documentation
|
sl@0
|
1033 |
@see pthread_mutex_init()
|
sl@0
|
1034 |
|
sl@0
|
1035 |
|
sl@0
|
1036 |
|
sl@0
|
1037 |
|
sl@0
|
1038 |
@publishedAll
|
sl@0
|
1039 |
@externallyDefinedApi
|
sl@0
|
1040 |
*/
|
sl@0
|
1041 |
|
sl@0
|
1042 |
/** @fn pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type)
|
sl@0
|
1043 |
@param attr
|
sl@0
|
1044 |
@param type
|
sl@0
|
1045 |
Refer pthread_mutexattr_init() for the documentation
|
sl@0
|
1046 |
@see pthread_mutex_init()
|
sl@0
|
1047 |
|
sl@0
|
1048 |
|
sl@0
|
1049 |
|
sl@0
|
1050 |
|
sl@0
|
1051 |
@publishedAll
|
sl@0
|
1052 |
@externallyDefinedApi
|
sl@0
|
1053 |
*/
|
sl@0
|
1054 |
|
sl@0
|
1055 |
/** @fn pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
|
sl@0
|
1056 |
@param attr
|
sl@0
|
1057 |
@param type # use integer valuses between 1 and 10
|
sl@0
|
1058 |
Refer pthread_mutexattr_init() for the documentation
|
sl@0
|
1059 |
@see pthread_mutex_init()
|
sl@0
|
1060 |
|
sl@0
|
1061 |
|
sl@0
|
1062 |
|
sl@0
|
1063 |
|
sl@0
|
1064 |
@publishedAll
|
sl@0
|
1065 |
@externallyDefinedApi
|
sl@0
|
1066 |
*/
|
sl@0
|
1067 |
|
sl@0
|
1068 |
/** @fn pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
|
sl@0
|
1069 |
@param mutex
|
sl@0
|
1070 |
@param attr
|
sl@0
|
1071 |
@return If successful, pthread_mutex_init will return zero and put the new mutex id into mutex ,
|
sl@0
|
1072 |
otherwise an error number will be returned to indicate the error.
|
sl@0
|
1073 |
|
sl@0
|
1074 |
The pthread_mutex_init function creates a new mutex, with attributes specified with attr .
|
sl@0
|
1075 |
If attr is NULL the default attributes are used.
|
sl@0
|
1076 |
|
sl@0
|
1077 |
Examples:
|
sl@0
|
1078 |
@code
|
sl@0
|
1079 |
pthread_mutex_t mutex;
|
sl@0
|
1080 |
int XYZ()
|
sl@0
|
1081 |
{
|
sl@0
|
1082 |
int rc;
|
sl@0
|
1083 |
/* Initialize a mutex object with the default mutex attributes */
|
sl@0
|
1084 |
if((rc=pthread_mutex_init(&mutex;,NULL)) != 0)
|
sl@0
|
1085 |
{
|
sl@0
|
1086 |
fprintf(stderr,"Error at pthread_mutex_init(), rc=%d0,rc);
|
sl@0
|
1087 |
return -1;
|
sl@0
|
1088 |
}
|
sl@0
|
1089 |
}
|
sl@0
|
1090 |
|
sl@0
|
1091 |
@endcode
|
sl@0
|
1092 |
@see pthread_mutex_destroy()
|
sl@0
|
1093 |
@see pthread_mutex_lock()
|
sl@0
|
1094 |
@see pthread_mutex_trylock()
|
sl@0
|
1095 |
@see pthread_mutex_unlock()
|
sl@0
|
1096 |
|
sl@0
|
1097 |
|
sl@0
|
1098 |
|
sl@0
|
1099 |
|
sl@0
|
1100 |
@publishedAll
|
sl@0
|
1101 |
@externallyDefinedApi
|
sl@0
|
1102 |
*/
|
sl@0
|
1103 |
|
sl@0
|
1104 |
/** @fn pthread_mutex_destroy(pthread_mutex_t *mutex)
|
sl@0
|
1105 |
@param mutex
|
sl@0
|
1106 |
@return If successful, pthread_mutex_destroy will return zero, otherwise an error number will be returned to
|
sl@0
|
1107 |
indicate the error.
|
sl@0
|
1108 |
|
sl@0
|
1109 |
The pthread_mutex_destroy function frees the resources allocated for mutex .
|
sl@0
|
1110 |
|
sl@0
|
1111 |
Examples:
|
sl@0
|
1112 |
@code
|
sl@0
|
1113 |
pthread_mutexattr_t mta;
|
sl@0
|
1114 |
int rc;
|
sl@0
|
1115 |
/* Initialize a mutex attributes object */
|
sl@0
|
1116 |
if((rc=pthread_mutexattr_init(&mta;)) != 0) {
|
sl@0
|
1117 |
fprintf(stderr,"Error at pthread_mutexattr_init(), rc=%d
|
sl@0
|
1118 |
",rc);
|
sl@0
|
1119 |
return -1;
|
sl@0
|
1120 |
}
|
sl@0
|
1121 |
/* Destroy the mutex attributes object */
|
sl@0
|
1122 |
if((rc=pthread_mutexattr_destroy(&mta;)) != 0) {
|
sl@0
|
1123 |
fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d
|
sl@0
|
1124 |
",rc);
|
sl@0
|
1125 |
return -1;
|
sl@0
|
1126 |
}
|
sl@0
|
1127 |
|
sl@0
|
1128 |
@endcode
|
sl@0
|
1129 |
@see pthread_mutex_init()
|
sl@0
|
1130 |
@see pthread_mutex_lock()
|
sl@0
|
1131 |
@see pthread_mutex_trylock()
|
sl@0
|
1132 |
@see pthread_mutex_unlock()
|
sl@0
|
1133 |
|
sl@0
|
1134 |
|
sl@0
|
1135 |
|
sl@0
|
1136 |
|
sl@0
|
1137 |
@publishedAll
|
sl@0
|
1138 |
@externallyDefinedApi
|
sl@0
|
1139 |
*/
|
sl@0
|
1140 |
|
sl@0
|
1141 |
/** @fn pthread_mutex_lock(pthread_mutex_t *mutex)
|
sl@0
|
1142 |
@param mutex
|
sl@0
|
1143 |
@return If successful, pthread_mutex_lock will return zero, otherwise an error number will be returned to
|
sl@0
|
1144 |
indicate the error.
|
sl@0
|
1145 |
|
sl@0
|
1146 |
The pthread_mutex_lock function locks mutex .
|
sl@0
|
1147 |
If the mutex is already locked, the calling thread will block until the
|
sl@0
|
1148 |
mutex becomes available.
|
sl@0
|
1149 |
|
sl@0
|
1150 |
Examples:
|
sl@0
|
1151 |
@code
|
sl@0
|
1152 |
pthread_mutex_t mutex;
|
sl@0
|
1153 |
int XYZ()
|
sl@0
|
1154 |
{
|
sl@0
|
1155 |
int rc;
|
sl@0
|
1156 |
/* Initialize a mutex object with the default mutex attributes */
|
sl@0
|
1157 |
if((rc=pthread_mutex_init(&mutex;,NULL)) != 0) {
|
sl@0
|
1158 |
fprintf(stderr,"Error at pthread_mutex_init(), rc=%d
|
sl@0
|
1159 |
",rc);
|
sl@0
|
1160 |
return -1;
|
sl@0
|
1161 |
}
|
sl@0
|
1162 |
/* Lock the mutex using pthread_mutex_lock() */
|
sl@0
|
1163 |
if((rc=pthread_mutex_lock(&mutex;)) == 0) {
|
sl@0
|
1164 |
printf(" lock is successful
|
sl@0
|
1165 |
");
|
sl@0
|
1166 |
}
|
sl@0
|
1167 |
}
|
sl@0
|
1168 |
|
sl@0
|
1169 |
@endcode
|
sl@0
|
1170 |
@see pthread_mutex_destroy()
|
sl@0
|
1171 |
@see pthread_mutex_init()
|
sl@0
|
1172 |
@see pthread_mutex_trylock()
|
sl@0
|
1173 |
@see pthread_mutex_unlock()
|
sl@0
|
1174 |
|
sl@0
|
1175 |
|
sl@0
|
1176 |
|
sl@0
|
1177 |
|
sl@0
|
1178 |
@publishedAll
|
sl@0
|
1179 |
@externallyDefinedApi
|
sl@0
|
1180 |
*/
|
sl@0
|
1181 |
|
sl@0
|
1182 |
/** @fn pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
|
sl@0
|
1183 |
@param mutex
|
sl@0
|
1184 |
@param abstime
|
sl@0
|
1185 |
@return If successful, pthread_mutex_timedlock will return zero, otherwise an error number will be returned to
|
sl@0
|
1186 |
indicate the error.
|
sl@0
|
1187 |
|
sl@0
|
1188 |
The pthread_mutex_timedlock function will lock mutex .
|
sl@0
|
1189 |
If it is already locked the calling thread will block until
|
sl@0
|
1190 |
the mutex becomes available or
|
sl@0
|
1191 |
the timeout,
|
sl@0
|
1192 |
specified by abstime,
|
sl@0
|
1193 |
expires.
|
sl@0
|
1194 |
The time of the timeout is an absolute time and
|
sl@0
|
1195 |
is not relative to the current time.
|
sl@0
|
1196 |
|
sl@0
|
1197 |
Examples:
|
sl@0
|
1198 |
@code
|
sl@0
|
1199 |
#include <sys/time.h>
|
sl@0
|
1200 |
#define TIMEOUT 3
|
sl@0
|
1201 |
/* 3 seconds of timeout time for pthread_mutex_timedlock(). */
|
sl@0
|
1202 |
struct timespec timeout;
|
sl@0
|
1203 |
struct timeval currsec1;
|
sl@0
|
1204 |
int rc;
|
sl@0
|
1205 |
pthread_mutex_t mutex;
|
sl@0
|
1206 |
|
sl@0
|
1207 |
|
sl@0
|
1208 |
/* Get the current time before the mutex locked. */
|
sl@0
|
1209 |
gettimeofday(&currsec1;, NULL);
|
sl@0
|
1210 |
/* Absolute time, not relative. */
|
sl@0
|
1211 |
timeout.tv_sec = currsec1.tv_sec + TIMEOUT;
|
sl@0
|
1212 |
timeout.tv_nsec = currsec1.tv_usec * 1000;
|
sl@0
|
1213 |
printf("Timed mutex lock will block for %d seconds starting from: %ld.%06ld
|
sl@0
|
1214 |
", TIMEOUT, (long)currsec1.tv_sec, (long)currsec1.tv_usec);
|
sl@0
|
1215 |
rc = pthread_mutex_timedlock(&mutex;, &timeout;);
|
sl@0
|
1216 |
if(rc != 0) {
|
sl@0
|
1217 |
if (rc == ETIMEDOUT) {
|
sl@0
|
1218 |
fprintf(stderr,"Thread stops waiting when time is out
|
sl@0
|
1219 |
");
|
sl@0
|
1220 |
}
|
sl@0
|
1221 |
else {
|
sl@0
|
1222 |
fprintf(stderr,"pthread_mutex_timedwait return %d
|
sl@0
|
1223 |
", rc);
|
sl@0
|
1224 |
}
|
sl@0
|
1225 |
}
|
sl@0
|
1226 |
fprintf(stderr,"Thread wakened up
|
sl@0
|
1227 |
");
|
sl@0
|
1228 |
|
sl@0
|
1229 |
@endcode
|
sl@0
|
1230 |
@see pthread_mutex_destroy()
|
sl@0
|
1231 |
@see pthread_mutex_init()
|
sl@0
|
1232 |
@see pthread_mutex_lock()
|
sl@0
|
1233 |
@see pthread_mutex_trylock()
|
sl@0
|
1234 |
@see pthread_mutex_unlock()
|
sl@0
|
1235 |
|
sl@0
|
1236 |
|
sl@0
|
1237 |
|
sl@0
|
1238 |
|
sl@0
|
1239 |
@publishedAll
|
sl@0
|
1240 |
@externallyDefinedApi
|
sl@0
|
1241 |
*/
|
sl@0
|
1242 |
|
sl@0
|
1243 |
/** @fn pthread_mutex_trylock(pthread_mutex_t *mutex)
|
sl@0
|
1244 |
@param mutex
|
sl@0
|
1245 |
@return If successful, pthread_mutex_trylock will return zero, otherwise an error number will be returned to
|
sl@0
|
1246 |
indicate the error.
|
sl@0
|
1247 |
|
sl@0
|
1248 |
The pthread_mutex_trylock function locks mutex .
|
sl@0
|
1249 |
If the mutex is already locked, pthread_mutex_trylock will not block waiting for the mutex, but will return an error condition.
|
sl@0
|
1250 |
|
sl@0
|
1251 |
Examples:
|
sl@0
|
1252 |
@code
|
sl@0
|
1253 |
int rc;
|
sl@0
|
1254 |
pthread_mutex_t mutex;
|
sl@0
|
1255 |
rc = pthread_mutex_trylock(&mutex;);
|
sl@0
|
1256 |
switch (rc)
|
sl@0
|
1257 |
{
|
sl@0
|
1258 |
case 0:
|
sl@0
|
1259 |
printf ("Locked successfully
|
sl@0
|
1260 |
");
|
sl@0
|
1261 |
break;
|
sl@0
|
1262 |
case EAGAIN:
|
sl@0
|
1263 |
printf ("could not lock, try later.....
|
sl@0
|
1264 |
");
|
sl@0
|
1265 |
break;
|
sl@0
|
1266 |
}
|
sl@0
|
1267 |
|
sl@0
|
1268 |
@endcode
|
sl@0
|
1269 |
@see pthread_mutex_destroy()
|
sl@0
|
1270 |
@see pthread_mutex_init()
|
sl@0
|
1271 |
@see pthread_mutex_lock()
|
sl@0
|
1272 |
@see pthread_mutex_unlock()
|
sl@0
|
1273 |
|
sl@0
|
1274 |
|
sl@0
|
1275 |
|
sl@0
|
1276 |
|
sl@0
|
1277 |
@publishedAll
|
sl@0
|
1278 |
@externallyDefinedApi
|
sl@0
|
1279 |
*/
|
sl@0
|
1280 |
|
sl@0
|
1281 |
/** @fn pthread_mutex_unlock(pthread_mutex_t *mutex)
|
sl@0
|
1282 |
@param mutex
|
sl@0
|
1283 |
@return If successful, pthread_mutex_unlock will return zero, otherwise an error number will be returned to
|
sl@0
|
1284 |
indicate the error.
|
sl@0
|
1285 |
|
sl@0
|
1286 |
If the current thread holds the lock on mutex ,
|
sl@0
|
1287 |
then the pthread_mutex_unlock function unlocks mutex .
|
sl@0
|
1288 |
|
sl@0
|
1289 |
Examples:
|
sl@0
|
1290 |
@code
|
sl@0
|
1291 |
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
sl@0
|
1292 |
int XYZ()
|
sl@0
|
1293 |
{
|
sl@0
|
1294 |
int rc;
|
sl@0
|
1295 |
/* Get the mutex using pthread_mutex_lock() */
|
sl@0
|
1296 |
if((rc=pthread_mutex_lock(&mutex;)) != 0) {
|
sl@0
|
1297 |
fprintf(stderr,"Error at pthread_mutex_lock(), rc=%d
|
sl@0
|
1298 |
",rc);
|
sl@0
|
1299 |
return -1;
|
sl@0
|
1300 |
}
|
sl@0
|
1301 |
/* Release the mutex using pthread_mutex_unlock() */
|
sl@0
|
1302 |
if((rc=pthread_mutex_unlock(&mutex;)) != 0) {
|
sl@0
|
1303 |
printf("unlock failed
|
sl@0
|
1304 |
");
|
sl@0
|
1305 |
return -1;
|
sl@0
|
1306 |
}
|
sl@0
|
1307 |
|
sl@0
|
1308 |
@endcode
|
sl@0
|
1309 |
@see pthread_mutex_destroy()
|
sl@0
|
1310 |
@see pthread_mutex_init()
|
sl@0
|
1311 |
@see pthread_mutex_lock()
|
sl@0
|
1312 |
@see pthread_mutex_trylock()
|
sl@0
|
1313 |
|
sl@0
|
1314 |
|
sl@0
|
1315 |
|
sl@0
|
1316 |
|
sl@0
|
1317 |
@publishedAll
|
sl@0
|
1318 |
@externallyDefinedApi
|
sl@0
|
1319 |
*/
|
sl@0
|
1320 |
|
sl@0
|
1321 |
/** @fn pthread_condattr_init(pthread_condattr_t *)
|
sl@0
|
1322 |
|
sl@0
|
1323 |
Note: This description also covers the following functions -
|
sl@0
|
1324 |
pthread_condattr_destroy()
|
sl@0
|
1325 |
|
sl@0
|
1326 |
@return If successful, these functions return 0.
|
sl@0
|
1327 |
Otherwise, an error number is returned to indicate the error.
|
sl@0
|
1328 |
|
sl@0
|
1329 |
Condition attribute objects are used to specify parameters to pthread_cond_init .
|
sl@0
|
1330 |
Current implementation of conditional variables does not support any
|
sl@0
|
1331 |
attributes, so these functions are not very useful.
|
sl@0
|
1332 |
|
sl@0
|
1333 |
The pthread_condattr_init function initializes a condition attribute object with the default
|
sl@0
|
1334 |
attributes.(As of now None)
|
sl@0
|
1335 |
|
sl@0
|
1336 |
The pthread_condattr_destroy function destroys a condition attribute object.
|
sl@0
|
1337 |
|
sl@0
|
1338 |
Examples:
|
sl@0
|
1339 |
@code
|
sl@0
|
1340 |
pthread_condattr_t condattr;
|
sl@0
|
1341 |
int rc;
|
sl@0
|
1342 |
/* Initialize a condition variable attributes object */
|
sl@0
|
1343 |
if((rc=pthread_condattr_init(&condattr;)) != 0)
|
sl@0
|
1344 |
{
|
sl@0
|
1345 |
fprintf(stderr,"Cannot initialize condition variable attributes object
|
sl@0
|
1346 |
");
|
sl@0
|
1347 |
return -1;
|
sl@0
|
1348 |
}
|
sl@0
|
1349 |
/* Destroy the condition variable attributes object */
|
sl@0
|
1350 |
if(pthread_condattr_destroy(&condattr;) != 0)
|
sl@0
|
1351 |
{
|
sl@0
|
1352 |
fprintf(stderr,"Error at pthread_condattr_destroy(), rc=%d
|
sl@0
|
1353 |
", rc);
|
sl@0
|
1354 |
return -1;
|
sl@0
|
1355 |
}
|
sl@0
|
1356 |
|
sl@0
|
1357 |
@endcode
|
sl@0
|
1358 |
@see pthread_cond_init()
|
sl@0
|
1359 |
|
sl@0
|
1360 |
|
sl@0
|
1361 |
|
sl@0
|
1362 |
|
sl@0
|
1363 |
@publishedAll
|
sl@0
|
1364 |
@externallyDefinedApi
|
sl@0
|
1365 |
*/
|
sl@0
|
1366 |
|
sl@0
|
1367 |
/** @fn pthread_condattr_destroy(pthread_condattr_t *)
|
sl@0
|
1368 |
|
sl@0
|
1369 |
Refer pthread_condattr_init() for the documentation
|
sl@0
|
1370 |
@see pthread_cond_init()
|
sl@0
|
1371 |
|
sl@0
|
1372 |
|
sl@0
|
1373 |
|
sl@0
|
1374 |
|
sl@0
|
1375 |
@publishedAll
|
sl@0
|
1376 |
@externallyDefinedApi
|
sl@0
|
1377 |
*/
|
sl@0
|
1378 |
|
sl@0
|
1379 |
/** @fn pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *)
|
sl@0
|
1380 |
@param cond
|
sl@0
|
1381 |
@param # represents the parameter attr
|
sl@0
|
1382 |
@return If successful, the pthread_cond_init function will return zero and put the new condition variable id into cond ,
|
sl@0
|
1383 |
otherwise an error number will be returned to indicate the error.
|
sl@0
|
1384 |
|
sl@0
|
1385 |
The pthread_cond_init function creates a new condition variable referenced
|
sl@0
|
1386 |
by cond with attributes referenced by attr. If attr is NULL, the default condition variable attributes will be used.
|
sl@0
|
1387 |
Upon successful initialization, the state of the condition variable will be initialized.
|
sl@0
|
1388 |
|
sl@0
|
1389 |
Examples:
|
sl@0
|
1390 |
@code
|
sl@0
|
1391 |
int rc;
|
sl@0
|
1392 |
struct testdata
|
sl@0
|
1393 |
{
|
sl@0
|
1394 |
pthread_mutex_t mutex;
|
sl@0
|
1395 |
pthread_cond_t cond;
|
sl@0
|
1396 |
};
|
sl@0
|
1397 |
static struct testdata td;
|
sl@0
|
1398 |
int function1()
|
sl@0
|
1399 |
{
|
sl@0
|
1400 |
/* Initialize a condition variable object */
|
sl@0
|
1401 |
if (pthread_cond_init(&td.cond;, NULL) != 0) {
|
sl@0
|
1402 |
fprintf(stderr,"Fail to initialize cond
|
sl@0
|
1403 |
");
|
sl@0
|
1404 |
return -1;
|
sl@0
|
1405 |
}
|
sl@0
|
1406 |
/* ... Use it for wait, signal, broadcast */
|
sl@0
|
1407 |
/* Destroy the condition variable object */
|
sl@0
|
1408 |
if((rc=pthread_cond_destroy(&td.cond;)) != 0)
|
sl@0
|
1409 |
{
|
sl@0
|
1410 |
fprintf(stderr,"Error at pthread_cond_destroy(), rc=%d
|
sl@0
|
1411 |
",rc);
|
sl@0
|
1412 |
return -1;
|
sl@0
|
1413 |
}
|
sl@0
|
1414 |
}
|
sl@0
|
1415 |
|
sl@0
|
1416 |
@endcode
|
sl@0
|
1417 |
@see pthread_cond_broadcast()
|
sl@0
|
1418 |
@see pthread_cond_destroy()
|
sl@0
|
1419 |
@see pthread_cond_signal()
|
sl@0
|
1420 |
@see pthread_cond_timedwait()
|
sl@0
|
1421 |
@see pthread_cond_wait()
|
sl@0
|
1422 |
|
sl@0
|
1423 |
|
sl@0
|
1424 |
|
sl@0
|
1425 |
|
sl@0
|
1426 |
@publishedAll
|
sl@0
|
1427 |
@externallyDefinedApi
|
sl@0
|
1428 |
*/
|
sl@0
|
1429 |
|
sl@0
|
1430 |
/** @fn pthread_cond_destroy(pthread_cond_t *cond)
|
sl@0
|
1431 |
@param cond
|
sl@0
|
1432 |
@return If successful, the pthread_cond_destroy function will return zero, otherwise an error number will be returned
|
sl@0
|
1433 |
to indicate the error.
|
sl@0
|
1434 |
|
sl@0
|
1435 |
The pthread_cond_destroy function frees the resources allocated by the condition variable cond .
|
sl@0
|
1436 |
|
sl@0
|
1437 |
Examples:
|
sl@0
|
1438 |
@code
|
sl@0
|
1439 |
int rc;
|
sl@0
|
1440 |
struct testdata
|
sl@0
|
1441 |
{
|
sl@0
|
1442 |
pthread_mutex_t mutex;
|
sl@0
|
1443 |
pthread_cond_t cond;
|
sl@0
|
1444 |
};
|
sl@0
|
1445 |
static struct testdata td;
|
sl@0
|
1446 |
int function1()
|
sl@0
|
1447 |
{
|
sl@0
|
1448 |
/* Initialize a condition variable object */
|
sl@0
|
1449 |
if (pthread_cond_init(&td.cond;, NULL) != 0) {
|
sl@0
|
1450 |
fprintf(stderr,"Fail to initialize cond
|
sl@0
|
1451 |
");
|
sl@0
|
1452 |
return -1;
|
sl@0
|
1453 |
}
|
sl@0
|
1454 |
/* ... Use it for wait, signal, broadcast */
|
sl@0
|
1455 |
/* Destroy the condition variable object */
|
sl@0
|
1456 |
if((rc=pthread_cond_destroy(&td.cond;)) != 0)
|
sl@0
|
1457 |
{
|
sl@0
|
1458 |
fprintf(stderr,"Error at pthread_cond_destroy(), rc=%d
|
sl@0
|
1459 |
",rc);
|
sl@0
|
1460 |
return -1;
|
sl@0
|
1461 |
}
|
sl@0
|
1462 |
}
|
sl@0
|
1463 |
|
sl@0
|
1464 |
@endcode
|
sl@0
|
1465 |
@see pthread_cond_broadcast()
|
sl@0
|
1466 |
@see pthread_cond_init()
|
sl@0
|
1467 |
@see pthread_cond_signal()
|
sl@0
|
1468 |
@see pthread_cond_timedwait()
|
sl@0
|
1469 |
@see pthread_cond_wait()
|
sl@0
|
1470 |
|
sl@0
|
1471 |
|
sl@0
|
1472 |
|
sl@0
|
1473 |
|
sl@0
|
1474 |
@publishedAll
|
sl@0
|
1475 |
@externallyDefinedApi
|
sl@0
|
1476 |
*/
|
sl@0
|
1477 |
|
sl@0
|
1478 |
/** @fn pthread_cond_destroy(pthread_cond_t *cond)
|
sl@0
|
1479 |
@param cond
|
sl@0
|
1480 |
@return If successful, the pthread_cond_destroy function will return zero, otherwise an error number will be returned
|
sl@0
|
1481 |
to indicate the error.
|
sl@0
|
1482 |
|
sl@0
|
1483 |
The pthread_cond_destroy function frees the resources allocated by the condition variable cond .
|
sl@0
|
1484 |
|
sl@0
|
1485 |
Examples:
|
sl@0
|
1486 |
@code
|
sl@0
|
1487 |
int rc;
|
sl@0
|
1488 |
struct testdata
|
sl@0
|
1489 |
{
|
sl@0
|
1490 |
pthread_mutex_t mutex;
|
sl@0
|
1491 |
pthread_cond_t cond;
|
sl@0
|
1492 |
};
|
sl@0
|
1493 |
static struct testdata td;
|
sl@0
|
1494 |
int function1()
|
sl@0
|
1495 |
{
|
sl@0
|
1496 |
/* Initialize a condition variable object */
|
sl@0
|
1497 |
if (pthread_cond_init(&td.cond;, NULL) != 0) {
|
sl@0
|
1498 |
fprintf(stderr,"Fail to initialize cond
|
sl@0
|
1499 |
");
|
sl@0
|
1500 |
return -1;
|
sl@0
|
1501 |
}
|
sl@0
|
1502 |
/* ... Use it for wait, signal, broadcast */
|
sl@0
|
1503 |
/* Destroy the condition variable object */
|
sl@0
|
1504 |
if((rc=pthread_cond_destroy(&td.cond;)) != 0)
|
sl@0
|
1505 |
{
|
sl@0
|
1506 |
fprintf(stderr,"Error at pthread_cond_destroy(), rc=%d
|
sl@0
|
1507 |
",rc);
|
sl@0
|
1508 |
return -1;
|
sl@0
|
1509 |
}
|
sl@0
|
1510 |
}
|
sl@0
|
1511 |
|
sl@0
|
1512 |
@endcode
|
sl@0
|
1513 |
@see pthread_cond_broadcast()
|
sl@0
|
1514 |
@see pthread_cond_init()
|
sl@0
|
1515 |
@see pthread_cond_signal()
|
sl@0
|
1516 |
@see pthread_cond_timedwait()
|
sl@0
|
1517 |
@see pthread_cond_wait()
|
sl@0
|
1518 |
|
sl@0
|
1519 |
|
sl@0
|
1520 |
|
sl@0
|
1521 |
|
sl@0
|
1522 |
@publishedAll
|
sl@0
|
1523 |
@externallyDefinedApi
|
sl@0
|
1524 |
*/
|
sl@0
|
1525 |
|
sl@0
|
1526 |
/** @fn pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
|
sl@0
|
1527 |
@param cond
|
sl@0
|
1528 |
@param mutex
|
sl@0
|
1529 |
@return If successful, the pthread_cond_wait function will return zero.
|
sl@0
|
1530 |
Otherwise an error number will be returned to
|
sl@0
|
1531 |
indicate the error.
|
sl@0
|
1532 |
|
sl@0
|
1533 |
The pthread_cond_wait function atomically blocks the current thread waiting on the condition
|
sl@0
|
1534 |
variable specified by cond ,
|
sl@0
|
1535 |
and unblocks the mutex specified by mutex .
|
sl@0
|
1536 |
The waiting thread unblocks only after another thread calls pthread_cond_signal ,
|
sl@0
|
1537 |
or pthread_cond_broadcast with the same condition variable, and the current thread reacquires the lock
|
sl@0
|
1538 |
on mutex .
|
sl@0
|
1539 |
|
sl@0
|
1540 |
Examples:
|
sl@0
|
1541 |
@code
|
sl@0
|
1542 |
struct testdata
|
sl@0
|
1543 |
{
|
sl@0
|
1544 |
pthread_mutex_t mutex;
|
sl@0
|
1545 |
pthread_cond_t cond;
|
sl@0
|
1546 |
};
|
sl@0
|
1547 |
static struct testdata td;
|
sl@0
|
1548 |
int rc;
|
sl@0
|
1549 |
/* mutex and conditional variable must be initialized */
|
sl@0
|
1550 |
if (pthread_mutex_lock(&td.mutex;) != 0) {
|
sl@0
|
1551 |
fprintf(stderr,"Thread failed to acquire mutex
|
sl@0
|
1552 |
");
|
sl@0
|
1553 |
return -1;
|
sl@0
|
1554 |
}
|
sl@0
|
1555 |
fprintf(stderr,"Thread started
|
sl@0
|
1556 |
");
|
sl@0
|
1557 |
fprintf(stderr,"Thread is waiting for the cond
|
sl@0
|
1558 |
");
|
sl@0
|
1559 |
rc = pthread_cond_wait(&td.cond;, &td.mutex;);
|
sl@0
|
1560 |
if (rc != 0) {
|
sl@0
|
1561 |
fprintf(stderr,"pthread_cond_wait return %d
|
sl@0
|
1562 |
", rc);
|
sl@0
|
1563 |
return -1;
|
sl@0
|
1564 |
}
|
sl@0
|
1565 |
fprintf(stderr,"Thread wakened
|
sl@0
|
1566 |
");
|
sl@0
|
1567 |
pthread_mutex_unlock(&td.mutex;);
|
sl@0
|
1568 |
|
sl@0
|
1569 |
@endcode
|
sl@0
|
1570 |
@see pthread_cond_broadcast()
|
sl@0
|
1571 |
@see pthread_cond_destroy()
|
sl@0
|
1572 |
@see pthread_cond_init()
|
sl@0
|
1573 |
@see pthread_cond_signal()
|
sl@0
|
1574 |
@see pthread_cond_timedwait()
|
sl@0
|
1575 |
|
sl@0
|
1576 |
|
sl@0
|
1577 |
|
sl@0
|
1578 |
|
sl@0
|
1579 |
@publishedAll
|
sl@0
|
1580 |
@externallyDefinedApi
|
sl@0
|
1581 |
*/
|
sl@0
|
1582 |
|
sl@0
|
1583 |
/** @fn pthread_cond_signal(pthread_cond_t *cond)
|
sl@0
|
1584 |
@param cond
|
sl@0
|
1585 |
@return If successful, the pthread_cond_signal function will return zero, otherwise an error number will be returned
|
sl@0
|
1586 |
to indicate the error.
|
sl@0
|
1587 |
|
sl@0
|
1588 |
The pthread_cond_signal function unblocks one thread waiting for the condition variable cond .
|
sl@0
|
1589 |
|
sl@0
|
1590 |
Examples:
|
sl@0
|
1591 |
@code
|
sl@0
|
1592 |
int rc;
|
sl@0
|
1593 |
struct testdata
|
sl@0
|
1594 |
{
|
sl@0
|
1595 |
pthread_mutex_t mutex;
|
sl@0
|
1596 |
pthread_cond_t cond;
|
sl@0
|
1597 |
};
|
sl@0
|
1598 |
static struct testdata td;
|
sl@0
|
1599 |
int function1()
|
sl@0
|
1600 |
{
|
sl@0
|
1601 |
if (pthread_mutex_init(&td.mutex;, NULL) != 0) {
|
sl@0
|
1602 |
fprintf(stderr,"Fail to initialize mutex
|
sl@0
|
1603 |
");
|
sl@0
|
1604 |
return -1;
|
sl@0
|
1605 |
}
|
sl@0
|
1606 |
if (pthread_cond_init(&td.cond;, NULL) != 0) {
|
sl@0
|
1607 |
fprintf(stderr,"Fail to initialize cond
|
sl@0
|
1608 |
");
|
sl@0
|
1609 |
return -1;
|
sl@0
|
1610 |
}
|
sl@0
|
1611 |
/* ....... other thread wait for signal */
|
sl@0
|
1612 |
/* Acquire the mutex to make sure that all waiters are currently
|
sl@0
|
1613 |
blocked on pthread_cond_wait */
|
sl@0
|
1614 |
if (pthread_mutex_lock(&td.mutex;) != 0) {
|
sl@0
|
1615 |
fprintf(stderr,"Main: Fail to acquire mutex
|
sl@0
|
1616 |
");
|
sl@0
|
1617 |
return -1;
|
sl@0
|
1618 |
}
|
sl@0
|
1619 |
if (pthread_mutex_unlock(&td.mutex;) != 0) {
|
sl@0
|
1620 |
fprintf(stderr,"Main: Fail to release mutex
|
sl@0
|
1621 |
");
|
sl@0
|
1622 |
return -1;
|
sl@0
|
1623 |
}
|
sl@0
|
1624 |
/* signal the condition to wake up all waiters */
|
sl@0
|
1625 |
fprintf(stderr," signal the condition
|
sl@0
|
1626 |
");
|
sl@0
|
1627 |
rc = pthread_cond_signal(&td.cond;);
|
sl@0
|
1628 |
if (rc != 0) {
|
sl@0
|
1629 |
fprintf(stderr," failed to signal the condition
|
sl@0
|
1630 |
");
|
sl@0
|
1631 |
return -1;
|
sl@0
|
1632 |
}
|
sl@0
|
1633 |
}
|
sl@0
|
1634 |
|
sl@0
|
1635 |
@endcode
|
sl@0
|
1636 |
@see pthread_cond_broadcast()
|
sl@0
|
1637 |
@see pthread_cond_destroy()
|
sl@0
|
1638 |
@see pthread_cond_init()
|
sl@0
|
1639 |
@see pthread_cond_timedwait()
|
sl@0
|
1640 |
@see pthread_cond_wait()
|
sl@0
|
1641 |
|
sl@0
|
1642 |
|
sl@0
|
1643 |
|
sl@0
|
1644 |
|
sl@0
|
1645 |
@publishedAll
|
sl@0
|
1646 |
@externallyDefinedApi
|
sl@0
|
1647 |
*/
|
sl@0
|
1648 |
|
sl@0
|
1649 |
/** @fn pthread_cond_broadcast(pthread_cond_t *cond)
|
sl@0
|
1650 |
@param cond
|
sl@0
|
1651 |
@return If successful, the pthread_cond_broadcast function will return zero, otherwise an error number will be returned
|
sl@0
|
1652 |
to indicate the error.
|
sl@0
|
1653 |
|
sl@0
|
1654 |
The pthread_cond_broadcast function unblocks all threads waiting for the condition variable cond .
|
sl@0
|
1655 |
|
sl@0
|
1656 |
Examples:
|
sl@0
|
1657 |
@code
|
sl@0
|
1658 |
int rc;
|
sl@0
|
1659 |
struct testdata
|
sl@0
|
1660 |
{
|
sl@0
|
1661 |
pthread_mutex_t mutex;
|
sl@0
|
1662 |
pthread_cond_t cond;
|
sl@0
|
1663 |
};
|
sl@0
|
1664 |
static struct testdata td;
|
sl@0
|
1665 |
if (pthread_mutex_init(&td.mutex;, NULL) != 0) {
|
sl@0
|
1666 |
fprintf(stderr,"Fail to initialize mutex
|
sl@0
|
1667 |
");
|
sl@0
|
1668 |
return -1;
|
sl@0
|
1669 |
}
|
sl@0
|
1670 |
if (pthread_cond_init(&td.cond;, NULL) != 0) {
|
sl@0
|
1671 |
fprintf(stderr,"Fail to initialize cond
|
sl@0
|
1672 |
");
|
sl@0
|
1673 |
return -1;
|
sl@0
|
1674 |
}
|
sl@0
|
1675 |
/* ....... other thread wait for signal */
|
sl@0
|
1676 |
/* Acquire the mutex to make sure that all waiters are currently
|
sl@0
|
1677 |
blocked on pthread_cond_wait */
|
sl@0
|
1678 |
if (pthread_mutex_lock(&td.mutex;) != 0) {
|
sl@0
|
1679 |
fprintf(stderr,"Main: Fail to acquire mutex
|
sl@0
|
1680 |
");
|
sl@0
|
1681 |
return -1;
|
sl@0
|
1682 |
}
|
sl@0
|
1683 |
if (pthread_mutex_unlock(&td.mutex;) != 0) {
|
sl@0
|
1684 |
fprintf(stderr,"Main: Fail to release mutex
|
sl@0
|
1685 |
");
|
sl@0
|
1686 |
return -1;
|
sl@0
|
1687 |
}
|
sl@0
|
1688 |
/* signal the condition to wake up all waiters */
|
sl@0
|
1689 |
fprintf(stderr," signal the condition0);
|
sl@0
|
1690 |
rc = pthread_cond_broadcast(&td.cond;);
|
sl@0
|
1691 |
if (rc != 0) {
|
sl@0
|
1692 |
fprintf(stderr," failed to signal the condition
|
sl@0
|
1693 |
");
|
sl@0
|
1694 |
return -1;
|
sl@0
|
1695 |
}
|
sl@0
|
1696 |
|
sl@0
|
1697 |
@endcode
|
sl@0
|
1698 |
@see pthread_cond_destroy()
|
sl@0
|
1699 |
@see pthread_cond_init()
|
sl@0
|
1700 |
@see pthread_cond_signal()
|
sl@0
|
1701 |
@see pthread_cond_timedwait()
|
sl@0
|
1702 |
@see pthread_cond_wait()
|
sl@0
|
1703 |
|
sl@0
|
1704 |
|
sl@0
|
1705 |
|
sl@0
|
1706 |
|
sl@0
|
1707 |
@publishedAll
|
sl@0
|
1708 |
@externallyDefinedApi
|
sl@0
|
1709 |
*/
|
sl@0
|
1710 |
|
sl@0
|
1711 |
/** @fn pthread_key_create(pthread_key_t *key, destructor_routine dest)
|
sl@0
|
1712 |
@param key
|
sl@0
|
1713 |
@param dest
|
sl@0
|
1714 |
@return If successful, the pthread_key_create function will store the newly created key value at the location specified by key and returns zero.
|
sl@0
|
1715 |
Otherwise an error number will be returned to indicate
|
sl@0
|
1716 |
the error.
|
sl@0
|
1717 |
|
sl@0
|
1718 |
The pthread_key_create function creates a thread-specific data key visible to all threads in the
|
sl@0
|
1719 |
process.
|
sl@0
|
1720 |
Key values provided by pthread_key_create are opaque objects used to locate thread-specific data.
|
sl@0
|
1721 |
Although the same
|
sl@0
|
1722 |
key value may be used by different threads, the values bound to the key
|
sl@0
|
1723 |
by pthread_setspecific are maintained on a per-thread basis and persist for the life of the calling
|
sl@0
|
1724 |
thread.
|
sl@0
|
1725 |
|
sl@0
|
1726 |
Upon key creation, the value NULL is associated with the new key in all
|
sl@0
|
1727 |
active threads.
|
sl@0
|
1728 |
Upon thread creation, the value NULL is associated with all
|
sl@0
|
1729 |
defined keys in the new thread.
|
sl@0
|
1730 |
|
sl@0
|
1731 |
An optional destructor function dest may be associated with each key value.
|
sl@0
|
1732 |
At
|
sl@0
|
1733 |
thread exit, if a key value has a non-NULL destructor pointer, and the
|
sl@0
|
1734 |
thread has a non-NULL value associated with the key, the function pointed
|
sl@0
|
1735 |
to is called with the current associated value as its sole argument.
|
sl@0
|
1736 |
The
|
sl@0
|
1737 |
order of destructor calls is unspecified if more than one destructor exists
|
sl@0
|
1738 |
for a thread when it exits.
|
sl@0
|
1739 |
|
sl@0
|
1740 |
If, after all the destructors have been called for all non-NULL values
|
sl@0
|
1741 |
with associated destructors, there are still some non-NULL values with
|
sl@0
|
1742 |
associated destructors, then the process is repeated.
|
sl@0
|
1743 |
If, after at least
|
sl@0
|
1744 |
[PTHREAD_DESTRUCTOR_ITERATIONS] iterations of destructor calls for
|
sl@0
|
1745 |
outstanding non-NULL values, there are still some non-NULL values with
|
sl@0
|
1746 |
associated destructors, the implementation stops calling destructors.
|
sl@0
|
1747 |
|
sl@0
|
1748 |
Examples:
|
sl@0
|
1749 |
@code
|
sl@0
|
1750 |
pthread_key_t keys;
|
sl@0
|
1751 |
if(pthread_key_create(&keys;, NULL) != 0)
|
sl@0
|
1752 |
{
|
sl@0
|
1753 |
printf("Error: pthread_key_create() failed
|
sl@0
|
1754 |
");
|
sl@0
|
1755 |
return -1;
|
sl@0
|
1756 |
}
|
sl@0
|
1757 |
|
sl@0
|
1758 |
@endcode
|
sl@0
|
1759 |
@see pthread_getspecific()
|
sl@0
|
1760 |
@see pthread_key_delete()
|
sl@0
|
1761 |
@see pthread_setspecific()
|
sl@0
|
1762 |
|
sl@0
|
1763 |
|
sl@0
|
1764 |
|
sl@0
|
1765 |
|
sl@0
|
1766 |
@publishedAll
|
sl@0
|
1767 |
@externallyDefinedApi
|
sl@0
|
1768 |
*/
|
sl@0
|
1769 |
|
sl@0
|
1770 |
/** @fn pthread_key_delete(pthread_key_t key)
|
sl@0
|
1771 |
@param key
|
sl@0
|
1772 |
@return If successful, the pthread_key_delete function will return zero.
|
sl@0
|
1773 |
Otherwise an error number will be returned to
|
sl@0
|
1774 |
indicate the error.
|
sl@0
|
1775 |
|
sl@0
|
1776 |
The pthread_key_delete function deletes a thread-specific data key previously returned by pthread_key_create .
|
sl@0
|
1777 |
The thread-specific data values associated with key need not be NULL at the time that pthread_key_delete is called.
|
sl@0
|
1778 |
It is the responsibility of the application to free any
|
sl@0
|
1779 |
application storage or perform any cleanup actions for data structures
|
sl@0
|
1780 |
related to the deleted key or associated thread-specific data in any threads;
|
sl@0
|
1781 |
this cleanup can be done either before or after pthread_key_delete is called.
|
sl@0
|
1782 |
Any attempt to use key following the call to pthread_key_delete results in undefined behavior.
|
sl@0
|
1783 |
|
sl@0
|
1784 |
The pthread_key_delete function is callable from within destructor functions.
|
sl@0
|
1785 |
Destructor functions
|
sl@0
|
1786 |
are not invoked by pthread_key_delete .
|
sl@0
|
1787 |
Any destructor function that may have been associated with key will no longer be called upon thread exit.
|
sl@0
|
1788 |
|
sl@0
|
1789 |
Examples:
|
sl@0
|
1790 |
@code
|
sl@0
|
1791 |
pthread_key_t keys;
|
sl@0
|
1792 |
if(pthread_key_create(&keys;, NULL) != 0)
|
sl@0
|
1793 |
{
|
sl@0
|
1794 |
printf("Error: pthread_key_create() failed
|
sl@0
|
1795 |
");
|
sl@0
|
1796 |
return -1;
|
sl@0
|
1797 |
}
|
sl@0
|
1798 |
if(pthread_key_delete(keys) != 0)
|
sl@0
|
1799 |
{
|
sl@0
|
1800 |
printf("Error: pthread_key_delete() failed
|
sl@0
|
1801 |
");
|
sl@0
|
1802 |
return -1;
|
sl@0
|
1803 |
}
|
sl@0
|
1804 |
|
sl@0
|
1805 |
@endcode
|
sl@0
|
1806 |
@see pthread_getspecific()
|
sl@0
|
1807 |
@see pthread_key_create()
|
sl@0
|
1808 |
@see pthread_setspecific()
|
sl@0
|
1809 |
|
sl@0
|
1810 |
|
sl@0
|
1811 |
|
sl@0
|
1812 |
|
sl@0
|
1813 |
@publishedAll
|
sl@0
|
1814 |
@externallyDefinedApi
|
sl@0
|
1815 |
*/
|
sl@0
|
1816 |
|
sl@0
|
1817 |
/** @fn pthread_setspecific(pthread_key_t key, const void *value)
|
sl@0
|
1818 |
@param key
|
sl@0
|
1819 |
@param value
|
sl@0
|
1820 |
@return If successful, the pthread_setspecific function will return zero.
|
sl@0
|
1821 |
Otherwise an error number will be returned to
|
sl@0
|
1822 |
indicate the error.
|
sl@0
|
1823 |
|
sl@0
|
1824 |
The pthread_setspecific function associates a thread-specific value with a key obtained via a previous call to pthread_key_create .
|
sl@0
|
1825 |
Different threads can bind different values to the same key.
|
sl@0
|
1826 |
These values are
|
sl@0
|
1827 |
typically pointers to blocks of dynamically allocated memory that have been
|
sl@0
|
1828 |
reserved for use by the calling thread.
|
sl@0
|
1829 |
|
sl@0
|
1830 |
The effect of calling pthread_setspecific with a key value not obtained from pthread_key_create or after key has been deleted with pthread_key_delete is undefined.
|
sl@0
|
1831 |
|
sl@0
|
1832 |
The pthread_setspecific function may be called from a thread-specific data destructor function,
|
sl@0
|
1833 |
however this may result in lost storage or infinite loops.
|
sl@0
|
1834 |
|
sl@0
|
1835 |
Examples:
|
sl@0
|
1836 |
@code
|
sl@0
|
1837 |
pthread_key_t keys;
|
sl@0
|
1838 |
void* rc;
|
sl@0
|
1839 |
if(pthread_key_create(&keys;, NULL) != 0)
|
sl@0
|
1840 |
{
|
sl@0
|
1841 |
printf("Error: pthread_key_create() failed
|
sl@0
|
1842 |
");
|
sl@0
|
1843 |
return -1;
|
sl@0
|
1844 |
} else
|
sl@0
|
1845 |
{
|
sl@0
|
1846 |
if(pthread_setspecific(keys, (void *)(long)(100)) != 0)
|
sl@0
|
1847 |
{
|
sl@0
|
1848 |
printf("Error: pthread_setspecific() failed
|
sl@0
|
1849 |
");
|
sl@0
|
1850 |
return -1;
|
sl@0
|
1851 |
}
|
sl@0
|
1852 |
}
|
sl@0
|
1853 |
|
sl@0
|
1854 |
@endcode
|
sl@0
|
1855 |
@see pthread_getspecific()
|
sl@0
|
1856 |
@see pthread_key_create()
|
sl@0
|
1857 |
@see pthread_key_delete()
|
sl@0
|
1858 |
|
sl@0
|
1859 |
|
sl@0
|
1860 |
|
sl@0
|
1861 |
|
sl@0
|
1862 |
@publishedAll
|
sl@0
|
1863 |
@externallyDefinedApi
|
sl@0
|
1864 |
*/
|
sl@0
|
1865 |
|
sl@0
|
1866 |
/** @fn pthread_getspecific(pthread_key_t key)
|
sl@0
|
1867 |
@param key
|
sl@0
|
1868 |
@return The pthread_getspecific function will return the thread-specific data value associated with the given key .
|
sl@0
|
1869 |
If no thread-specific data value is associated with key ,
|
sl@0
|
1870 |
then the value NULL is returned.
|
sl@0
|
1871 |
|
sl@0
|
1872 |
The pthread_getspecific function returns the value currently bound to the specified key on behalf of the calling thread.
|
sl@0
|
1873 |
|
sl@0
|
1874 |
The effect of calling pthread_getspecific with a key value not obtained from pthread_key_create or after key has been deleted with pthread_key_delete is undefined.
|
sl@0
|
1875 |
|
sl@0
|
1876 |
The pthread_getspecific function may be called from a thread-specific data destructor function.
|
sl@0
|
1877 |
|
sl@0
|
1878 |
Examples:
|
sl@0
|
1879 |
@code
|
sl@0
|
1880 |
pthread_key_t keys;
|
sl@0
|
1881 |
void* rc;
|
sl@0
|
1882 |
if(pthread_key_create(&keys;, NULL) != 0)
|
sl@0
|
1883 |
{
|
sl@0
|
1884 |
printf("Error: pthread_key_create() failed0);
|
sl@0
|
1885 |
return -1;
|
sl@0
|
1886 |
} else
|
sl@0
|
1887 |
{
|
sl@0
|
1888 |
if(pthread_setspecific(keys, (void *)(long)(100)) != 0)
|
sl@0
|
1889 |
{
|
sl@0
|
1890 |
printf("Error: pthread_setspecific() failed
|
sl@0
|
1891 |
");
|
sl@0
|
1892 |
return -1;
|
sl@0
|
1893 |
}
|
sl@0
|
1894 |
}
|
sl@0
|
1895 |
rc = pthread_getspecific(keys);
|
sl@0
|
1896 |
if(rc != (void *)(long)(100))
|
sl@0
|
1897 |
{
|
sl@0
|
1898 |
printf("getspecific failed
|
sl@0
|
1899 |
");
|
sl@0
|
1900 |
return -1;
|
sl@0
|
1901 |
}
|
sl@0
|
1902 |
|
sl@0
|
1903 |
@endcode
|
sl@0
|
1904 |
@see pthread_key_create()
|
sl@0
|
1905 |
@see pthread_key_delete()
|
sl@0
|
1906 |
@see pthread_setspecific()
|
sl@0
|
1907 |
|
sl@0
|
1908 |
|
sl@0
|
1909 |
|
sl@0
|
1910 |
|
sl@0
|
1911 |
@publishedAll
|
sl@0
|
1912 |
@externallyDefinedApi
|
sl@0
|
1913 |
*/
|
sl@0
|
1914 |
|
sl@0
|
1915 |
/** @fn pthread_attr_getscope(const pthread_attr_t *attrib, int *scope)
|
sl@0
|
1916 |
@param attrib
|
sl@0
|
1917 |
@param scope
|
sl@0
|
1918 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
1919 |
@see pthread_create()
|
sl@0
|
1920 |
|
sl@0
|
1921 |
|
sl@0
|
1922 |
|
sl@0
|
1923 |
|
sl@0
|
1924 |
@publishedAll
|
sl@0
|
1925 |
@externallyDefinedApi
|
sl@0
|
1926 |
*/
|
sl@0
|
1927 |
|
sl@0
|
1928 |
/** @fn pthread_attr_setscope(pthread_attr_t *attrib, int conscope)
|
sl@0
|
1929 |
@param attrib
|
sl@0
|
1930 |
@param conscope
|
sl@0
|
1931 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
1932 |
@see pthread_create()
|
sl@0
|
1933 |
|
sl@0
|
1934 |
|
sl@0
|
1935 |
|
sl@0
|
1936 |
|
sl@0
|
1937 |
@publishedAll
|
sl@0
|
1938 |
@externallyDefinedApi
|
sl@0
|
1939 |
*/
|
sl@0
|
1940 |
|
sl@0
|
1941 |
/** @fn pthread_attr_setschedpolicy(pthread_attr_t *attrib, int policy)
|
sl@0
|
1942 |
@param attrib
|
sl@0
|
1943 |
@param policy
|
sl@0
|
1944 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
1945 |
@see pthread_create()
|
sl@0
|
1946 |
|
sl@0
|
1947 |
|
sl@0
|
1948 |
|
sl@0
|
1949 |
|
sl@0
|
1950 |
@publishedAll
|
sl@0
|
1951 |
@externallyDefinedApi
|
sl@0
|
1952 |
*/
|
sl@0
|
1953 |
|
sl@0
|
1954 |
/** @fn pthread_attr_getschedpolicy(const pthread_attr_t *attrib, int *policy)
|
sl@0
|
1955 |
@param attrib
|
sl@0
|
1956 |
@param policy
|
sl@0
|
1957 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
1958 |
@see pthread_create()
|
sl@0
|
1959 |
|
sl@0
|
1960 |
|
sl@0
|
1961 |
|
sl@0
|
1962 |
|
sl@0
|
1963 |
@publishedAll
|
sl@0
|
1964 |
@externallyDefinedApi
|
sl@0
|
1965 |
*/
|
sl@0
|
1966 |
|
sl@0
|
1967 |
/** @fn pthread_attr_getschedparam(const pthread_attr_t *attrib, struct sched_param *param)
|
sl@0
|
1968 |
@param attrib
|
sl@0
|
1969 |
@param param
|
sl@0
|
1970 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
1971 |
@see pthread_create()
|
sl@0
|
1972 |
|
sl@0
|
1973 |
|
sl@0
|
1974 |
|
sl@0
|
1975 |
|
sl@0
|
1976 |
@publishedAll
|
sl@0
|
1977 |
@externallyDefinedApi
|
sl@0
|
1978 |
*/
|
sl@0
|
1979 |
|
sl@0
|
1980 |
/** @fn pthread_attr_setschedparam(pthread_attr_t *attrib, const struct sched_param *param)
|
sl@0
|
1981 |
@param attrib
|
sl@0
|
1982 |
@param param
|
sl@0
|
1983 |
Refer pthread_attr_init() for the documentation
|
sl@0
|
1984 |
@see pthread_create()
|
sl@0
|
1985 |
|
sl@0
|
1986 |
|
sl@0
|
1987 |
|
sl@0
|
1988 |
|
sl@0
|
1989 |
@publishedAll
|
sl@0
|
1990 |
@externallyDefinedApi
|
sl@0
|
1991 |
*/
|
sl@0
|
1992 |
|
sl@0
|
1993 |
/** @fn pthread_getschedparam(pthread_t thr, int *policy, struct sched_param *param)
|
sl@0
|
1994 |
@param thr
|
sl@0
|
1995 |
@param policy
|
sl@0
|
1996 |
@param param
|
sl@0
|
1997 |
Refer pthread_setschedparam() for the documentation
|
sl@0
|
1998 |
|
sl@0
|
1999 |
|
sl@0
|
2000 |
|
sl@0
|
2001 |
|
sl@0
|
2002 |
@publishedAll
|
sl@0
|
2003 |
@externallyDefinedApi
|
sl@0
|
2004 |
*/
|
sl@0
|
2005 |
|
sl@0
|
2006 |
/** @fn pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
|
sl@0
|
2007 |
@param thread
|
sl@0
|
2008 |
@param policy
|
sl@0
|
2009 |
@param param
|
sl@0
|
2010 |
Note: This description also covers the following functions -
|
sl@0
|
2011 |
pthread_getschedparam()
|
sl@0
|
2012 |
|
sl@0
|
2013 |
@return If successful, these functions return 0.
|
sl@0
|
2014 |
Otherwise, an error number is returned to indicate the error.
|
sl@0
|
2015 |
|
sl@0
|
2016 |
The pthread_setschedparam and pthread_getschedparam functions set and get the scheduling parameters of individual threads.
|
sl@0
|
2017 |
The scheduling policy for a thread will be SCHED_RR (round-robin). ( SCHED_FIFO is not supported)
|
sl@0
|
2018 |
The thread priority (accessed via param-\>sched_priority )
|
sl@0
|
2019 |
must be at least PTHREAD_MIN_PRIORITY and no more than PTHREAD_MAX_PRIORITY(as returned by sched_get_priority_max() and sched_get_priority_min() APIs) .
|
sl@0
|
2020 |
|
sl@0
|
2021 |
Examples:
|
sl@0
|
2022 |
@code
|
sl@0
|
2023 |
struct sched_param sparam;
|
sl@0
|
2024 |
int policy, priority, policy_1;
|
sl@0
|
2025 |
int rc;
|
sl@0
|
2026 |
policy = SCHED_RR;
|
sl@0
|
2027 |
priority = 50;
|
sl@0
|
2028 |
sparam.sched_priority = priority;
|
sl@0
|
2029 |
rc = pthread_setschedparam(pthread_self(), policy, &sparam;);
|
sl@0
|
2030 |
if (rc != 0)
|
sl@0
|
2031 |
{
|
sl@0
|
2032 |
printf("Error at pthread_setschedparam: rc=%d
|
sl@0
|
2033 |
", rc);
|
sl@0
|
2034 |
return -1;
|
sl@0
|
2035 |
}
|
sl@0
|
2036 |
|
sl@0
|
2037 |
@endcode
|
sl@0
|
2038 |
@code
|
sl@0
|
2039 |
int e ;
|
sl@0
|
2040 |
struct sched_param param;
|
sl@0
|
2041 |
pthread_t thread; // Thread which will be assigned the scheduling priority and the policy.
|
sl@0
|
2042 |
pthread_t thread = pthread_self();
|
sl@0
|
2043 |
param.sched_priority = 100;
|
sl@0
|
2044 |
|
sl@0
|
2045 |
e = pthread_setschedparam(thread,SCHED_RR, & param);
|
sl@0
|
2046 |
if(e != 0)
|
sl@0
|
2047 |
{
|
sl@0
|
2048 |
printf("setting scheduling policy and priority failed."));
|
sl@0
|
2049 |
return -1;
|
sl@0
|
2050 |
}
|
sl@0
|
2051 |
|
sl@0
|
2052 |
@endcode
|
sl@0
|
2053 |
@code
|
sl@0
|
2054 |
int e ;
|
sl@0
|
2055 |
struct sched_param param;
|
sl@0
|
2056 |
pthread_t thread; // Thread which will be assigned the scheduling priority and the policy.
|
sl@0
|
2057 |
int policy;
|
sl@0
|
2058 |
pthread_t thread = pthread_self();
|
sl@0
|
2059 |
param.sched_priority = 100;
|
sl@0
|
2060 |
|
sl@0
|
2061 |
e = pthread_setschedparam(thread,SCHED_RR, & param);
|
sl@0
|
2062 |
if(e != 0)
|
sl@0
|
2063 |
{
|
sl@0
|
2064 |
printf("setting scheduling policy and priority failed."));
|
sl@0
|
2065 |
return -1;
|
sl@0
|
2066 |
}
|
sl@0
|
2067 |
e = pthread_getschedparam(thread,&policy;,& param);
|
sl@0
|
2068 |
if (e != 0)
|
sl@0
|
2069 |
{
|
sl@0
|
2070 |
printf("getting scheduling policy and priority failed."));
|
sl@0
|
2071 |
return -1;
|
sl@0
|
2072 |
}
|
sl@0
|
2073 |
|
sl@0
|
2074 |
@endcode
|
sl@0
|
2075 |
|
sl@0
|
2076 |
|
sl@0
|
2077 |
@publishedAll
|
sl@0
|
2078 |
@externallyDefinedApi
|
sl@0
|
2079 |
*/
|
sl@0
|
2080 |
|
sl@0
|
2081 |
|
sl@0
|
2082 |
/** @fn pthread_cond_timedwait(pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec *abstime)
|
sl@0
|
2083 |
@param cond
|
sl@0
|
2084 |
@param mutex
|
sl@0
|
2085 |
@param abstime
|
sl@0
|
2086 |
@return If successful, the pthread_cond_timedwait function will return zero.
|
sl@0
|
2087 |
Otherwise an error number will be returned to
|
sl@0
|
2088 |
indicate the error.
|
sl@0
|
2089 |
|
sl@0
|
2090 |
The pthread_cond_timedwait function atomically blocks the current thread waiting on the condition,
|
sl@0
|
2091 |
specified by the cond,and unblocks the mutex specified by the mutex.
|
sl@0
|
2092 |
|
sl@0
|
2093 |
The waiting thread unblocks only after another thread calls pthread_cond_signal,pthread_cond_broadcast with the same condition variable,
|
sl@0
|
2094 |
or if the system time reaches the time specified in abstime,and the current thread reacquires the lock on the mutex.
|
sl@0
|
2095 |
|
sl@0
|
2096 |
|
sl@0
|
2097 |
|
sl@0
|
2098 |
Examples:
|
sl@0
|
2099 |
@code
|
sl@0
|
2100 |
struct testdata
|
sl@0
|
2101 |
{
|
sl@0
|
2102 |
pthread_mutex mutex;
|
sl@0
|
2103 |
pthread_cond_t cond;
|
sl@0
|
2104 |
};
|
sl@0
|
2105 |
static struct testdata td;
|
sl@0
|
2106 |
int rc;
|
sl@0
|
2107 |
struct timespec timeout;
|
sl@0
|
2108 |
struct timeval curtime;
|
sl@0
|
2109 |
if(pthread_mutex_lock(&td.mutex)!=0)
|
sl@0
|
2110 |
{
|
sl@0
|
2111 |
fprintf(stderr,"fail");
|
sl@0
|
2112 |
return -1;
|
sl@0
|
2113 |
}
|
sl@0
|
2114 |
if(gettimeofday(&curtime,NULL)!=0)
|
sl@0
|
2115 |
{
|
sl@0
|
2116 |
fprintf(stderr,"fail");
|
sl@0
|
2117 |
return -1;
|
sl@0
|
2118 |
}
|
sl@0
|
2119 |
timeout.tv_sec=curtime.tv_sec;
|
sl@0
|
2120 |
timeout.tv_nsec=curtime.tv_nsec;
|
sl@0
|
2121 |
timeout.tv_sec+=TIMEOUT;
|
sl@0
|
2122 |
fprintf(stderr,"thread is waiting on the cond");
|
sl@0
|
2123 |
rc=pthread_cond_timedwait(&td.cond,&td.mutex,&timeout);
|
sl@0
|
2124 |
if(rc!=0)
|
sl@0
|
2125 |
{
|
sl@0
|
2126 |
if(rc==ETIMEDOUT)
|
sl@0
|
2127 |
{
|
sl@0
|
2128 |
fprintf(stderr,"thread stops when time is out");
|
sl@0
|
2129 |
return -1;
|
sl@0
|
2130 |
}
|
sl@0
|
2131 |
else
|
sl@0
|
2132 |
{
|
sl@0
|
2133 |
fprintf(stderr,"pthread_cond_timedwait return");
|
sl@0
|
2134 |
return -1;
|
sl@0
|
2135 |
}
|
sl@0
|
2136 |
}
|
sl@0
|
2137 |
fprintf(stderr,"thread wakened up");
|
sl@0
|
2138 |
pthread_mutex_unlock(&td.mutex);
|
sl@0
|
2139 |
@endcode
|
sl@0
|
2140 |
@see pthread_exit()
|
sl@0
|
2141 |
@see pthread_join()
|
sl@0
|
2142 |
|
sl@0
|
2143 |
|
sl@0
|
2144 |
|
sl@0
|
2145 |
|
sl@0
|
2146 |
@publishedAll
|
sl@0
|
2147 |
@externallyDefinedApi
|
sl@0
|
2148 |
*/
|