我正在学习 C 线程概念,并编写了下面的简单代码。现在,当我编译并运行它时,会出现随机行为,例如意外打印。
#include <pthread.h>
#include <stdio.h>
void * func_threadName(void * i) {
int *x=(int *)i;
printf("I'm thread : %d\n",*x);
return NULL;
}
main()
{
int iter;
printf("testing multithreading....\n");
pthread_t thread_arr[3];
for (iter=0;iter<3;iter++)
{
pthread_create(&thread_arr[iter],NULL,func_threadName,&iter);
}
for (iter=0;iter<3;iter++)
{
pthread_join(thread_arr[iter],NULL);
}
}
Run Code Online (Sandbox Code Playgroud)
它打印出不可预测的像:
diwakar@diwakar-VPCEH25EN:~/Documents/my_C_codes$ ./thread_test.o
testing multithreading....
I'm thread : 0
I'm thread : 0
I'm thread : 0
diwakar@diwakar-VPCEH25EN:~/Documents/my_C_codes$ ./thread_test.o
testing multithreading....
I'm thread : 0
I'm thread : 2
I'm thread : 1
diwakar@diwakar-VPCEH25EN:~/Documents/my_C_codes$ ./thread_test.o
testing multithreading....
I'm …Run Code Online (Sandbox Code Playgroud) 我认为 pthread_join 应该总是返回一个值,然后允许主线程在此之后处理代码。根据我过去的经验,这会奏效。但现在我被它困住了。不知何故,它只是不返回并阻塞主线程。或者它可能是执行任务的主线程。我不知道为什么。在下面的代码中,在终止客户端之前,我无法访问“Thread created2”。任何的想法?
int main(int argc, char *argv[]) {
int sockfd, port; /* listen on sock_fd, new connection on new_fd */
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* connector's address information */
socklen_t sin_size;
if(signal(SIGINT, sigintEvent) == SIG_ERR)
printf("can't catch SIGINT!");
/* generate the socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (argc > 1) {
port = atoi(argv[1]);
} else {
port = MYPORT;
}
/* …Run Code Online (Sandbox Code Playgroud) 文件 'hello' 的内容是hello.
$ od -tx1 -tc hello
0000000 68 65 6c 6c 6f 0a
h e l l o \n
0000006
Run Code Online (Sandbox Code Playgroud)
下面是我对文件“hello”进行一些更改的代码。
static void *task();
int main(void)
{
int *p;
pthread_t Thread;
int fd = open("hello", O_RDWR);
if (fd < 0) {
perror("open hello");
exit(1);
}
p = mmap(NULL, 6, PROT_WRITE, MAP_PRIVATE, fd, 0);
if (p == MAP_FAILED) {
perror("mmap");
exit(1);
}
close(fd);
pthread_create(&Thread, NULL, &task, p)
printf("Help");
pthread_join(Thread, 0);
munmap(p, 6);
return 0;
}
static void …Run Code Online (Sandbox Code Playgroud) 需要一个创建 5 个 pthread 的解决方案。每个 pthread 执行一个函数,该函数涉及循环 10 次。在循环的每次迭代中,线程将 int 从 0 增加到 0.9*MAX_INT,然后打印迭代编号。确保 5 个线程中的每一个都在它们可以开始第 (i+1) 次迭代之前完成循环的第 i 次迭代(即所有线程在每次迭代结束时同步/会合)。我需要使用使用 POSIX 信号量实现的两阶段屏障来强制执行同步约束
我写了以下代码我正确吗?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int thread_count;
void* MyThread(void* rank);
int main()
{
long thread;
pthread_t* thread_handles;
thread_count = 5;
thread_handles = malloc (thread_count*sizeof(pthread_t));
for (thread = 0; thread < thread_count; thread++)
pthread_create(&thread_handles[thread],NULL,MyThread,(void*) thread);
for (thread = 0; thread < thread_count; thread++)
pthread_join(thread_handles[thread], NULL);
free(thread_handles);
return 0;
}
void* Hello(void* rank)
{
long my_rank = (long) …Run Code Online (Sandbox Code Playgroud) 我有两个函数,比如 foo() 和 bar(),我希望它们互斥,即在 bar() 运行时完全阻止 foo() 的运行或在 foo() 运行时完全阻止 bar() 的运行为线程安全而运行。
但是,我可能会在 foo() 内部调用 bar(),也就是说,当 foo() 在 foo() 内部调用 bar() 时,让 bar() 运行,但其他线程不会调用 bar()。
是否可以?如果是,你能提供大致的想法吗?
尝试在C中使用一个或多个互斥体,很容易使两个函数互斥,但是我不能在foo()中调用bar(),它们会进入死锁。
我不能在 foo() 内部调用 bar() 之前就解锁互斥锁,因为我不能保证下一个 bar() 运行是在 foo() 内部调用的那个。
我正在寻找一种解决方案,即 foo 将阻止 bar,如果它们在不同的线程中运行,bar 将阻止 foo。但是当 foo 在其主体内(同一线程)调用 bar 时,让 bar 运行。
谢谢
多线程初学者在这里。恰好在第5次迭代(即执行pthread_join(threadID [4],NULL)时),我的程序由于分段错误而失败。
我正在创建多个线程以从计数器变量中加减1以研究竞争条件。一切正常,直到我尝试5个线程或更多。恰好在pthread_join(threadID [4],NULL)的最后一次迭代中,它失败了,我无法确定原因。我确定问题出在哪里,因为我使用printf语句查看失败之前到达的位置。
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <time.h>
int opt_threads;
int opt_iterations;
long nThreads;
long nIterations;
int opt_yield;
long long counter;
void add(long long *pointer, long long value) {
long long sum = *pointer + value;
if (opt_yield)
sched_yield();
*pointer = sum;
}
void *thread_worker(void * arg) {
long i;
for (i=0; i<nIterations; i++) {
add(&counter, 1);
add(&counter, -1);
}
return arg; …Run Code Online (Sandbox Code Playgroud) 在Pthreads手册页中,提到了
调用getpid(2)在每个线程中返回不同的值
在LinuxThreads部分中。
我创建了两个线程,并在其中打印了PID。但是,两者的PID都是相同的。
int main ()
{
//pid_t pid;
pthread_t tid[2];
{
printf("In main, PID : %d, PPID : %d\n", getpid(), getppid());
pthread_create(&(tid[0]), NULL, &(f),NULL);
pthread_create(&(tid[1]), NULL, &(g),NULL);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
}
return 0;
}
void *g()
{
printf("My PID in G : %d, PPID : %d\n", getpid(), getppid());
}
void* f()
{
printf("My PID in F : %d, PPID : %d\n", getpid(), getppid());
}
Run Code Online (Sandbox Code Playgroud)
以下是我得到的输出,
总的来说,PID:5219,PPID:5214
我的PID在F中:5219,PPID:5214
我的PID在G中:5219,PPID:5214
我需要知道我是否在这里误解了什么。
所以我有一个在 c 中使用 pthreads 的电梯程序的缩小版本。每个线程都是一个单独的调用函数request()。我不知道如何知道哪个电梯(1、2 或 3)是线程正在使用函数请求。在请求函数中,我需要打印当时哪个线程使用了它。对不起,如果我的解释不完全有意义。
void* request(void* abc)
{
int ii;
for(ii = 0; ii < 8; ii++)
{
sleep(1);
printf("REQUEST FROM LIFT COMPLETED\n");
}
}
int main()
{
pthread_t lift1;
pthread_t lift2;
pthread_t lift3;
pthread_create(&lift1, NULL, request, NULL);
pthread_create(&lift2, NULL, request, NULL);
pthread_create(&lift3, NULL, request, NULL);
pthread_join(lift1, NULL);
pthread_join(lift1, NULL);
pthread_join(lift1, NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 大约 50% 的时间,对我的线程池的测试不会引发任何异常并且似乎按预期工作。然而,另外 50% 的时间它会抛出一个std::bad_function_call或double free or corruption (!prev). 我究竟做错了什么?
#include <thread>
#include <iostream>
#include <atomic>
#include <any>
#include <stack>
#include <mutex>
#include <algorithm>
class reusable_thread {
std::thread thread;
std::atomic_bool kill = false;
std::stack<std::function<void(void)>> function_stack;
std::stack<std::function<void(void)>> pending_function_stack;
std::mutex stack_mutex;
std::atomic_size_t num_jobs = 0;
/** Seperate containers for functions and pending_function_stack, so that add_job does not have to be locking **/
inline void transfer_functions() {
std::lock_guard lock(stack_mutex);
while(pending_function_stack.size() != 0) {
function_stack.push(pending_function_stack.top());
pending_function_stack.pop();
}
}
public:
/** …Run Code Online (Sandbox Code Playgroud) 我在 c 中创建了两个线程。我想由每个线程执行两个单独的函数。如何使一个特定的线程首先被执行。
#include <stdio.h>
#include <pthread.h>
void* function_1(void* p)
{
// statements
}
void* function_2(void* p)
{
// statements
}
int main(void)
{
pthread_t id1;
pthread_t id2;
pthread_create(&id1, NULL, function_1, NULL);
pthread_create(&id2, NULL, function_2, NULL);
pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud)
程序启动时如何使function_1在function_2之前执行?
pthreads ×10
c ×9
linux ×3
c++ ×1
c++17 ×1
mmap ×1
pthread-join ×1
semaphore ×1
sockets ×1
threadpool ×1