参考视频
https://www.bilibili.com/video/BV1kt411z7ND/?spm_id_from=333.1007.top_right_bar_window_history.content.click&vd_source=6c5d3cd50fc7fa8732bdfb760a055839
pthread安装课程:
1
https://blog.csdn.net/qq_33194301/article/details/104879626?spm=1001.2101.3001.6661.1&depth_1-utm_relevant_index=1
1
https://blog.csdn.net/cuicui_ruirui/article/details/106782911?ops_request_misc=&request_id=&biz_id=102&spm=1018.2226.3001.4187
1 创建线程
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#pragma comment(lib, "pthreadVC2.lib")
void* myfunc(void* args) {
int i;
for (int i = 0; i <= 50; i++) {
printf("%d\n", i);
}
return NULL;
}
int main() {
pthread_t th1;
pthread_t th2;
pthread_create(&th1, NULL, myfunc, NULL);
pthread_create(&th2, NULL, myfunc, NULL);
pthread_join(th1, NULL);
pthread_join(th2 ,NULL);
return 0;
}
2 创建线程
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#pragma comment(lib, "pthreadVC2.lib")
void* myfunc(void* args) {
int i;
char* name = (char*)args;
for (int i = 0; i <= 50; i++) {
printf("%s: %d\n",name, i);
}
return NULL;
}
int main() {
pthread_t th1;
pthread_t th2;
pthread_create(&th1, NULL, myfunc, "th1"); //第四个参数是,传入调用函数的参数
pthread_create(&th2, NULL, myfunc, "th2");
pthread_join(th1, NULL);
pthread_join(th2, NULL);
return 0;
}
2:多线程求和
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#pragma comment(lib, "pthreadVC2.lib")
typedef struct {
int first;
int last;
int result;
}MY_ARGS;
int arr[5000];
int s1 = 0;
int s2 = 0;
void* myfunc(void* args) {
int i;
int s = 0;
MY_ARGS* my_args = (MY_ARGS*)args;
int fisrt = my_args->first;
int last = my_args->last;
for (int i = fisrt; i <= last; i++) {
s = s + arr[i];
}
my_args->result = s;
return NULL;
}
int main() {
int i;
for (int i = 0; i < 5000; i++)
{
arr[i] = rand() % 50;
}
pthread_t th1;
pthread_t th2;
MY_ARGS args1 = { 0,2500,0 };
MY_ARGS args2 = { 2500,5000,0 };
pthread_create(&th1, NULL, myfunc, &args1); //第四个参数是,传入调用函数的参数
pthread_create(&th2, NULL, myfunc, &args2);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
int s1 = args1.result;
int s2 = args2.result;
printf("s1=%d\n", s1);
printf("s2=%d\n", s2);
printf("s1 + s2= %d\n", s1);
return 0;
}
3:第三讲 线程同步的问题
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#pragma comment(lib, "pthreadVC2.lib")
#define MAX_SIZE 50000000
int s = 0;
void* myfunc(void* args) {
int i = 0;
for (int i = 0; i < 100000000; i++){
s++;
}
return 0;
}
int main() {
pthread_t th1;
pthread_t th2;
pthread_create(&th1, NULL, myfunc, NULL);
pthread_create(&th2, NULL, myfunc, NULL);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
printf("s= %d\n", s);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#pragma comment(lib, "pthreadVC2.lib")
#define MAX_SIZE 50000000
int s = 0;
pthread_mutex_t lock;
void* myfunc(void* args) {
int i = 0;
for (int i = 0; i < 100000000; i++){
pthread_mutex_lock(&lock);
s++;
pthread_mutex_unlock(&lock);
}
return 0;
}
int main() {
pthread_t th1;
pthread_t th2;
pthread_mutex_init(&lock, NULL);
pthread_create(&th1, NULL, myfunc, NULL);
pthread_create(&th2, NULL, myfunc, NULL);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
printf("s= %d\n", s);
return 0;
}
加锁过后 结果正确
example5
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#pragma comment(lib, "pthreadVC2.lib")
#define MAX_SIZE 50000000
typedef struct {
int first;
int last;
int id;
} MY_ARGS;
int* arr;
int result[2];
void* myfun(void* args) {
int i;
MY_ARGS* my_args = (MY_ARGS*)args;
int first = my_args->first;
int last = my_args->last;
int id = my_args->id;
for (int i = first; i < last; i++)
{
result[id] = result[id] + arr[i];
}
return NULL;
}
int main() {
int i;
arr = malloc(sizeof(int)*MAX_SIZE);
for (i = 0; i < MAX_SIZE; i++) {
arr[i] = rand() % 5;
}
result[0] = 0;
result[1] = 1;
pthread_t th1;
pthread_t th2;
int mid = MAX_SIZE / 2;
MY_ARGS args1 = { 0,mid,0};
MY_ARGS args2 = { mid,MAX_SIZE,1 };
pthread_create(&th1, NULL, myfun, &args1);
pthread_create(&th2, NULL, myfun, &args2);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
printf("result0 is:%d \n", result[0]);
printf("result1 is:%d \n", result[1]);
printf("sum of result is: %d", (result[0] + result[1]));
return 0;
}