所以,我知道你们喜欢讲细节,我保证会提供更多他们似乎真正相关的内容,但就我的情况而言,你们应该记住以下几点:
我有P线程是生产者
我有C线程是使用者
我有N个元素的缓冲区
我想在程序消耗M元素之后完成它。
我怎么能这么做?
下面是我的主要功能
int main(int argc, char *argv[]) {
int i;
/* Verify the correct number of arguments were passed in */
if(argc != 4) {
fprintf(stderr, "USAGE:.
我是C语言的新手,我正在努力学习。我试图在我的程序中实现多线程,但我遇到了问题。程序(和线程)应该在无限循环中运行,但实际上程序在几秒钟后就停止了。为了让程序永远运行,我能做些什么?我做错了什么吗?
#include <stdio.h>
#include <pthread.h>
void *func(void *threadid){
while (1)
{
/* do stuff, but the program terminates after that thread does few cycles */
}
}
int
我想为三个线程打印1到10。我的代码能够做到这一点,但在那之后,程序被卡住了。我试着在函数的末尾使用pthread_exit。另外,我尝试删除while (1) in main,并在那里使用p线程联接。但我还是得到了同样的结果。我应该如何终止线程?
enter code here
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
int done = 1;
//Thread function
void *foo()
{
嘿-我在我写的一个小玩具程序上遇到了一个奇怪的问题,用来测试线程。
这是我的代码:
#include <pthread.h>
#include <iostream>
using std::cout;
using std::endl;
void *threadFunc(void *arg) {
cout << "I am a thread. Hear me roar." << endl;
pthread_exit(NULL);
}
int main() {
cout << "Hel
我正在使用p线程库编写一个程序。当我使用命令valgrind --leak-check=full运行我的程序时,我得到以下错误描述:
==11784==
==11784== **HEAP SUMMARY:**
==11784== in use at exit: 4,952 bytes in 18 blocks
==11784== total heap usage: 1,059 allocs, 1,041 frees, 51,864 bytes allocated
==11784==
==11784== **288 bytes** in 1 blocks are
我的程序是这样的..我想知道如果我调用pthread join会不会让我的程序变慢?
void* a(void *a)
{
do---something();//which is a very long procedure, I mean takes a lot of time...
pthread_exit();
}
main()
{
while(1)
{
pthread_create(a);
pthread_join(a);
}
}
那么,如果我调用pthread_join,我是会在该点暂停,直到子线程完成执行,还是继续创建另一个线程?
我有下面的代码。
void *timer1_function(void * eit);
pthread_t timer1;
int thread_check1 = 0;
line72: thread_check1 = pthread_create( &timer1, NULL, timer1_function, NULL);
Valgrind显示了下面的输出,并指出line 72中存在问题。上面的pthread_create用法有什么问题?
272 bytes in 1 blocks are possibly lost in loss record 2 of 5
in main