The process is simply to copy a piece of code into multiple copies and have them execute at the same time. Interprocess communication is designed to allow them to run in an orderly fashion
The thread is simply to have multiple functions execute simultaneously, and the threads communicate between them in order for them to run in an orderly way.
When compiling a threaded handler, it warns that the thread function cannot be found
The Pthread library is not the default library of the Linux system, which requires the use of a static library libpthread.a, so that when you create a thread using Pthread_create () and call the Pthread_atfork () function to establish a fork handler, You need to link to the library.
Problem solving:
To add-lpthread parameters to the compilation
GCC Thread.c-o thread-lpthread
THREAD.C for your source files, don't forget to add the header file #include<pthread.h>
http://blog.csdn.net/llqkk/article/details/2854558
Instance 1: Create two threads while executing the same function
* EX7-1.C * *
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
void Print_msg (char *ptr);
int main ()
{
pthread_t Thread1, Thread2;
int i,j;
Char *msg1= "do sth1\n";
Char *msg2= "do sth2\n";
Pthread_create (&thread1,null, (void *) (&print_msg), (void *) MSG1);
Pthread_create (&thread2,null, (void *) (&print_msg), (void *) MSG2);
Sleep (1);
return 0;
}
void Print_msg (char *ptr)
{
int retval;
int id=pthread_self ();
printf ("Thread ID:%x\n", id);
printf ("%s", PTR);
Pthread_exit (&retval);
}
Perform GCC Ex7-1.c-lpthread
./a.out
Instance 2 Create multiple threads to perform different functions
Code from http://www.cnblogs.com/BiffoLee/archive/2011/11/18/2254540.html
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10
pthread_t thread[2];
pthread_mutex_t Mut;
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: The main function is 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: The main function is waiting for me to complete the task." \ n ");
Pthread_exit (NULL);
}
void Thread_create (void)
{
int temp;
memset (&thread, 0, sizeof (thread)); Comment1
Creating Threads
if (temp = Pthread_create (&thread[0], NULL, THREAD1, NULL)!= 0)//comment2
printf ("Thread 1 Create failed!\n");
Else
printf ("Thread 1 is created \ n");
if (temp = Pthread_create (&thread[1], NULL, THREAD2, NULL)!= 0)//comment3
printf ("Thread 2 creation failed");
Else
printf ("Thread 2 is created \ n");
}
void thread_wait (void)
{
Wait for thread to end
if (Thread[0]!=0) {//COMMENT4
Pthread_join (Thread[0],null);
printf ("Thread 1 is terminated \ \");
}
if (Thread[1]!=0) {//COMMENT5
Pthread_join (Thread[1],null);
printf ("Thread 2 is terminated \ \");
}
}
int main ()
{
Initializing mutexes with default properties
Pthread_mutex_init (&mut,null);
printf ("I'm the main function oh, I'm creating threads, huh \ n");
Thread_create ();
printf ("I'm the main function oh, I'm waiting for the thread to finish the task, huh \ n");
Thread_wait ();
return 0;
}
?
Compile:
Gcc-lpthread-o Thread_example lp.c
Example 3: Semaphore control thread Run order
* THREAD_SEM.C * *
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define Thread_number 3
#define Repeat_number 3
#define DELAY_TIME_LEVELS 10.0
Sem_t Sem[thread_number];
void * Thrd_func (void *arg)
{
int thrd_num = (int) arg;
int delay_time = 0;
int count = 0;
Sem_wait (&sem[thrd_num]);
printf ("Thread%d is starting\n", thrd_num);
for (count = 0; count < Repeat_number; count++)
{
Delay_time = (int) (rand () * delay_time_levels/(Rand_max)) + 1;
Sleep (Delay_time);
printf ("\tthread%d:job%d delay =%d\n", Thrd_num, Count, Delay_time);
}
printf ("Thread%d finished\n", thrd_num);
Pthread_exit (NULL);
}
int main (void)
{
pthread_t Thread[thread_number];
int no = 0, res;
void * THRD_RET;
Srand (Time (NULL));
for (no = 0; no < Thread_number; no++)
{
Sem_init (&sem[no], 0, 0);
res = Pthread_create (&thread[no], NULL, Thrd_func, (void*) no);
if (res!= 0)
{
printf ("Create thread%d failed\n", no);
Exit (RES);
}
}
printf ("Create Treads success\n waiting for threads to finish...\n");
Sem_post (&sem[thread_number-1]);
for (no = thread_number-1; no >= 0; no--)
{
res = Pthread_join (Thread[no], &thrd_ret);
if (!res)
{
printf ("Thread%d joined\n", no);
}
Else
{
printf ("Thread%d join failed\n", no);
}
Sem_post (&sem[(no + thread_number-1)% thread_number]);
}
for (no = 0; no < Thread_number; no++)
{
Sem_destroy (&sem[no]);
}
return 0;
}
Instance 4: Use of mutexes
In this program, one thread writes data to the buffer, another thread reads the data, and only one thread can manipulate the buffer at a time
/*ex7-3.c*/
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define FALSE 0
#define TRUE 1
void Readfun ();
void Writefun ();
Char buffer[256];
int buffer_has_item=0;
int retflag=false,i=0;
pthread_mutex_t Mutex;
int main ()
{
void *retval;
pthread_t reader;
Pthread_mutex_init (&mutex,null);
Pthread_create (&reader,null, (void *) &readfun,null);
Writefun ();
Pthread_join (Reader,&retval);
}
void Readfun ()
{
while (1)
{
if (Retflag)
Return
Pthread_mutex_lock (&mutex);
if (buffer_has_item==1)
{
printf ("%s", buffer);
buffer_has_item=0;
}
Pthread_mutex_unlock (&mutex);
}
}
void Writefun ()
{
int i=0;
while (1)
{
if (i==10)
{
Retflag=true;
Return
}
Pthread_mutex_lock (&mutex);
if (buffer_has_item==0)
{
sprintf (Buffer, "This is%d\n", i++);
Buffer_has_item=1;
}
Pthread_mutex_unlock (&mutex);
}
}
Instance 5 condition Variable
Text
/* ex7-4.c/#include <stdio.h> #include <pthread.h> #define BUFFER_SIZE 4 #define OVER ( -1) struct producers
{int buffer[buffer_size];
pthread_mutex_t lock;
int Readpos, Writepos;
pthread_cond_t Notempty;
pthread_cond_t Notfull;
};
void init (struct producers *b) {pthread_mutex_init (&b->lock,null);
Pthread_cond_init (&b->notempty,null);
Pthread_cond_init (&b->notfull,null);
b->readpos=0;
b->writepos=0;
} void put (struct producers *b, int data) {Pthread_mutex_lock (&b->lock);
while ((b->writepos+1)%buffer_size==b->readpos) {pthread_cond_wait (&b->notfull,&b->lock);
} b->buffer[b->writepos]=data;
b->writepos++;
if (b->writepos>=buffer_size) b->writepos=0;
Pthread_cond_signal (&b->notempty);
Pthread_mutex_unlock (&b->lock);
int get (struct producers *b) {int data;
Pthread_mutex_lock (&b->lock); while (B->writepos==b->readpos) {pthread_cond_wait &B->notempty,&b->lock);
} data=b->buffer[b->readpos];
b->readpos++;
if (b->readpos>=buffer_size) b->readpos=0;
Pthread_cond_signal (&b->notfull);
Pthread_mutex_unlock (&b->lock);
return data;
} struct producers buffer;
void *producer (void *data) {int n;
for (n=0;n<10;n++) {printf ("Producer:%d-->\n", N);
Put (&buffer,n);
Put (&buffer,over);
return NULL;
} void *consumer (void *data) {int D;
while (1) {d=get (&buffer);
if (d==over) break;
printf ("Consumer:-->%d\n", D);
return NULL;
int main () {pthread_t THA,THB;
void *retval;
Init (&buffer);
Pthread_create (&tha,null,producer,0);
Pthread_create (&thb,null,consumer,0);
Pthread_join (Tha,&retval);
Pthread_join (Thb,&retval);
return 0;
}