#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string. h> #include <pthread. h> char message[50]="Hello World"; void *thread_function(void *arg) { printf("=-=CHILD run and want to sleep(5). message NOW is %s \n", (char *Jarg); sleep(5); strcpy(message, "HELLO FATHER!"]; fatheV pthread_ exit("=== Thank you for your CPU time!"); } int main() { int res; pthread_t threadCH void thread_ resulty res=pthread create(&threadCH, NULL, thread_function, (void' "]message); 仓 if (res!=0) { perror("Thread creation failed!"); exit(EXIT_FAILURE); } printf("MAIN THREAD is Waiting for thread to finish by JOIN. . . \n"]; res=pthread_join(threadCH, &thread_ result); if(res!=0) { perror("Thread join failed! \n"); exit(EXIT_FAILURE); } printf("CHILD THREAD returned %s \n", (char*)thread_result); printf("Message NOW is %s \n", message); exit(EXIT_FAILURE); }
时间: 2024-04-28 21:26:57 浏览: 189
这段代码是一个使用pthread库创建线程的例子,主函数中调用pthread_create函数创建了一个子线程,并将一个字符串指针作为参数传递给子线程。子线程执行thread_function函数,在打印一条信息后,调用了sleep函数暂停了5秒钟,然后将message字符串的值改为"HELLO FATHER!",最后调用pthread_exit函数结束了子线程的执行。
在主函数中,使用pthread_join函数等待子线程结束,并获取子线程的返回值。最后打印出子线程的返回值和message字符串的值。
相关问题
修改这段代码,让它能够在gcc编译器中顺利的运行#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> char message[50] = "Hello World"; void *thread_function(void *arg) { printf("===CHILD run and want to sleep(5). message NOW is %s\n", (char *)arg); sleep(5); strcpy(message, "HELLO FATHER!"); pthread_exit("===Thank you for your CPU time!"); } int main() { int res; pthread_t threadCH; void *thread_result; res = pthread_create(&threadCH, NULL, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed!"); exit(EXIT_FAILURE); } printf("MAIN THREAD is Waiting for thread to finish by JOIN...\n"); res = pthread_join(threadCH, &thread_result); if (res != 0) { perror("Thread join failed!\n"); exit(EXIT_FAILURE); } printf("CHILD THREAD returned %s\n", (char *)thread_result); printf("Message NOW is %s\n", message); exit(EXIT_FAILURE); }//main
这段代码在我的gcc编译器中可以顺利运行,这里我提供一些修改的建议:
1. 在调用pthread_exit()函数时,需要传递一个void*类型的指针作为线程的返回值,而不是直接传递一个字符串。因此,需要定义一个指针变量,将字符串的地址赋值给它,然后再将指针变量作为参数传递给pthread_exit()函数。
2. 在printf()函数中,需要将换行符“\n”放在双引号内,否则会出现编译错误。
下面是修改后的代码:
```
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
char message[50] = "Hello World";
void *thread_function(void *arg) {
printf("===CHILD run and want to sleep(5). message NOW is %s\n", (char *)arg);
sleep(5);
strcpy(message, "HELLO FATHER!");
char *ret = "===Thank you for your CPU time!";
pthread_exit(ret);
}
int main() {
int res;
pthread_t threadCH;
void *thread_result;
res = pthread_create(&threadCH, NULL, thread_function, (void *)message);
if (res != 0) {
perror("Thread creation failed!");
exit(EXIT_FAILURE);
}
printf("MAIN THREAD is Waiting for thread to finish by JOIN...\n");
res = pthread_join(threadCH, &thread_result);
if (res != 0) {
perror("Thread join failed!\n");
exit(EXIT_FAILURE);
}
printf("CHILD THREAD returned %s\n", (char *)thread_result);
printf("Message NOW is %s\n", message);
exit(EXIT_SUCCESS);
}
```
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <pthread.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <ctype.h> #include <sys/pps.h> #include <sys/types.h> #include "camera.h" #include <encoder.h> #include "ppsFunc.h" #include "timeDiff.h"
这是一段C语言的代码,使用了一些系统库和自定义头文件。其中包括了网络编程相关的库,如sys/socket.h和netinet/in.h,以及多线程相关的库pthread.h。另外还有一些自定义的头文件,如camera.h和encoder.h,可能是该程序所需的一些外部依赖。该程序可能是一个视频流的采集和编码程序,同时还使用了PPS和时间差等功能。
阅读全文
相关推荐
















