#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int flg=0;
typedef struct{
pthread_t pid;
int time;
}CONTROL;
CONTROL my_control[3];
void dump()
{
int i=3;
//pid_t pid = getpid();
pthread_t pid = pthread_self();
do{
sleep(1);
std::cout << "Im running, pid:= " << pid << " \n" << std::endl;
i--;
}while(i>0);#include <iostream>
}
void *start_thread(void *args){
std::cout << "Im running------- "<< std::endl;
dump();
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t pid[3];
int i=0;
std::cout << "create thread....... " << std::endl;
for(i=0; i<3; i++){
std::cout << "create thread, one by one " << std::endl;
if(pthread_create(&pid[i],NULL,start_thread,NULL) != 0){
std::cout << "create thread err " << std::endl;
return -1;
}
}
sleep(5);
return 0;
}
c/c++创建线程,并在自线程中获取自身id
最新推荐文章于 2024-06-27 16:10:26 发布