|

楼主 |
发表于 2004-11-4 17:44:11
|
显示全部楼层
lot是 int 类型,arg 好像是 void类型!
更正:
(void *) *arg 和 (void *) arg 应该为 *(void *)arg 和 (void *) arg
整个程序是:
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <pthread.h>
- #define NUM_THREADS 6
- void *thread_function(void *arg);
- int main(void)
- {
- int res, lots_of_threads;
- pthread_t a_thread[NUM_THREADS];
- void *thread_result;
- for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++)
- {
- res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function,
- [color=red](void *)lots_of_threads[/color]);
- if(res != 0)
- {
- perror("phtread create failed\n");
- exit(EXIT_FAILURE);
- }
- sleep(1);
- }
- printf("Waiting for threads of finish...\n");
- for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--)
- {
- res = pthread_join(a_thread[lots_of_threads], &thread_result);
- if(res == 0)
- {
- printf("Picked up a thread\n");
- }
- else
- {
- perror("pthread_join failed\n");
- }
- }
- printf("All done\n");
- return 0;
- }
- void *thread_function(void *arg)
- {
- [color=red]int my_number = (int )arg;[/color]
- int rand_num;
- printf("thread_function is runing. Argument was %d\n", my_number);
- rand_num = 1 + (int)(9.0*rand() / (RAND_MAX+1.0));
- sleep(rand_num);
- printf("Bye form %d\n", my_number);
- pthread_exit(NULL);
- }
复制代码
看到书上说把(void *)&lots_of_threads,*(int *)arg改为红字部分可以解决某些问题 |
|