thread in c
Sure! Here is a response without any personal words or sentences:
To create a thread in C, you can use the pthread_create
function from the pthread.h
header file. Here is an example of how to create a simple thread:
#include <stdio.h>
#include <pthread.h>
void thread_function(void arg) {
printf("This is a new thread!\n");
return NULL;
}
int main() {
pthread_t thread_id;
pthread_create(&thread_id, NULL, thread_function, NULL);
printf("This is the main thread!\n");
pthread_join(thread_id, NULL);
return 0;
}
In this example, we define a function thread_function
that will be executed by the new thread. Inside this function, we print a message to indicate that it is a new thread. In the main
function, we create a new thread using pthread_create
, passing the address of the thread_function
as the start routine. We also pass NULL
as the argument for the thread function. After creating the thread, we print a message from the main thread. Finally, we use pthread_join
to wait for the thread to finish execution before exiting the program.
I hope this helps! Let me know if you have any further questions.