mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-04-14 18:30:28 +08:00
P296 18.3 线程存在的问题和临界区
This commit is contained in:
1
ch18/README.md
Normal file
1
ch18/README.md
Normal file
@@ -0,0 +1 @@
|
||||
18.
|
||||
30
ch18/thread1.c
Normal file
30
ch18/thread1.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
void *thread_main(void *arg);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pthread_t t_id;
|
||||
int thread_param = 5;
|
||||
// 请求创建一个线程,从 thread_main 调用开始,在单独的执行流中运行。同时传递参数
|
||||
if (pthread_create(&t_id, NULL, thread_main, (void *)&thread_param) != 0)
|
||||
{
|
||||
puts("pthread_create() error");
|
||||
return -1;
|
||||
}
|
||||
sleep(10); //延迟进程终止时间
|
||||
puts("end of main");
|
||||
return 0;
|
||||
}
|
||||
void *thread_main(void *arg) //传入的参数是 pthread_create 的第四个
|
||||
{
|
||||
int i;
|
||||
int cnt = *((int *)arg);
|
||||
for (int i = 0; i < cnt; i++)
|
||||
{
|
||||
sleep(1);
|
||||
puts("running thread");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
40
ch18/thread2.c
Normal file
40
ch18/thread2.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
void *thread_main(void *arg);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pthread_t t_id;
|
||||
int thread_param = 5;
|
||||
void *thr_ret;
|
||||
// 请求创建一个线程,从 thread_main 调用开始,在单独的执行流中运行。同时传递参数
|
||||
if (pthread_create(&t_id, NULL, thread_main, (void *)&thread_param) != 0)
|
||||
{
|
||||
puts("pthread_create() error");
|
||||
return -1;
|
||||
}
|
||||
//main函数将等待 ID 保存在 t_id 变量中的线程终止
|
||||
if (pthread_join(t_id, &thr_ret) != 0)
|
||||
{
|
||||
puts("pthread_join() error");
|
||||
return -1;
|
||||
}
|
||||
printf("Thread return message : %s \n", (char *)thr_ret);
|
||||
free(thr_ret);
|
||||
return 0;
|
||||
}
|
||||
void *thread_main(void *arg) //传入的参数是 pthread_create 的第四个
|
||||
{
|
||||
int i;
|
||||
int cnt = *((int *)arg);
|
||||
char *msg = (char *)malloc(sizeof(char) * 50);
|
||||
strcpy(msg, "Hello,I'am thread~ \n");
|
||||
for (int i = 0; i < cnt; i++)
|
||||
{
|
||||
sleep(1);
|
||||
puts("running thread");
|
||||
}
|
||||
return (void *)msg; //返回值是 thread_main 函数中内部动态分配的内存空间地址值
|
||||
}
|
||||
30
ch18/thread3.c
Normal file
30
ch18/thread3.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
void *thread_summation(void *arg);
|
||||
int sum = 0;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pthread_t id_t1, id_t2;
|
||||
int range1[] = {1, 5};
|
||||
int range2[] = {6, 10};
|
||||
|
||||
pthread_create(&id_t1, NULL, thread_summation, (void *)range1);
|
||||
pthread_create(&id_t2, NULL, thread_summation, (void *)range2);
|
||||
|
||||
pthread_join(id_t1, NULL);
|
||||
pthread_join(id_t2, NULL);
|
||||
printf("result: %d \n", sum);
|
||||
return 0;
|
||||
}
|
||||
void *thread_summation(void *arg)
|
||||
{
|
||||
int start = ((int *)arg)[0];
|
||||
int end = ((int *)arg)[1];
|
||||
while (start <= end)
|
||||
{
|
||||
sum += start;
|
||||
start++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
45
ch18/thread4.c
Normal file
45
ch18/thread4.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#define NUM_THREAD 100
|
||||
|
||||
void *thread_inc(void *arg);
|
||||
void *thread_des(void *arg);
|
||||
long long num = 0;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pthread_t thread_id[NUM_THREAD];
|
||||
int i;
|
||||
|
||||
printf("sizeof long long: %d \n", sizeof(long long));
|
||||
for (i = 0; i < NUM_THREAD; i++)
|
||||
{
|
||||
if (i % 2)
|
||||
pthread_create(&(thread_id[i]), NULL, thread_inc, NULL);
|
||||
else
|
||||
pthread_create(&(thread_id[i]), NULL, thread_des, NULL);
|
||||
}
|
||||
|
||||
for (i = 0; i < NUM_THREAD; i++)
|
||||
pthread_join(thread_id[i], NULL);
|
||||
|
||||
printf("result: %lld \n", num);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *thread_inc(void *arg)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 50000000; i++)
|
||||
num += 1;
|
||||
return NULL;
|
||||
}
|
||||
void *thread_des(void *arg)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 50000000; i++)
|
||||
num -= 1;
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user