-
Notifications
You must be signed in to change notification settings - Fork 49
/
inertia.c
56 lines (50 loc) · 1.48 KB
/
inertia.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* inertia.c
*
* Demonstrate a class of programming error, assuming that
* threads can't start right away.
*
* Special notes: On a Solaris 2.5 uniprocessor, this test can't
* fail unless a second LWP is created by calling
* thr_setconcurrency() because threads are not timesliced.
*/
#include <pthread.h>
#include "errors.h"
void *printer_thread (void *arg)
{
char *string = *(char**)arg;
printf ("%s\n", string);
return NULL;
}
int main (int argc, char *argv[])
{
pthread_t printer_id;
char *string_ptr;
int i, status;
#ifdef sun
/*
* On Solaris 2.5, threads are not timesliced. To ensure
* that our two threads can run concurrently, we need to
* increase the concurrency level to 2.
*/
DPRINTF (("Setting concurrency level to 2\n"));
thr_setconcurrency (2);
#endif
string_ptr = "Before value";
status = pthread_create (
&printer_id, NULL, printer_thread, (void*)&string_ptr);
if (status != 0)
err_abort (status, "Create thread");
/*
* Give the thread a chance to get started if it's going to run
* in parallel, but not enough that the current thread is likely
* to be timesliced. (This is a tricky balance, and the loop may
* need to be adjusted on your system before you can see the bug.)
*/
for (i = 0; i < 10000000; i++);
string_ptr = "After value";
status = pthread_join (printer_id, NULL);
if (status != 0)
err_abort (status, "Join thread");
return 0;
}