Skip to content

Commit

Permalink
[cpp] Update pthread learning
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoujingfighting committed Sep 10, 2023
1 parent 52e57a8 commit e29b634
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 语言相关/cpp基础入门/pthread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <cstdlib>
#include <iostream>
#include <pthread.h>

using namespace std;

#define NUM_THREAD 5

void *printHello(void *threadid) {
long tid;
tid = (long)threadid;
cout << "Hello World! Thread ID, " << tid << endl;
pthread_exit(NULL);
}

int main() {
pthread_t threads[NUM_THREAD];
int rc;
int i;
for (i = 0; i < NUM_THREAD; i++) {
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, printHello, (void *)i);

if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
return 0;
}

0 comments on commit e29b634

Please sign in to comment.