1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | /*****************************************************************************
|
|---|
| 2 | * FILE: bug3.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * This "hello world" Pthreads program demonstrates an unsafe (incorrect)
|
|---|
| 5 | * way to pass thread arguments at thread creation. Compare with hello_arg1.c.
|
|---|
| 6 | * AUTHOR: Blaise Barney
|
|---|
| 7 | * LAST REVISED: 01/29/09
|
|---|
| 8 | ******************************************************************************/
|
|---|
| 9 | #include <pthread.h>
|
|---|
| 10 | #include <stdio.h>
|
|---|
| 11 | #include <stdlib.h>
|
|---|
| 12 | #define NUM_THREADS 8
|
|---|
| 13 |
|
|---|
| 14 | void *PrintHello(void *threadid)
|
|---|
| 15 | {
|
|---|
| 16 | long taskid = (long)threadid;
|
|---|
| 17 | sleep(1);
|
|---|
| 18 | printf("Hello from thread %ld\n", taskid);
|
|---|
| 19 | pthread_exit(NULL);
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | int main(int argc, char *argv[])
|
|---|
| 23 | {
|
|---|
| 24 | pthread_t threads[NUM_THREADS];
|
|---|
| 25 | int rc;
|
|---|
| 26 | long t;
|
|---|
| 27 |
|
|---|
| 28 | for(t=0;t<NUM_THREADS;t++) {
|
|---|
| 29 | printf("Creating thread %ld\n", t);
|
|---|
| 30 | rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t);
|
|---|
| 31 | if (rc) {
|
|---|
| 32 | printf("ERROR; return code from pthread_create() is %d\n", rc);
|
|---|
| 33 | exit(-1);
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | pthread_exit(NULL);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.