使用C语言用pthread_create编写一段代码 
时间: 2023-05-18 16:04:03 浏览: 38
可以,以下是使用C语言用pthread_create编写的代码:
#include <stdio.h>
#include <pthread.h>
void *thread_func(void *arg) {
printf("Hello from thread!\n");
return NULL;
}
int main() {
pthread_t thread;
int ret = pthread_create(&thread, NULL, thread_func, NULL);
if (ret != 0) {
printf("Error creating thread!\n");
return 1;
}
printf("Hello from main!\n");
pthread_join(thread, NULL);
return 0;
}
相关问题
使用c语言编写实现哲学家就餐问题
哲学家就餐问题是一个经典的并发问题,以下是使用C语言实现的参考代码:
```C
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#define N 5 // 哲学家数量
pthread_mutex_t chopsticks[N]; // 筷子数组
void *philosopher(void *arg) { // 哲学家函数
int number = *(int *)arg;
int left = number; // 左筷子编号
int right = (number + 1) % N; // 右筷子编号
while (1) {
printf("Philosopher %d is thinking\n", number);
sleep(1); // 思考一段时间
printf("Philosopher %d is hungry\n", number);
pthread_mutex_lock(&chopsticks[left]); // 拿起左筷子
printf("Philosopher %d picks up chopstick %d\n", number, left);
pthread_mutex_lock(&chopsticks[right]); // 拿起右筷子
printf("Philosopher %d picks up chopstick %d\n", number, right);
printf("Philosopher %d is eating\n", number);
sleep(1); // 吃一段时间
pthread_mutex_unlock(&chopsticks[left]); // 放下左筷子
printf("Philosopher %d puts down chopstick %d\n", number, left);
pthread_mutex_unlock(&chopsticks[right]); // 放下右筷子
printf("Philosopher %d puts down chopstick %d\n", number, right);
}
}
int main() {
pthread_t philosophers[N];
int numbers[N];
int i;
for (i = 0; i < N; i++) {
pthread_mutex_init(&chopsticks[i], NULL);
numbers[i] = i;
pthread_create(&philosophers[i], NULL, philosopher, &numbers[i]);
}
for (i = 0; i < N; i++) {
pthread_join(philosophers[i], NULL);
}
return 0;
}
```
在主函数中,我们首先初始化筷子数组,并创建哲学家线程。每个哲学家线程都循环执行思考、拿起筷子、吃饭、放下筷子的操作。注意在拿起和放下筷子时要使用互斥锁保证同一时间只有一个哲学家能够拿起筷子。最后我们等待所有哲学家线程运行结束,程序退出。
vs上写一段c语言编写的串口线程接收例程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <pthread.h>
#define BAUDRATE B9600
#define SERIAL_PORT "/dev/ttyUSB0"
#define BUFFER_SIZE 256
//串口文件描述符
int serial_fd;
//接收缓冲区
char recv_buffer[BUFFER_SIZE];
//线程退出标志
int thread_exit_flag = 0;
//串口接收线程函数
void *serial_recv_thread(void *arg) {
while(!thread_exit_flag) {
int recv_len = read(serial_fd, recv_buffer, BUFFER_SIZE);
if(recv_len > 0) {
printf("Received data: %s", recv_buffer);
memset(recv_buffer, 0, BUFFER_SIZE);
}
}
pthread_exit(NULL);
}
int main() {
//打开串口
serial_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
if(serial_fd < 0) {
perror("open serial port failed");
return -1;
}
//配置串口
struct termios options;
tcgetattr(serial_fd, &options);
cfsetispeed(&options, BAUDRATE);
cfsetospeed(&options, BAUDRATE);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CRTSCTS;
tcsetattr(serial_fd, TCSANOW, &options);
//创建串口接收线程
pthread_t recv_thread;
if(pthread_create(&recv_thread, NULL, serial_recv_thread, NULL)) {
perror("create serial recv thread failed");
return -1;
}
//主线程等待串口接收线程退出
pthread_join(recv_thread, NULL);
//关闭串口
close(serial_fd);
return 0;
}
相关推荐










