标签: pthreads

使用线程编译程序中的错误

在编译时,我收到错误"未定义引用pthread_create()",类似于"未定义引用pthread_join()".可能的原因是什么?我无法识别它们.

c pthreads

0
推荐指数
1
解决办法
120
查看次数

为什么这个线程安全队列会造成死锁?

我已经编写了自己的线程安全队列版本.但是,当我运行此程序时,它会挂起/死锁.

想知道,为什么这会永远锁定/挂起.

void concurrentqueue::addtoQueue(const int number)
{
    locker currentlock(lock_for_queue);
    numberlist.push(number);
    pthread_cond_signal(&queue_availability_condition);
}

int concurrentqueue::getFromQueue()
{
    int number = 0;


    locker currentlock(lock_for_queue);
    if ( empty() )
    {
        pthread_cond_wait(&queue_availability_condition,&lock_for_queue);
    }

    number = numberlist.front();
    numberlist.pop();
    return number;
}

bool concurrentqueue::empty()
{       
    return numberlist.empty();
}
Run Code Online (Sandbox Code Playgroud)

我写过,班级储物柜是RAII.

class locker
{
public:
    locker(pthread_mutex_t& lockee): target(lockee)
    {
        pthread_mutex_lock(&target);
    }
    ~locker()
    {
        pthread_mutex_unlock(&target);
    }
private:
        pthread_mutex_t target;
};
Run Code Online (Sandbox Code Playgroud)

我的编剧/读者线程代码非常简单.Writer线程,添加到队列和读取器线程,从队列中读取.

