我正在尝试学习 C 编程和多线程。我开始编写一些基本的东西 [如下所示],但我陷入了困境。有人可以帮帮我吗?
程序.c
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#define NUM_THREADS 4
void *main_thread(void *threadID) {
long tid;
tid = (long)threadID;
printf("main thread #%ld!\n", tid);
pthread_exit(NULL);
}
void *first_thread(void *threadID) {
long tid;
tid = (long)threadID;
printf("first thread #%ld!\n", tid);
pthread_exit(NULL);
}
void *second_thread(void *threadID) {
long tid;
tid = (long)threadID;
printf("second thread #%ld!\n", tid);
pthread_exit(NULL);
}
void *last_thread(void *threadID) {
long tid;
tid = (long)threadID;
printf("last thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main () {
pthread_t threads[NUM_THREADS];
int rc;
long t;
for (t=0; t < NUM_THREADS; t++) {
printf("In main Function creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, first_thread, (void *)t);
if (rc) {
printf("ERROR; Return code from pthread_create is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}
当我发现新事物时,我将不断更新上面的代码*嘿伙计们。我没有正确编译它,但现在我想通了。gcc -pthread -o main main.c