Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 成功创建pthread_后,线程不执行任何操作_C++_Pthreads - Fatal编程技术网

C++ 成功创建pthread_后,线程不执行任何操作

C++ 成功创建pthread_后,线程不执行任何操作,c++,pthreads,C++,Pthreads,在我的项目中,我想创建一个线程,它什么也不做,只是在文本文件中附加一些字符串来测试它是否工作。我在Ubuntu12.04上使用IDEEclipseJuno。我的部分代码是: pthread_t processThread; threadData * thData = new threadData; int t = pthread_create(&processThread, NULL, BufferedData::processData,

在我的项目中,我想创建一个线程,它什么也不做,只是在文本文件中附加一些字符串来测试它是否工作。我在Ubuntu12.04上使用IDEEclipseJuno。我的部分代码是:

pthread_t processThread;
threadData * thData = new threadData;
int t = pthread_create(&processThread, NULL, 
                       BufferedData::processData, (void *)thData);
其中,threadData是带有线程参数的struct。类BufferedData的线程开始成员函数,因此processData方法是静态的。其声明如下:

static void * processData(void * arg);
在这部分代码之后,我检查t value——pthread_create的返回值。每次它都等于0,所以我认为线程的开始是成功的。但它仍然什么也不做——它不向文件追加字符串。processData函数做什么并不重要:将字符串追加到文件、抛出异常、写入cout或其他。它每次都不起作用

我不是经验丰富的C++程序员,所以我不知道该检查什么,编辑或做什么来解决这个问题。IDE并没有给我任何错误的回应,它面对的是一切正常

谢谢你的回答

编辑: processData函数的代码:

void * BufferedData::processData(void * arg) {
HelperFunctions h;
h.appendToFile("log", "test");
    return 0;
}

appendToFile方法将字符串“test”写入文件“log”。这是在其他项目中测试过的,它可以工作。

现在您的线程将在一段时间内完成(不是无限期),因此这可以帮助您:

int pthread_join(pthread_t thread, void **status);
在下面的
code
中,当创建线程时,然后
pthread\u join
函数等待线程返回。在这种状态下,使用
pthread\u exit()
而不是使用
return
关键字

试试这个
pthread\u join()

并使用
pthread\u exit()

注:

pthread\u join()
成功返回时,目标线程已分离。 多个线程不能使用
pthread\u join()
等待同一个目标线程结束。如果一个线程在另一个线程成功为同一个目标线程发出
pthread\u join()
后,为目标线程发出
pthread\u join()
,则第二个
pthread\u join()
将不成功


如果调用
pthread_join()
的线程被取消,则目标线程不会分离

向我们显示
BufferedData::processData
的代码如何?这可能会给你一个线索,你在等待线程完成后会发生什么?使用
pthread\u-join
很可能会在线程有机会执行任何操作之前终止程序(可能是从
main
返回)!谢谢但如果我想继续编写代码,不想等待线程完成其代码,该怎么办呢。现在我明白了。谢谢。
void *ret;
pthread_t processThread;
threadData * thData = new threadData;
int t = pthread_create(&processThread, NULL, 
                       BufferedData::processData, (void *)thData);

if (pthread_join(processThread, &ret) != 0) {
    perror("pthread_create() error");
    exit(3);
  }

   delete ret;      // dont forget to delete ret (avoiding of memory leak)
void * BufferedData::processData(void * arg) {
int *r = new int(10);
HelperFunctions h;
h.appendToFile("log", "test");
    pthread_exit(static_cast<void*>(a));
}
Description :

EDEADLK
    A deadlock has been detected. This can occur if the target is directly or indirectly joined to the current thread.
EINVAL
    The value specified by thread is not valid.
ESRCH
    The value specified by thread does not refer to an undetached thread.