1.创建线程
// pthread.c
#include<pthread.h>
pthread.c
int pthread_create(pthread_t *restrict thread,const pthread_attr_t *restrict attr,void *(*start_routine)(void*), void *restrict arg);
// 储存线程号,NULL,要执行函数的指针,要执行函数的参数
#################################################################
// main.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
pthread_t ntid;
void printids(const char *s){
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,\
(unsigned int)tid, (unsigned int)tid);
while(1);
}
void *thr_fn(void *arg){
printids(arg);
return NULL;
}
int main(void){
int err;
err = pthread_create(&ntid, NULL, thr_fn, "new thread: ");
if (err != 0) {
fprintf(stderr, "can't create thread: %s\n",
strerror(err));
exit(1);
}
printids("main thread:");
while(1);
return 0;
}
####################################################################
gcc -c -fPIC pthread.c
ar -rc libpthread.a pthread.o
gcc main.c -lpthread -o main
2.终止线程
// pthread.c
#include <pthread.h>
int pthread_join(pthread_t thread, void **value_ptr);
#######################################################################
//main.c
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
void *thr_fn1(void *arg){
printf("thread 1 returning\n");
return (void *)1;
}
void *thr_fn2(void *arg){
printf("thread 2 exiting\n");
pthread_exit((void *)2);
}
void *thr_fn3(void *arg){
while(1) {
printf("thread 3 writing\n");
sleep(1);
}
}
int main(void){
pthread_t tid;
void *tret;
pthread_create(&tid, NULL, thr_fn1, NULL);
pthread_join(tid, &tret);
//线程终止,其它线程可以调用pthread_join获取指向这个值的指针
printf("thread 1 exit code %d\n", (int)tret);
pthread_create(&tid, NULL, thr_fn2, NULL);
pthread_join(tid, &tret);
//线程终止,其它线程可以调用pthread_join获取指向这个值的指针
printf("thread 2 exit code %d\n", (int)tret);
pthread_create(&tid, NULL, thr_fn3, NULL);
sleep(3);
pthread_cancel(tid);
//终止同一进程中的另一个线程,返回PTHREAD_CANCELED,值是-1
pthread_join(tid, &tret);
//线程终止,其它线程可以调用pthread_join获取指向这个值的指针
printf("thread 3 exit code %d\n", (int)tret);
return 0;
}
gcc -c -fPIC pthread.c
ar -rc libpthread.a pthread.o
gcc main.c -lpthread -o main
结果:
thread 1 returning
thread 1 exit code 1
thread 2 exiting
thread 2 exit code 2
thread 3 writing
thread 3 writing
thread 3 writing
thread 3 exit code -1
3.进程锁
// pthread.c
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
################################################################
// main.py
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NLOOP 5000
int counter;
/* incremented by threads */
pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER; // 锁标记
void *doit(void *);
int main(int argc, char **argv){
pthread_t tidA, tidB;
pthread_create(&tidA, NULL, doit, NULL);
pthread_create(&tidB, NULL, doit, NULL);
pthread_join(tidA, NULL);
pthread_join(tidB, NULL);
return 0;
}
void *doit(void *vptr){
int i, val;
for (i = 0; i < NLOOP; i++) {
pthread_mutex_lock(&counter_mutex); // 锁住,原子操作开始
val = counter;
printf("%x: %d\n", (unsigned int)pthread_self(),val + 1);
counter = val + 1;
pthread_mutex_unlock(&counter_mutex); // 解锁,原子操作结束
}
return NULL;
}
```shell
gcc -c -fPIC pthread.c
ar -rc libpthread.a pthread.o
gcc main.c -lpthread -o main
结果
cfa84700: 0
cfa84700: 1
cfa84700: 2
......
......
......
cfa84700: 9998
cfa84700: 9999
cfa84700: 10000
###############################################################
// pthread.c
#include <pthread.h>
int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_signal(pthread_cond_t *cond);
// main.c
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
struct msg {
struct msg *next;
int num;
};
struct msg *head;
pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *consumer(void *p){
struct msg *mp;
for (;;) {
pthread_mutex_lock(&lock);
while (head == NULL)
pthread_cond_wait(&has_product, &lock); // 在一个Condition Variable上阻塞等待
mp = head;
head = mp->next;
pthread_mutex_unlock(&lock);
printf("Consume %d\n", mp->num);
free(mp);
sleep(rand() % 5);
}
}
void *producer(void *p){
struct msg *mp;
for (;;) {
mp = malloc(sizeof(struct msg));
mp->num = rand() % 1000 + 1;
printf("Produce %d\n", mp->num);
pthread_mutex_lock(&lock);
mp->next = head;
head = mp;pthread_mutex_unlock(&lock);
pthread_cond_signal(&has_product); // 唤醒在某个Condition Variable上等待的另一个线程
sleep(rand() % 5);
}
}
int main(int argc, char *argv[]){
pthread_t pid, cid;
srand(time(NULL));
pthread_create(&pid, NULL, producer, NULL);
pthread_create(&cid, NULL, consumer, NULL);
pthread_join(pid, NULL);
pthread_join(cid, NULL);
return 0;
}
gcc -c -fPIC pthread.c
ar -rc libpthread.a pthread.o
gcc main.c -lpthread -o main
######################################################################333
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
int sem_wait(sem_t *sem);
int sem_trywait(sem_t *sem);
int sem_post(sem_t * sem);
int sem_destroy(sem_t * sem);
#include<stdlib.h>
#include<pthread.h>
#include<stdio.h>
#include<semaphore.h>
#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;
void *producer(void *arg){
int p = 0;
while (1) {
sem_wait(&blank_number);
queue[p] = rand() % 1000 + 1;
printf("Produce %d\n", queue[p]);
sem_post(&product_number);
p = (p+1)%NUM;
sleep(rand()%5);
}
}
void *consumer(void *arg){
int c = 0;
while (1) {
sem_wait(&product_number);
// 可以获得资源,使semaphore的值减1,如果调用sem_wait()时semaphore已经是0,则挂起等待
printf("Consume %d\n", queue[c]);
queue[c] = 0;
sem_post(&blank_number);
// sem_post()可以释放资源,使semaphore的值加1,同时唤醒挂起等待的线程
c = (c+1)%NUM;
sleep(rand()%5);
}
}
int main(int argc, char *argv[]){
pthread_t pid, cid;
sem_init(&blank_number, 0, NUM); // 初始化一个信号量,中间0表示该信号量用于同一进程的线程间同步
sem_init(&product_number, 0, 0);
pthread_create(&pid, NULL, producer, NULL);
pthread_create(&cid, NULL, consumer, NULL);
pthread_join(pid, NULL);
pthread_join(cid, NULL);
sem_destroy(&blank_number); // 释放信号量
sem_destroy(&product_number);
return 0;
}