0
2 2
1 1
0 0
3 3
1610766130 4
正常水煤浆或错误在我的代码?
代码:
#ifdef __cplusplus
extern "C" {
#endif
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define N_TREADS 5
void *p(void* in)
{
int w;
void * word;
word = in;
w = *((int*)word);
usleep((rand() % 1000) + 1000);
printf("%i %i\n", *((int*)word),w);
pthread_exit(NULL);
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t threads[N_TREADS];
int numberz[N_TREADS];
int rc,i;
for(i =0;i< N_TREADS; i++)
{
numberz[i]=i;
rc = pthread_create(&threads[i], NULL, p, (void*)&numberz[i]);
if(rc)
{
printf("error");
exit(-1);
}
}
pthread_exit(NULL);
}
#ifdef __cplusplus
}
#endif
这很棒。看起来像网上的大多数例子都有保存错误。 – 2011-05-19 11:19:49