The first LINUX multi-thread C concurrent program to be copied must use the pthread. h header file ~ This is not complete. Remember to use-lpthread to link libpthread. A during compilation.
The principle is very simple. Use sleep (1) to print once every second. The two threads print "hello" and "world! respectively! \ N "Although I slept for one second, the two prints were not even cross.
There are a lot of existing thread interfaces, pthread_create, pthread_join (), and pthread_t. In addition, there is a sleep ()
/* Hello, world -- single thread * // remember-lpthread link libpthread. A # include <stdio. h> # include <pthread. h> # define num 6int main () {void print_msg (void *); pthread_t T1, T2; pthread_create (& T1, null, print_msg, (void *) "Hello, "); pthread_create (& T2, null, print_msg, (void *)" world! \ N "); pthread_join (T1, null); pthread_join (t2, null);} void print_msg (void * m) {char * CP = (char *) m; // There is a conversion process int I; for (I = 0; I <num; I ++) {// print printf ("% s", m) every second ); fflush (stdout); // clear sleep (1); // sleep for one second}/** function prototype: * int pthread_create (pthread_t * thread, pthread_attr_t * ATTR, void * (* func) (void *), void * Arg); * parameter: pointer of thread to pthread_t type variable * ATTR to pointer of pthread_attr_t type variable, or null * func points to the pointer of the function running in the new thread * Arg passed * Errcode Error * is returned successfully when the * return value is 0 in the func parameter *. We can use the pthread_join function to wait for a process to end. * Function prototype: int pthread_join (pthread_t thread, void ** retval); * parameter: The process waiting for thread * retval points to the variable returned by a storage thread * return value: 0 returns * error code *. The above two functions are included in the header file pthread. h. */
Let's look at the second example.
/* Thread_example.c: C multiple thread programming in Linux **/# include <pthread. h> # include <stdio. h> # include <sys/time. h> # include <string. h> # define Max 10pthread_t thread [2]; // create an array of threads... Pthread_mutex_t mut; // from the translation perspective, this is a semaphore int number = 0, I; void * thread1 () {printf ("thread1: I'm thread 1 \ n "); for (I = 0; I <Max; I ++) {printf ("thread1: Number = % d \ n", number); pthread_mutex_lock (& MUT ); number ++; pthread_mutex_unlock (& MUT); sleep (2);} printf ("thread1: is the main function waiting for me to complete the task? \ N "); pthread_exit (null);} void * thread2 () {printf (" thread2: I'm thread 2 \ n "); for (I = 0; I <Max; I ++) {printf ("thread2: Number = % d \ n", number); pthread_mutex_lock (& MUT); Number ++; pthread_mutex_unlock (& MUT); sleep (3);} printf ("thread2: is the main function waiting for me to complete the task? \ N "); pthread_exit (null);} void thread_create (void) {int temp; memset (& Thread, 0, sizeof (thread )); // comment1/* Create thread */If (temp = pthread_create (& Thread [0], null, thread1, null ))! = 0) // comment2 printf ("thread 1 creation failed! \ N "); else printf (" thread 1 created \ n "); If (temp = pthread_create (& Thread [1], null, thread2, null ))! = 0) // comment3 printf ("thread 2 creation failed"); else printf ("thread 2 created \ n");} void thread_wait (void) {/* Wait for the thread to end */If (thread [0]! = 0) {// comment4 pthread_join (thread [0], null); printf ("thread 1 has ended \ n");} If (thread [1]! = 0) {// comment5 pthread_join (thread [1], null); printf ("thread 2 has ended \ n") ;}} int main () {/* use the default attribute to initialize the mutex lock */pthread_mutex_init (& Mut, null); printf ("I am the main function, I am creating a thread, haha \ n "); thread_create (); printf ("I am the main function, I am waiting for the thread to complete the task, ah, huh \ n"); thread_wait (); Return 0 ;}