void * writeintoqueue(void* myqueue)
{
    void *t = 0;
    concurrentqueue *localqueue = (concurrentqueue *) myqueue;

    for ( int i …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading stl pthreads

0
推荐指数
1
解决办法
1205
查看次数

互斥初始化 - (UNIX)

在下面的code.A 互斥是initialized.what是意义NULL.

pthread_mutex_init(&a->monitor,NULL);
Run Code Online (Sandbox Code Playgroud)

我想知道为什么我们传递NULL作为第二个参数.

c unix mutex pthreads

0
推荐指数
1
解决办法
2263
查看次数

请帮助编译这个程序

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>

void *WriteNumbers(void *threadArg)
{
 int start, stop;
 start = atoi((char *)threadArg);
 stop = start + 10;

 while(start<stop)
 {
  printf("%d\n", start++);
  sleep(1);
 }
}

int main(int argc, char **argv)
{
 pthread_t thread1, thread2;

 // create the threads and start the printing 
 pthread_create(&thread1, NULL, WriteNumbers, (void *)argv[1] );
 pthread_create(&thread2, NULL, WriteNumbers, (void *)argv[2]);

 pthread_join(thread1, NULL);
 pthread_join(thread2, NULL);

 return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc -o pthread pthread.c 
/tmp/cckJD3rd.o: In function `main':
pthread.c:(.text+0x7a): undefined reference to `pthread_create'
pthread.c:(.text+0xa2): undefined reference to `pthread_create' …
Run Code Online (Sandbox Code Playgroud)

c linux pthreads

0
推荐指数
2
解决办法
622
查看次数

无法从main()函数pthreads访问变量?

int main()
{
   int i; 
   pthread_t t; 
}
Run Code Online (Sandbox Code Playgroud)

看不到我?在主要内部创建,对吧?这意味着它必须使用相同的共享内存main()正在使用?如何在不制作全局变量的情况下让它看到我?

c++ pthreads

0
推荐指数
1
解决办法
2026
查看次数

pthread_exit(NULL)段错误

我有一个使用pthreads的项目; 主线程和子线程以及它们之间的管道.这一切都很好,除非有时它不起作用.子线程运行命令解释器,并且基于ncurses的GUI通过管道向其传送(一些)输入.

我正常创建线程(线程是pthread_t文件范围变量,interp_start是函数)

if (pthread_create(&thread, NULL, interp_start, NULL)) { perror("couldn't create thread"); return; }
Run Code Online (Sandbox Code Playgroud)

然后,如果解释器线程从用户收到"退出"命令,则调用interp_exit

fclose(output);
pthread_exit(NULL);
Run Code Online (Sandbox Code Playgroud)

主线程有一个select(),它检查输出的FD,并调用从FD读取()的函数:

int num=read(interp_output[0], &ch, 1);
if (num==0) shell_done();
if (num==-1) perror("read");
Run Code Online (Sandbox Code Playgroud)

通常可行的预期行为是关闭线程中的FILE*,这使得select()报告准备就绪,这使得read()发生,返回0,调用shell_done().经过一些简单而无关的清理后,这样做:

//fprintf(stderr, "joining thread\n");
pthread_join(thread, NULL);
//fprintf(stderr, "joined\n");
exit(EXIT_SUCCESS);
Run Code Online (Sandbox Code Playgroud)

所有这些有时是段错误.通常很好.如果我取消注释这两个printfs,如果它失败,我得不到(它在pthread_exit中的段错误)或者只是第一个(它在pthread_join中的段错误).

我不会在任何其他方面弄乱"线程",而我只处理空指针.这是怎么回事?我会在其他地方寻找,除非我在这两行之一一直遇到问题 - 甚至在restore_sem_to_pool.我认为它必须是我杀死线程的方式,但我正在做最简单的事情.

提前致谢...

c unix pthreads

0
推荐指数
1
解决办法
1463
查看次数

Pthread程序使用条件变量死锁

该计划正在努力实现的目标:

该程序应该同步"访客"和"汽车"的几个线程.游客们随意漫步,直到他们决定乘车.如果他们是第一次乘坐汽车并且有车可以乘坐,那么他们必须等到他们第一次排队或者车回来.如果没有排队的游客,车辆会等待,直到游客想要乘车.

更多背景信息:

我重拍使用条件变量作为接受的答案建议我的线程同步程序在这里.我知道我在正确的轨道上,但由于某些原因,我的程序仍然陷入僵局,对于我的生活,我无法弄清楚为什么.除非我给你代码,否则我不知道你怎么能帮助我,所以这里是:

问题:

1.)一小段后死锁

2.)有时候一位游客首先排队买车,但从不上车.

解决方案:

我的代码中有太多的错误......我认为,因为我会修复一个,我经常(不经意间)引入另一个.我一直在删除程序的功能,直到我消除了所有的错误,然后以一种不会使我的程序死锁的方式逐个构建功能.谢谢大家的建议.

c deadlock pthreads

0
推荐指数
1
解决办法
3381
查看次数

编译pthreads程序时出现问题

我尝试用这个命令编译这个简单的pthreads程序

$ gcc -pthread -o pthreads pthreads.c
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

void *myThread(void *arg);

int main()
{
    pthread_t mythread;
    int ret;

    ret = pthread_create( &mythread, NULL, myThread, NULL );

    if (ret != 0){
        printf( "Can't create pthread: %s", strerror(errno));
        exit(-1);
    }
    return 0;
}

void *myThread(void *arg){

    // Thread code goes here..
    printf("OK! NOW ON THE THREAD\n");
    pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud)

但是在尝试./pthreads时没有输出!

c gcc pthreads

0
推荐指数
1
解决办法
3179
查看次数

在辅助线程中调用select()/ pselect()会导致主线程阻塞吗?

我有一个我正在处理的应用程序需要几个辅助线程,每个应用程序都要负责一些文件句柄(至少1个,超过10个).文件句柄不在线程之间共享,因此我不必担心一个辅助线程在select查看准备读/写的内容时阻塞另一个.我想要确定的是,在执行select/ pselectcall时,两个辅助线程都不会导致主线程停止执行.

我认为这不是一个问题 - 人们可以想象这样的事情会在网络服务器上完成 - 但我找不到任何具体说"是的,你可以这样做",当我用Google搜索时.我是否认为这不会导致任何问题?

为了澄清,我看到的是:

  • 主要执行线程(select()循环处理传入命令消息和传出响应)

  • 辅助线程#1(select()提供服务的循环)

  • 辅助线程#2(select()提供另一个服务的循环)

正如我之前提到的,没有一个文件句柄在线程之间共享 - 它们是在一个单独的线程中创建,使用和销毁的,其他线程不知道它们的存在.

c io pthreads multiplexing

0
推荐指数
1
解决办法
1690
查看次数

如何修复此Pthread示例中的错误?

我做了所有必要的配置,比如包括Pthread库和头文件......

错误是:

error C3867: 'FunctionClass::calledfunction': function call missing argument list; use '&FunctionClass::calledfunction' to create a pointer to member
Run Code Online (Sandbox Code Playgroud)

这是导致错误的示例:

class FunctionClass
{

 public:

    void * calledfunction( void *);
    /*
    this is the definition of the function in the FunctionClass.cpp
    void * FunctionClass::calledfunction( void *)
    {
        //do sth;
    }

    */


};

int main(void)
{

pthread_t process_m;
FunctionClass *obj = new FunctionClass ();

int nbr= 5;

if(pthread_create(&process_m,NULL,obj->calledfunction,(void *)nbr)< 0)
    {
        std::cout << "thread1";

}
 }
Run Code Online (Sandbox Code Playgroud)

什么可能导致错误?我尊重函数pthread_create的语法......但是我找不到这个错误的原因!

c++ pthreads

0
推荐指数
1
解决办法
1052
查看次数

标签 统计

pthreads ×10

c ×7

c++ ×3

unix ×2

deadlock ×1

gcc ×1

io ×1

linux ×1

multiplexing ×1

multithreading ×1

mutex ×1

stl ×1