-1
我在学习C编程和多线程。我开始编写一些基本的东西[如下所示],但我陷入了困境。有人能帮我一把吗?C使用pthread.h编程多线程
program.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.c
是不是为任何基本的pthread编程读过任何书籍或文档? – tristan
使用pthread_join等待main中的所有线程。 –
我刚开始阅读一些在线的东西。但它有点混乱。 – NewFile