2,408
社区成员
发帖
与我相关
我的任务
分享
#define NUM 1000
pthread_mutex_t mutex;
static int threads = 2;
static int iter = NUM;
long arr[NUM];
static rbtree_t * rbtreePtr;
void* task(void* arg){
unsigned short id = (unsigned short)(unsigned int)arg;
int n = iter;
long key;
unsigned long seed = id;
unsigned long rand;
while (n--) {
rand = Random(&seed);
key = rand % 1000;
arr[n-1]=key;
pthread_mutex_lock(&mutex);
printf("%d ", n);
insertInt(rbtreePtr, &key);
pthread_mutex_unlock(&mutex);
}
return 0;
}
int
main (int argc, char** argv)
{
int ch,i;
pthread_t t[256];
rbtreePtr = rbtree_alloc(&compare);\\相当于给树初始化,没有给出compare函数
if(threads>256) threads = 256;
for(i=0; i<threads; i++)
pthread_create(&t[i], NULL, task, i+1);
for(i=0; i<threads; i++)
pthread_join(t[i], NULL);
}
pthread_mutex_lock(&mutex);
printf("%d ", n);
insertInt(rbtreePtr, &key);
pthread_mutex_unlock(&mutex);