我需要执行以下操作: 创建一个线程,可以在一行中创建10个线程。 每个线程只打印它的ID并睡眠n秒,其中n是当前线程的序列号。 但是,我无法获得传递参数的权利,当我运行我的代码时,它似乎像线程只是在睡觉..有些帮助吗?Pthread ID和睡眠
这里是我的代码:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
# define N 10
void* printID (void *i)
{
int* p=(int*) i;
sleep(p);
pthread_exit(NULL);
}
void* th (void* unused)
{
int sec,i;
sec=1;
i=1;
while(i<=10){
pthread_t pid1;
pthread_create (&pid1, NULL, &printID, (void *)&i);
pthread_join(pid1,NULL);
printf("Thread ID je: %d \n",(int) pid1);
i=i+1;
}
}
int main(){
pthread_t pid;
pthread_create (&pid, NULL, &th, NULL);
pthread_join(pid,NULL);
return 0;
}
启用您的编译器的警告。 – Quentin