現在,這只是一個小測試,並且是學校作業的一部分。在我的代碼中,printf至少不會讓我看到它。這是線程無法運行的結果嗎?打印線在線程之外工作。感謝您的任何幫助。C printf不在線內打印?
我是新的線程在c。
#include<stdio.h>
#include<pthread.h>
#include<string.h>
#include<stdlib.h>
void *threadServer(void *arg)
{
printf("This is the file Name: %s\n", arg);
pthread_exit(0);
}
int main(int argc, char* argv[]){
int i=1;
while(argv[i]!=NULL){
pthread_t thread;
pthread_create(&thread, NULL, threadServer,argv[i]);
i++;
}
您需要啓動線程,並等待它完成,否則在打印之前該程序可能會結束:因此,用於實驗的緣故,代碼可以做如下修改。 – Carcigenicate
您在此粘貼的代碼是否完整?如果是這樣,它就會丟失'pthread_join'調用,以等待線程完成。否則,您的主程序可能在線程有機會運行之前退出。 –
這是完整的好吧知道我錯過了一些東西。 – TheMangaStand