标签: pthreads

使用线程传递指针时出错

更新代码:3/7/11:9:29 pm

using namespace std;

void * matrixACreate(void * param);  
void *status;

struct a  
{  
     int Arow; // Matrix A    
     int Acol; // WxX  
     int low;  // Range low  
     int high;  
 };  

int main(int argc, char * argv[])
{     
    struct a matrix_mult_info;  

    matrix_mult_info.Arow = atoi(argv[1]); // Matrix A  
    matrix_mult_info.Acol = atoi(argv[2]); // WxX

    matrix_mult_info.low = atoi(argv[5]); // Range low
    matrix_mult_info.high = atoi(argv[6]);

    pthread_t matrixAthread;

    pthread_t runner;  
    int error, retValue;  

    struct a * a = (struct a *) malloc(sizeof(struct a));   
    error …
Run Code Online (Sandbox Code Playgroud)

c++ unix g++ pthreads

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

带有gcc和pthreads的未定义引用错误_dl_stack_flags

尝试在Ubuntu上使用pthreads编译应用程序时出现以下错误:

/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/libpthread.a(pthread_create.o): In function `allocate_stack':
/build/buildd/eglibc-2.11.1/nptl/allocatestack.c:444: undefined reference to `_dl_stack_flags'
Run Code Online (Sandbox Code Playgroud)

Ubuntu版本是:

wade@wadesworld:~$ uname -a
Linux wadesworld 2.6.18-194.8.1.el5.028stab070.5ent #1 SMP Fri Sep 17 19:46:02 MSD 2010 i686 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

gcc版本是:

wade@wadesworld:~$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
Run Code Online (Sandbox Code Playgroud)

构建命令是:

/usr/bin/cmake -E cmake_link_script CMakeFiles/vtest.dir/link.txt …
Run Code Online (Sandbox Code Playgroud)

ubuntu gcc pthreads

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

pthread在c ++中创建错误

可能重复:
来自类的pthread函数

我收到一个错误("无法转换....."),我认为pthread_create调用中的第三个参数是错误的.我知道第三个参数的类型应该是(void*)*(void*)但我仍然会收到错误.

void ServerManager::Init(){  
     pthread_t thread;
     pthread_create(&thread, NULL, AcceptLoop, (void *)this);
}
Run Code Online (Sandbox Code Playgroud)

我已经这样声明了,我试着调用下面的函数

void* ServerManager::AcceptLoop(void * delegate){

}
Run Code Online (Sandbox Code Playgroud)

请让我知道如何解决这个问题..

提前致谢.

c++ pthreads

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

在成员函数中调用pthread_create?

我创建了一个包含声明的widget.h文件pthread_function,我想destroyWidget在widget.cpp中该类Widget 的成员函数中调用它.但总是显示错误.我将展示.cpp和.h文件.

widget.h文件


class Widget
{
public:
Widget();
void createWidget(int x,int y,int w,int h);
void showWidget();
int wid;
pthread_t thread;
int *incomingval,id;
void join();
Window win;
XEvent evt;
private:
void* destroyWidget(void* ptr);
Display *disp;
int screenNumber;
unsigned long white;
unsigned long black;
long eventMask;
GC gc;
int tbit;
int *incoming,val;
};

Run Code Online (Sandbox Code Playgroud)

现在是widget.cpp


Widget::Widget()
{
disp=XOpenDisplay( NULL );
screenNumber=DefaultScreen(disp);
white=WhitePixel(disp,screenNumber);
black=BlackPixel(disp,screenNumber);
eventMask=StructureNotifyMask;
tbit=0;
}

void Widget::createWidget(int x,int y,int w,int h)
{
wid=w;
win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),x,y,w,h,1,white,black);
}

void Widget::showWidget() …
Run Code Online (Sandbox Code Playgroud)

c++ x11 pthreads

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

在pthread_mutex_destroy之后我是否需要使用delete

假设我使用

pthread_mutex_t *m = new pthread_mutex_t;
pthread_mutex_init(m, NULL);
Run Code Online (Sandbox Code Playgroud)

初始化互斥锁.在我完成之后,调用pthread_mutex_destroy,我是否需要使用

delete m;
Run Code Online (Sandbox Code Playgroud)

释放所有资源?

c++ multithreading pthreads

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

最好是锁定共享资源,还是有线程来满足请求?

我有一个共享内存池,许多不同的线程可以从中请求分配.从这个请求分配将在每个线程中发生很多,但是线程的数量可能很小,通常只有1个线程在运行.我不确定以下哪种方法可以解决这个问题.

最终我可能需要实现两者并看看哪个产生更有利的结果......我也担心即使考虑#2也可能是过早的优化,因为我实际上并没有使用这个共享资源编写的代码.但问题是如此有趣,以至于它继续分散我对其他工作的注意力.

1)创建互斥锁并让线程在获取分配之前尝试锁定它,然后解锁它.

2)让每个线程注册一个请求槽,当需要分配时,它将请求放入槽中,然后阻塞(while(result == NULL){usleep()})等待请求槽有结果.单个线程连续迭代请求时隙,进行分配并将它们分配给请求时隙中的结果.

数字1是简单的解决方案,但如果时机正确,单个线程可能会占用锁定.第二个更复杂,但是当从资源中提取时,确保线程之间的公平性.但是它仍然会阻塞请求线程,如果有很多线程,迭代可以在没有进行任何实际分配的情况下刻录周期,直到找到要满足的请求.

注意:使用pthreads在Linux上使用C语言

c mutex locking pthreads task-queue

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

