一个简单的C++线程示例
时间: 2023-05-11 17:06:20 浏览: 90
以下是一个简单的C线程示例:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function(void *ptr);
int main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
iret1 = pthread_create(&thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create(&thread2, NULL, print_message_function, (void*) message2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Thread 1 returns: %d\n", iret1);
printf("Thread 2 returns: %d\n", iret2);
return 0;
}
void *print_message_function(void *ptr)
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
阅读全文