C语言学习之:并发编程(多线程编程)资源整理 pthread

  • 先挖个坑,改天再填充细节。。。。。

如何解决 VS 中的 pthread.h 头文件问题

https://blog.csdn.net/qq_35292447/article/details/103541936

线程的创建

代码范例

/*************************************
Demo for pthread commands
compile: gcc threadX.c -o threadX -lpthread
***************************************/
#include <pthread.h>
#include <stdio.h>
void* say_hello(void* param); /* the work_function */
int main(int args, char** argv) {
	pthread_t tid; /* thread identifier */
	/* create the thread */
	pthread_create(&tid, NULL, say_hello, NULL);
	/* wait for thread to exit */
	pthread_join(tid, NULL);
	printf("Hello from first thread\n");
	return 0;
}
void* say_hello(void* param) {
	printf("Hello from second thread\n");
	return NULL;
}

参考资源

https://zhuanlan.zhihu.com/p/97418361

互斥锁

代码范例

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ITERATIONS 1000000
void* runner(void* param); /* thread doing the work */
int count = 0;
pthread_mutex_t lock;
int main(int argc, char** argv) {
	pthread_t tid1, tid2;
	if (pthread_mutex_init(&lock, NULL) != 0) {
		printf("mutex init failed\n");
		exit(1);
	}
	if (pthread_create(&tid1, NULL, runner, NULL)) {
		printf("Error creating thread 1\n");
		exit(1);
	}
	if (pthread_create(&tid2, NULL, runner, NULL)) {
		printf("Error creating thread 2\n");
		exit(1);
	}
	/* wait for the threads to finish */
	if (pthread_join(tid1, NULL)) {
		printf("Error joining thread\n");
		exit(1);
	}
	if (pthread_join(tid2, NULL)) {
		printf("Error joining thread\n");
		exit(1);
	}
	if (count != 2 * ITERATIONS)
		printf("** ERROR ** count is [%d], should be %d\n", count, 2 * ITERATIONS);
	else
		printf("OK! count is [%d]\n", count);
	pthread_exit(NULL);
	pthread_mutex_destroy(&lock);
	return 0;
}

/* thread doing the work */
void* runner(void* param) {
	int i, temp;
	for (i = 0; i < ITERATIONS; i++) {
		temp = count; /* copy the global count locally */
		temp = temp + 1; /* increment the local copy */
		count = temp; /* store the local value into the global count */
	}
	return NULL;
}

参考资源

https://zhuanlan.zhihu.com/p/97512154

信号量

https://zhuanlan.zhihu.com/p/98717838

线程池

https://zhuanlan.zhihu.com/p/141615042

进程的创建

代码范例

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char** argv) {
	pid_t root = getpid();
	// when forking, program does not start again since memory/register values are exactly the same
	// i.e. instruction pointer is at the same line too. So fork() wont execute again.
	pid_t pid = fork();
	printf("from %d forking into %d\n", root, pid);
	sleep(20);
	// watch 2 different PIDs spawn 2 more child processes.
	pid_t mypid = getpid();
	pid = fork();
	printf("from %d forking into %d\n", mypid, pid);
	sleep(20);
	if (getpid() == root) {
		sleep(20);
		printf("root exiting\n");
	}
	else {
		printf("Child -- PID %d exiting\n", getpid());
	}
	return 0;
}
#include<unistd.h>
int main(int argc, char** argv) {
	return execv("/usr/bin/ls", argv);
}

建立管道

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
	int fd[2], nbytes;
	pid_t childpid;
	char string[] = "Hello, world!\n";
	char readbuffer[80];
	pipe(fd);
	if ((childpid = fork()) == -1) {
		perror("fork");
		exit(1);
	}
	if (childpid == 0) {
		/* Child process closes up input side of pipe */
		close(fd[0]);
		/* Send "string" through the output side of pipe */
		write(fd[1], string, (strlen(string) + 1));
		exit(0);
	}
	else {
		/* Parent process closes up output side of pipe */
		close(fd[1]);
		/* Read in a string from the pipe */
		nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
		printf("Received string: %s", readbuffer);
	}
	return (0);
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暖仔会飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值