#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <pthread.h>
#define NUM 32
static int i = 0;
int par[1];
pthread_mutex_t pmutex_t;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,(unsigned int)tid, (unsigned int)tid);
}
int printfThreadid()
{
printids("new thread: ");
return 0;
}
int count(int *pthread_num)
{
pthread_mutex_lock(&pmutex_t);
printf("tid = %u,i = %d\n",pthread_self(),i++);
pthread_mutex_unlock(&pmutex_t);
}
void main()
{
pthread_t pth_t[NUM];
int iret ;
int i = 0;
pthread_mutex_init(&pmutex_t,NULL);
for(i = 0; i< NUM; i++)
{
par[0] = i + 1;
iret = pthread_create(&pth_t[i],NULL,(void *)count,(void *)par);
if(iret != 0)
{
printf("create thread failed! because %s\n",strerror(iret));
}
}
sleep(3);
for(i = 0; i< NUM; i++)
{
printf("thread exit\n");
pthread_join(pth_t[i],NULL);
}
if (i == NUM)
{
pthread_mutex_destroy(&pmutex_t);
}
exit(0);
}
#include <stdlib.h>
#include <error.h>
#include <pthread.h>
#define NUM 32
static int i = 0;
int par[1];
pthread_mutex_t pmutex_t;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,(unsigned int)tid, (unsigned int)tid);
}
int printfThreadid()
{
printids("new thread: ");
return 0;
}
int count(int *pthread_num)
{
pthread_mutex_lock(&pmutex_t);
printf("tid = %u,i = %d\n",pthread_self(),i++);
pthread_mutex_unlock(&pmutex_t);
}
void main()
{
pthread_t pth_t[NUM];
int iret ;
int i = 0;
pthread_mutex_init(&pmutex_t,NULL);
for(i = 0; i< NUM; i++)
{
par[0] = i + 1;
iret = pthread_create(&pth_t[i],NULL,(void *)count,(void *)par);
if(iret != 0)
{
printf("create thread failed! because %s\n",strerror(iret));
}
}
sleep(3);
for(i = 0; i< NUM; i++)
{
printf("thread exit\n");
pthread_join(pth_t[i],NULL);
}
if (i == NUM)
{
pthread_mutex_destroy(&pmutex_t);
}
exit(0);
}