Tag: pthreads

如何产生n个线程?

我正在尝试编写一个multithreading程序,基于命令行输入的线程数,所以我不能硬编码预先声明的线程。 这是一种有效的方法吗? int threads = 5; // (dynamic, not hard-coded) int i = 0; pthread_t * thread = malloc(sizeof(pthread_t)*threads); for (i = 0; i < threads; i++) { pthread_t foobar; thread[i] = foobar; // will this cause a conflict? } for (i = 0; i < threads; i++) { int ret = pthread_create(&thread[i], NULL, (void […]

线程无法计数,给出错误的结果

我写了这段代码 #include /* Input/Output */ #include /* General Utilities */ #include /* POSIX Threads */ unsigned int cnt=0; /*Count variable%*/ const int NITERS=1000; void count() { int i=0; for(i=0; i<NITERS; i++) { cnt++; } pthread_exit(0); } int main() { pthread_t tid1,tid2; /* create threads 1 and 2 */ pthread_create(&tid1,NULL,count,NULL); pthread_create(&tid2,NULL,count,NULL); /* Main block now waits […]

C pthread mutex:`{‘之前的预期表达式

我正在使用pthread库来创建两个线程。 我使用两个队列来传递两个线程(生产者 – 消费者)之间的数据,因此希望有一个互斥体来同步线程中队列中的push-pops。 但我得到一个编译错误如下: $ gcc simple-tun.c simple-tun -lpthread simple-tun.c: In function ‘new_queue’: simple-tun.c:920:13: error: expected expression before ‘{‘ token 我得到错误的函数是: 908 struct queue * new_queue () { 909 910 struct queue * q; 911 q = (struct queue *) malloc (sizeof(struct queue)); 912 913 if (q == NULL) 914 return NULL; 915 916 […]

c /中断系统调用/ fork与线程

我发现了线程实现的问题,这对我来说很奇怪。 也许有些人可以向我解释一下,会很棒。 我正在开发类似代理的程序,一个程序(在不同的机器上运行),它通过eth0接收数据包并通过ath0(无线)发送到另一台完全相同的机器。 实际上我完全不确定是什么导致了我的问题,那是因为我对一切,linux和c编程都是新手。 我开始两个线程, 一个是在eth0上侦听(套接字)传入的数据包并通过ath0(也是套接字)发送出去 另一个线程正在监听ath0并通过eth0发送。 如果我使用线程,我会得到一个错误: sh-2.05b# ./socketex Failed to send network header packet. : Interrupted system call 如果我使用fork(),程序按预期工作。 有人可以向我解释这种行为吗? 只是为了显示发件人实现,这里是它的代码片段: while(keep_going) { memset(&buffer[0], ‘\0’, sizeof(buffer)); recvlen = recvfrom(sockfd_in, buffer, BUFLEN, 0, (struct sockaddr *) &incoming, &ilen); if(recvlen < 0) { perror("something went wrong / incoming\n"); exit(-1); } strcpy(msg, buffer); buflen = strlen(msg); […]

pthread_cond_wait()总能赢得锁定互斥锁的竞争吗?

这个问题与llnl中的pthread教程有关 。 说有三个主题。 线程1: pthread_mutex_lock(&mutex) do_something… if condition pthread_cond_signal(&con) pthread_mutex_unlock(&mutex) repeat 线程2: pthread_mutex_lock(&mutex) do_something… if condition pthread_cond_signal(&con) pthread_mutex_unlock(&mutex) repeat 线程3: pthread_mutex_lock(&mutex) while(condition not holds) pthread_cond_wait(&con) do_something… pthread_mutex_unlock(&mutex) 假设Thread1检查条件是否满足,然后发送信号以唤醒Thread3 。 最后它解锁了互斥锁。 但与此同时, 线程2正试图锁定互斥锁。 我的问题是:有没有保证Thread3将永远在竞争中获胜? 如果没有,则在Thread2 do_something …之后,可以更改条件变量,然后当Thread3锁定互斥锁时,条件变量与预期的不同。

运行给定线程的核心是什么?

是否有函数或任何其他方式以编程方式知道我的程序(pid)的给定线程正在运行的处理器的核心是什么? 如果可能的话,OpenMP或Pthreads解决方案都会对我有所帮助。 谢谢。

了解pthread_detach

以下打印 In Main() Hello World Hello World 为什么打印Hello World两次? 如果我使用pthread_join(),则会发生所需的输出(只有一个Hello World以In Main()开头。 #include void *thread_func(void *arg); int main(int argc, char **argv) { int s; void *res; pthread_t t1; s = pthread_create(&t1, NULL, thread_func, “Hello World\n”); if (s != 0) printf(“Err\n”); printf(“In Main()\n”); s = pthread_detach(t1); if (s != 0) printf(“Err\n”); return 0; } void *thread_func(void […]

C警告:不兼容的指针类型传递

我在尝试编译代码时遇到错误。 错误如下: warning: incompatible pointer types passing ‘void *(threadData *)’ to parameter of type ‘void * (*)(void *)’ [-Wincompatible-pointer-types] pthread_create(&threads[id], NULL, start,&data[id]); 我正在尝试将一个结构传递给函数, void * start(threadData* data) ,这一直让我失望。 有任何想法吗?

线程终止问题(c编程)

我正在使用C语言编写一个使用multithreading的Linux应用程序。 由main函数生成的线程完成大部分工作,因此通常最后完成。 我看到一些奇怪的行为,我相信这是由于主线程在生成的线程有机会完成其工作之前终止。 这里有一些示例代码来说明我在说什么: #define _POSIX_C_SOURCE 200112L #define _ISOC99_SOURCE #define __EXTENSIONS__ #define _GNU_SOURCE #include #include void my_cleanup(void *arg) { printf(“cleanup: %s\n”, (char *)arg); } void * thread_stuff(void *arg) { printf(“thread started\n”); pthread_cleanup_push(cleanup, “running”); if (arg) pthread_exit((void *)2); pthread_cleanup_pop(0); pthread_exit((void *)2); } int main() { int err; pthread_t tid1, tid2; err = pthread_create(&tid1, NULL, thread_stuff, (void […]

pthread_cancel()函数无法终止线程

我正在使用pthread_create()和pthread_cancel()函数来创建一个multithreading程序,但我注意到pthread_cancel()并没有真正终止它应该的线程。 void check(void *param){ header_t *Bag = (header_t *) param; pthread_t timer_thread; while(true){ if(Bag->size && TIMER_OFF){ pthread_create(&timer_thread, NULL, (void *) &timer, (void *) Bag); printf(“\nCREATE THREAD ID = %d\n”, timer_thread); // ADD } else if(!TIMER_OFF && Bag->size >= 2 && Bag->next->time next->tag){ printf(“\nOLD THREAD ID = %d TO BE CANCELLED\n”, timer_thread); pthread_cancel(timer_thread); pthread_create(&timer_thread, NULL, (void […]