Tag: pthreads

Linux Pthread库,线程参数

如果我在Linux下使用Pthread库创建线程,我需要使用函数pthread_create ,作为其中一个参数,它需要void * ,所以我可以传递一个指向某个东西,所以我的线程例程可以访问它,但它是否安全做这样的事情 {//some scope int a=5//scope variable pthread_create(&id,NULL,some_function,(void*)a); }//end of scope 在我的日常工作中: void *some_function(void *_arg) { int a=(int)arg; return NULL; } 我想做这样的事情,所以我可以保持变量的值在堆栈上,这样我就可以从我的线程例程中访问它,但我不想为单个变量创建struct或手动分配内存。 我将创建这样的几个线程,所以我想知道在这样的情况下我是否可以通过并且不使用列表或动态数组。

在子进程内创建线程

#include #include int value=0; void *runner(void *param); int main(int argc,char *argv[]) { int pid; pthread_t tid; pthread_attr_t attr; pid=fork(); if(pid==0){ pthread_attr_init(&attr); pthread_create(&tid,&attr,runner,NULL); pthread_join(tid,NULL); printf(“CHILD VALUE=%d”,value); } else if(pid>0){ wait(NULL); printf(“PARENT VALUE=%d”,value); } } void *runner(void *param){ value=5; pthread_exit(0); } 孩子和父母的价值是什么? 孩子和它创建的线程会共享数据吗? 所以输出将是5和0?

pthread_cancel在solaris下不起作用

#include #include #include #include char a[]=”Hello”; void * thread_body(void * param) { while(1) printf(“%s\n”, param); } int main(int argc, char *argv[]) { pthread_t threadHello; int code; pthread_create(&threadHello, NULL, thread_body, a); pthread_cancel(threadHello); pthread_exit(0); } 当我在Solaris 10(SunOS 5.10)下编译并运行它时,它不会停止。 但在Linux下,它按预期工作。

使用线程,我应该如何处理理想情况下按顺序发生的事情?

我有一个图像生成器,可以从线程运行中受益。 我打算使用POSIX线程,并编写了一些基于https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal的模拟代码来测试。 在预期的程序中,当使用GUI时,我希望生成的行逐个从上到下显示(图像生成可能非常慢)。 还应注意,线程中生成的数据不是实际的图像数据。 读取线程数据并将其转换为RGB数据并放入实际的图像缓冲区中。 并且在GUI内,可以在图像生成期间改变线程生成的数据被转换为RGB数据的方式而不停止图像生成。 但是,线程调度程序无法保证线程将以我想要的顺序运行,这不幸地使得线程生成的数据的转换更加棘手,这意味着保持数组保持bool值以指示哪些行的不合需要的解决方案完成。 我应该怎么处理这个? 目前我有一个观察者线程来报告图像何时完成(实际上应该是一个进度条,但我还没有那么远,它使用pthread_cond_wait )。 和几个渲染线程一起做while(next_line()); next_line()执行互斥锁定,并在递增它并解锁互斥锁之前获取img_next_line的值。 它然后呈现该行并执行互斥锁(与第一个不同)以获得line_done对高度的检查,如果完成则发出信号,解锁并在完成时返回0或如果不完成则返回1。

C pthreads在套接字上发送()ing和recv()。 分开工作但不在一起工作。 不会退出

为了消除我对C知识的渴望,在连接到我的家庭网络的两个linux盒子上,我正在写一个send() s和recv() s字符串的骨架telnet(仅用于一些套接字和线程的经验) 。 服务器侦听并且客户端连接并从stdin发送字符串。 我得到了那些工作然后我改变它们来实现pthreads和线程版本工作。 最后,我把两者放在一个程序中,这样连接的任何一端都可以(理论上)发送和接收字符串。 客户端和服务器都使用strstr()来监视”quit”然后退出。 正如这篇文章的标题所暗示的那样,当我把它们放在一起时,组合版本将发送字符串但它不会在它应该的时候退出。 我不确定出了什么问题。 我尝试用gdb逐步完成它,但我对gdb太缺乏经验,无法分辨出发生了什么。 那么,为什么不退出呢? 为了退一步,有没有更好的方法来实现我想要做的事情? 谢谢你的帮助。 clientserver.c #include #include #include #include #include #include #include #include #include #include #include int sockfd = 0, send_running = 1, recv_running = 1, status = 0, acptsockfd = 0; char str_to_send[200], str_rcvd[200]; char *remote_host_addr_str = NULL; struct sockaddr_in remote_addr, listening_addr; void *sender(void […]

测量Mutex和Busy等待的效率

该程序将创建多个线程,其中每个线程使用for循环将共享变量增加10000,在每次迭代中将其递增1。 需要互斥锁和自旋锁(忙等待)版本。 据我所知,互斥版本应该比自旋锁更快。 但我实施的内容给了我一个相反的答案…… 这是互斥锁版本中每个线程的实现: void *incr(void *tid) { int i; for(i = 0; i < 10000; i++) { pthread_mutex_lock(&the_mutex); //Grab the lock sharedVar++; //Increment the shared variable pthread_mutex_unlock(&the_mutex); //Release the lock } pthread_exit(0); } 这是自旋锁版本中的实现: void *incr(void *tid) { int i; for(i = 0; i < 10000; i++) { enter_region((int)tid); //Grab the lock sharedVar++; […]

如何将for循环的索引作为pthread_create的参数传递

我正在使用for循环来创建多个线程并将索引i作为参数传递,如下所示: pthread_t p[count]; for (int i = 0; i < count; i++){ pthread_create(&p[i], NULL, &somefunc, (void*)&i); } 然后我尝试检索i的值: void *somefunc (void* ptr){ int id = *(int*)ptr; } 但是,我注意到有时候,线程中的id会有重叠值,我怀疑是由于for循环更新的索引在线程能够检索值之前(因为我传入了指针,而不是值本身)。 有没有人有任何建议来克服这个问题,而不会减慢for循环? 谢谢

multithreading – 在arrays中我应该保护什么?

我正在研究一些具有全局数组的代码,可以通过两个线程访问这些代码来读写。 没有批处理会读取或写入一系列索引,所以我试图弄清楚我是应该锁定整个数组还是只锁定我当前正在使用的数组索引。 最简单的解决方案是将数组视为一个CS并对其进行大量锁定,但是我可以避免这种情况并锁定索引吗? 干杯。

线程具有相同的ID

我学习线程。 我已经读过该线程在一个函数之外终止(它作为参数传递给pthread_create函数)。 所以我在循环中创建线程,它们被执行,之后它们被终止。 (对不起一些长代码) 但是当我调用函数pthread_create时,新线程获得相同的ID。 为什么? struct data { FILE *f; }; void *read_line_of_file(void *gdata) { pthread_mutex_lock(&g_count_mutex); // only one thread can work with file, //doing so we block other threads from accessing it data *ldata = (data *) gdata; char line[80]; int ret_val =fscanf(ldata->f,”%s”,line); pthread_mutex_unlock(&g_count_mutex); // allow other threads to access it if (ret_val […]

pthread中函数的参数个数

在ploread的hello world示例中说明: #include #include void * print_hello(void *arg) { printf(“Hello world!\n”); return NULL; } int main(int argc, char **argv) { pthread_t thr; if(pthread_create(&thr, NULL, &print_hello, NULL)) { printf(“Could not create thread\n”); return -1; } if(pthread_join(thr, NULL)) { printf(“Could not join thread\n”); return -1; } return 0; } 正如您所看到的, pthread_create() print_hello没有参数,但是在定义中,它看起来像void * print_hello(void *arg) 那是什么意思? 现在假设我有这个实现 […]