#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *threadhandle(
void *
);
int sum=
0;
pthread_mutexattr_t mt=
PTHREAD_COND_INITIALIZER;
int main()
{
pthread_t pt[10];
for(
int i=
0;i<
10;i++
)
{
pthread_create(&
pt[i],NULL,threadhandle,i);
}
for(
int i=
0;i<
10;i++
)
{
pthread_join(pt[i],NULL);
}
printf("sum=%d",sum);
return 0;
}
void *threadhandle(
void *
args)
{
pthread_t pt;
pt=
pthread_self();
for(
int i=
0;i<
10000;i++
)
{
pthread_mutex_lock(&
mt);
sum++
;
pthread_mutex_unlock(&
mt);
// printf("thread id=%d,i=%d\r\n",(int)args,i);
}
}
转载于:https://www.cnblogs.com/yixiong/p/3529241.html
转载请注明原文地址: https://win8.8miu.com/read-1556490.html