如何在C中使用互斥锁

我对在C中使用多个互斥锁感到困惑.

int main() {

        pthread_t thread1;
        char *message1 = "Thread 1";
        int  r;
        pthread_mutex_init(&mutex1, NULL);
        pthread_mutex_init(&mutex2, NULL);

        pthread_mutex_lock(&mutex1);

        r = pthread_create( &thread1, NULL, print_message_function, (void*) message1);

        printf("Parent 1\n");


        pthread_mutex_lock(&mutex2);
        printf("Parent 2\n");
        pthread_mutex_unlock(&mutex2);


        pthread_mutex_unlock(&mutex1);


        pthread_join( thread1, NULL);

        printf("Thread 1 returns: %d\n",r);
        return 0;
}

void *print_message_function( void *str ) {

        pthread_mutex_lock(&mutex1);
        char *message;
        message = (char *) str;
        printf("Child 1 received message: %s \n", message);


        pthread_mutex_lock(&mutex2);
        printf("child 2\n");
        pthread_mutex_unlock(&mutex2);




        pthread_mutex_unlock(&mutex1);

        return NULL;
}
Run Code Online (Sandbox Code Playgroud)

输出是

Parent 1
Parent 2
Child 1 received …
Run Code Online (Sandbox Code Playgroud)

c multithreading mutex pthreads

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

以下c ++线程代码会导致嵌入式系统上的进程崩溃吗?

为嵌入式设备编写如下类似的c ++代码.设备上运行的进程在启动时崩溃.在设备的某些其他版本上,未观察到崩溃.它可以与被调用的线程参数和线程分离有关.在正常的Linux桌面环境中它不会崩溃.任何人都可以发表意见.提前致谢.

#include <pthread.h>
#include <iostream>

using namespace std;


#define NUM_THREADS     2

void *PrintHello(void *msg)
{
    cout<<(char*)msg<<endl;
    while(1)
    {
        printf("Hello World! It's me, thread !\n");
        sleep(2);
    }
}


int main (int argc, char *argv[])
{
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    const char* ch = "hello how r u.i'm passing argument";
    for(t=0; t<NUM_THREADS; t++)
    {
        printf("In main: creating thread %ld\n", t);
        rc = pthread_create(&threads[t], NULL, PrintHello, (void *)ch);
        pthread_detach(threads[t]);
        if (rc)
        {
            printf("ERROR; return code from pthread_create() is %d\n", rc); …
Run Code Online (Sandbox Code Playgroud)

c++ pthreads

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

线程C客户端 - 服务器聊天应用程序中SIGSEGV的未知原因

我是软件工程专业的三年级大学生,参加过操作系统课程.

我一直在使用pthreads和套接字在C中使用客户端 - 服务器聊天应用程序.我一直在尝试使用pthread来提升客户端处理的并发性,而不会产生分叉的开销.(如果重要的话,我正在开发Ubuntu 11.04 x86).

而不是存储的一切,静态或全局/局部变量,我创建了两个结构化的数据类型,serverInfo_t并且clientInfo_t,其存储互斥,文件描述符,连接标志,和其他这样的信息.客户端列表实现为存储在serverInfo_t其中的简单单链表,在修改期间锁定和解锁.

在启动服务器应用程序时,我首先调用createServer(),它建立服务器,并最终启动一个子线程,其职责是监听新连接.此函数返回指向新分配serverInfo_t实例的指针,然后将其传递给调用createClient(serverInfo_t* pServer, int out_fd)以创建"admin"客户端,从而允许服务器本身用作客户端.这是我遇到问题的地方,因为它似乎一旦调用它就会产生段错误createClient().(一些修剪过的代码在下面:)

int main(int argc, char** argv)
{
    // ...
    // Get listenPort from argv[1].

    // Initialize the server.
    serverInfo_t* pServer = createServer(listenPort);

    // If createServer() failed, exit.
    if (!pServer) {
        fatal_error("ERROR: Unable to create server!"); // exit program
    }

    // Create and register the admin client.
    clientInfo_t* pAdmin = createClient(pServer, STDOUT_FILENO); // Outputs directly to stdout …
Run Code Online (Sandbox Code Playgroud)

c sockets gdb client-server pthreads

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

线程体未执行

我写了这段代码:

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

void* cuoco(void* arg)
{
    fprintf(stderr,"Inizio codice cuoco\n");
    fprintf(stderr,"Fine codice cuoco\n");
    return NULL;
}

void* cameriere(void* arg)
{
    fprintf(stderr,"Inizio codice cameriere\n");
    fprintf(stderr,"Fine codice cameriere\n");
    return NULL;
}

void* cliente(void* arg)
{
    fprintf(stderr,"Inizio codice cliente\n");
    fprintf(stderr,"Fine codice cliente\n");
    return NULL;
}

int main(int argc, char* argv[])
{
    void* (*routine)(void*);
    void* result;
    routine=cuoco;
    pthread_t thread_cuoco,thread_cameriere,thread_cliente;
    pthread_create(&thread_cuoco,NULL,routine,*argv);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

只是为了测试线程是如何工作的.但是cuoco函数的主体永远不会执行.因为它应该打印"Inizio codice cuoco"和"fine codice cuoco",但事实并非如此.

c linux pthreads

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

标签 统计

pthreads ×10

c++ ×5

c ×4

multithreading ×2

mutex ×2

client-server ×1

g++ ×1

gcc ×1

gdb ×1

linux ×1

locking ×1

sockets ×1

task-queue ×1

ubuntu ×1

unix ×1

x11 ×1