简单的一个插入排序过程初级版本.
错误版本
void *InsArray(void *args)
{
SLink *slist=((void **)args)[0];
int *array=((void **)args)[1];
int *n=((void **)args)[2];
pthread_mutex_t *mutex=((void **)args)[3];
int *myid=((void **)args)[4];
int i;
printf("pthread_self %d, addr in passing parameters %d\n",pthread_self(),start);
for(i=0;i< *n; i++)
{
pthread_mutex_lock(mutex);
printf("myid %d\n",pthread_self()%10);
InsElem(slist, array[i]);
printf("-----------------------------------------------------\n");
DispList(slist);
printf("\n");
printf("-----------------------------------------------------\n");
pthread_mutex_unlock(mutex);
}
}
void **args=(void*)malloc(sizeof(void *)*5);
for(i=0;i<N;i++)
{
int myid=i;
// void *args[5]={slist,array+n*i,&n,mutex,&myid};
args[0]=slist;
j=n*i;
args[1]=&j;
//args[1]=array+n*i;
args[2]=&n;
args[3]=mutex;
args[4]=&myid;
// InsArray(args);
printf("addr before passing parameters %d\n",args[1]);
pthread_create(&tid[i],NULL,InsArray,args);
printf("%d\t addr after passing parameters %d\n",tid[i], args[1]);
}
正确版本
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- //#include <sys/types.h>
- #define ElemType int
- #define N 5
- typedef struct node
- {
- ElemType data;
- struct node *next;
- }SLink;
- // basic operation on single link list
- void InitList(SLink **sl)
- {
- SLink *list;
- list=(SLink*)malloc(sizeof(SLink)); // create head node
- // list->next=NULL;
- list->data=-1; //smallest
- *sl=list;
- }
- // insert a node
- int InsElem(SLink *sl ,ElemType x)
- {
- SLink *curr;
- SLink *pre=sl;
- curr=pre->next;
- while(curr!=NULL && x > curr->data )
- {
- pre=curr;
- curr=pre->next;
- }
- if(curr !=NULL && x== curr->data) /// ! curr is not null and ...
- {
- printf(" %d has already in\n", x);
- return -1;
- }
- else
- {
- SLink *s;
- s=(SLink*)malloc(sizeof(SLink)); // create node s
- s->data=x;
- s->next=NULL;
- s->next=curr;
- pre->next=s;
- return 0;
- }
- }
- int DelElem(SLink *sl,int data)
- {
- SLink *curr;
- SLink *pre=sl;
- curr=pre->next;
- while(curr!=NULL && data > curr->data )
- {
- pre=curr;
- curr=pre->next;
- }
- if(curr!=NULL && data == curr->data)
- {
- pre->next=curr->next;
- return 0;
- }
- else
- {
- printf(" %d does not exist\n", data);
- return -1;
- }
- }
- void DispList(SLink *sl)
- {
- SLink *p=sl->next;
- while(p!=NULL)
- {
- printf("%d\t",p->data);
- p=p->next;
- }
- printf("\n");
- }
- void *InsArray(void *args)
- {
- SLink *slist=((void **)args)[0];
- int *array=((void **)args)[1];
- int *n=((void **)args)[2];
- pthread_mutex_t *mutex=((void **)args)[3];
- int *myid=((void **)args)[4];
- int i;
- printf("pthread_self %d, addr in passing parameters %d\n",pthread_self(),((void **)args)[1]);
- for(i=0;i< *n; i++)
- {
- pthread_mutex_lock(mutex);
- printf("myid %d\n",pthread_self()%10);
- InsElem(slist, array[i]);
- //printf("-----------------------------------------------------\n");
- DispList(slist);
- //printf("\n");
- //printf("-----------------------------------------------------\n");
- pthread_mutex_unlock(mutex);
- }
- }
- int main()
- {
- SLink *slist;
- int i;
- int a[5]={0,9,2,6,4};
- SLink *lis;
- InitList(&slist);
- pthread_t tid[N];
- int n=10;
- int *array=(int *)malloc(sizeof(int)*n*5);
- srand(13);
- for(i=0;i<n*N;i++)
- array[i]=rand()%8191;
- int j;
- for(i=0;i<N;i++)
- {
- for(j=0;j<n;j++)
- printf("%d\t",array[i*n+j]);
- printf("\n");
- }
- printf("-----------------------------------------------------\n");
- pthread_mutex_t *mutex=(pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
- pthread_mutex_init(mutex,NULL);
- void ***args=(void*)malloc(sizeof(void **)*5);
- for(i=0;i<5;i++)
- args[i]=(void *)malloc(sizeof(void *)*5);
- for(i=0;i<N;i++)
- {
- int myid=i;
- args[i][0]=slist;
- args[i][1]=array+n*i;
- args[i][2]=&n;
- args[i][3]=mutex;
- args[i][4]=&myid;
- //printf("addr before passing parameters %d\n",args[i][1]);
- pthread_create(&tid[i],NULL,InsArray,args[i]);
- //printf("%d\t addr after passing parameters %d\n",tid[i], args[i][1]);
- }
- for(i=0;i<N;i++)
- pthread_join(tid[i],NULL);
- DispList(slist);
- pthread_mutex_destroy(mutex);
- return 0;
- }
原因create时,参数要分别申请空间,不能共用一个args